/kern_2.6.32/drivers/usb/atm/ueagle-atm.c

http://omnia2droid.googlecode.com/ · C · 2704 lines · 1990 code · 457 blank · 257 comment · 313 complexity · 44a0bd0e677b17f620f506cc0c763f86 MD5 · raw file

  1. /*-
  2. * Copyright (c) 2003, 2004
  3. * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
  4. *
  5. * Copyright (c) 2005-2007 Matthieu Castet <castet.matthieu@free.fr>
  6. * Copyright (c) 2005-2007 Stanislaw Gruszka <stf_xl@wp.pl>
  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. #include <linux/module.h>
  58. #include <linux/moduleparam.h>
  59. #include <linux/init.h>
  60. #include <linux/crc32.h>
  61. #include <linux/usb.h>
  62. #include <linux/firmware.h>
  63. #include <linux/ctype.h>
  64. #include <linux/sched.h>
  65. #include <linux/kthread.h>
  66. #include <linux/mutex.h>
  67. #include <linux/freezer.h>
  68. #include <asm/unaligned.h>
  69. #include "usbatm.h"
  70. #define EAGLEUSBVERSION "ueagle 1.4"
  71. /*
  72. * Debug macros
  73. */
  74. #define uea_dbg(usb_dev, format, args...) \
  75. do { \
  76. if (debug >= 1) \
  77. dev_dbg(&(usb_dev)->dev, \
  78. "[ueagle-atm dbg] %s: " format, \
  79. __func__, ##args); \
  80. } while (0)
  81. #define uea_vdbg(usb_dev, format, args...) \
  82. do { \
  83. if (debug >= 2) \
  84. dev_dbg(&(usb_dev)->dev, \
  85. "[ueagle-atm vdbg] " format, ##args); \
  86. } while (0)
  87. #define uea_enters(usb_dev) \
  88. uea_vdbg(usb_dev, "entering %s\n", __func__)
  89. #define uea_leaves(usb_dev) \
  90. uea_vdbg(usb_dev, "leaving %s\n", __func__)
  91. #define uea_err(usb_dev, format,args...) \
  92. dev_err(&(usb_dev)->dev ,"[UEAGLE-ATM] " format , ##args)
  93. #define uea_warn(usb_dev, format,args...) \
  94. dev_warn(&(usb_dev)->dev ,"[Ueagle-atm] " format, ##args)
  95. #define uea_info(usb_dev, format,args...) \
  96. dev_info(&(usb_dev)->dev ,"[ueagle-atm] " format, ##args)
  97. struct intr_pkt;
  98. /* cmv's from firmware */
  99. struct uea_cmvs_v1 {
  100. u32 address;
  101. u16 offset;
  102. u32 data;
  103. } __attribute__ ((packed));
  104. struct uea_cmvs_v2 {
  105. u32 group;
  106. u32 address;
  107. u32 offset;
  108. u32 data;
  109. } __attribute__ ((packed));
  110. /* information about currently processed cmv */
  111. struct cmv_dsc_e1 {
  112. u8 function;
  113. u16 idx;
  114. u32 address;
  115. u16 offset;
  116. };
  117. struct cmv_dsc_e4 {
  118. u16 function;
  119. u16 offset;
  120. u16 address;
  121. u16 group;
  122. };
  123. union cmv_dsc {
  124. struct cmv_dsc_e1 e1;
  125. struct cmv_dsc_e4 e4;
  126. };
  127. struct uea_softc {
  128. struct usb_device *usb_dev;
  129. struct usbatm_data *usbatm;
  130. int modem_index;
  131. unsigned int driver_info;
  132. int annex;
  133. #define ANNEXA 0
  134. #define ANNEXB 1
  135. int booting;
  136. int reset;
  137. wait_queue_head_t sync_q;
  138. struct task_struct *kthread;
  139. u32 data;
  140. u32 data1;
  141. int cmv_ack;
  142. union cmv_dsc cmv_dsc;
  143. struct work_struct task;
  144. struct workqueue_struct *work_q;
  145. u16 pageno;
  146. u16 ovl;
  147. const struct firmware *dsp_firm;
  148. struct urb *urb_int;
  149. void (*dispatch_cmv) (struct uea_softc *, struct intr_pkt *);
  150. void (*schedule_load_page) (struct uea_softc *, struct intr_pkt *);
  151. int (*stat) (struct uea_softc *);
  152. int (*send_cmvs) (struct uea_softc *);
  153. /* keep in sync with eaglectl */
  154. struct uea_stats {
  155. struct {
  156. u32 state;
  157. u32 flags;
  158. u32 mflags;
  159. u32 vidcpe;
  160. u32 vidco;
  161. u32 dsrate;
  162. u32 usrate;
  163. u32 dsunc;
  164. u32 usunc;
  165. u32 dscorr;
  166. u32 uscorr;
  167. u32 txflow;
  168. u32 rxflow;
  169. u32 usattenuation;
  170. u32 dsattenuation;
  171. u32 dsmargin;
  172. u32 usmargin;
  173. u32 firmid;
  174. } phy;
  175. } stats;
  176. };
  177. /*
  178. * Elsa IDs
  179. */
  180. #define ELSA_VID 0x05CC
  181. #define ELSA_PID_PSTFIRM 0x3350
  182. #define ELSA_PID_PREFIRM 0x3351
  183. #define ELSA_PID_A_PREFIRM 0x3352
  184. #define ELSA_PID_A_PSTFIRM 0x3353
  185. #define ELSA_PID_B_PREFIRM 0x3362
  186. #define ELSA_PID_B_PSTFIRM 0x3363
  187. /*
  188. * Devolo IDs : pots if (pid & 0x10)
  189. */
  190. #define DEVOLO_VID 0x1039
  191. #define DEVOLO_EAGLE_I_A_PID_PSTFIRM 0x2110
  192. #define DEVOLO_EAGLE_I_A_PID_PREFIRM 0x2111
  193. #define DEVOLO_EAGLE_I_B_PID_PSTFIRM 0x2100
  194. #define DEVOLO_EAGLE_I_B_PID_PREFIRM 0x2101
  195. #define DEVOLO_EAGLE_II_A_PID_PSTFIRM 0x2130
  196. #define DEVOLO_EAGLE_II_A_PID_PREFIRM 0x2131
  197. #define DEVOLO_EAGLE_II_B_PID_PSTFIRM 0x2120
  198. #define DEVOLO_EAGLE_II_B_PID_PREFIRM 0x2121
  199. /*
  200. * Reference design USB IDs
  201. */
  202. #define ANALOG_VID 0x1110
  203. #define ADI930_PID_PREFIRM 0x9001
  204. #define ADI930_PID_PSTFIRM 0x9000
  205. #define EAGLE_I_PID_PREFIRM 0x9010 /* Eagle I */
  206. #define EAGLE_I_PID_PSTFIRM 0x900F /* Eagle I */
  207. #define EAGLE_IIC_PID_PREFIRM 0x9024 /* Eagle IIC */
  208. #define EAGLE_IIC_PID_PSTFIRM 0x9023 /* Eagle IIC */
  209. #define EAGLE_II_PID_PREFIRM 0x9022 /* Eagle II */
  210. #define EAGLE_II_PID_PSTFIRM 0x9021 /* Eagle II */
  211. #define EAGLE_III_PID_PREFIRM 0x9032 /* Eagle III */
  212. #define EAGLE_III_PID_PSTFIRM 0x9031 /* Eagle III */
  213. #define EAGLE_IV_PID_PREFIRM 0x9042 /* Eagle IV */
  214. #define EAGLE_IV_PID_PSTFIRM 0x9041 /* Eagle IV */
  215. /*
  216. * USR USB IDs
  217. */
  218. #define USR_VID 0x0BAF
  219. #define MILLER_A_PID_PREFIRM 0x00F2
  220. #define MILLER_A_PID_PSTFIRM 0x00F1
  221. #define MILLER_B_PID_PREFIRM 0x00FA
  222. #define MILLER_B_PID_PSTFIRM 0x00F9
  223. #define HEINEKEN_A_PID_PREFIRM 0x00F6
  224. #define HEINEKEN_A_PID_PSTFIRM 0x00F5
  225. #define HEINEKEN_B_PID_PREFIRM 0x00F8
  226. #define HEINEKEN_B_PID_PSTFIRM 0x00F7
  227. #define PREFIRM 0
  228. #define PSTFIRM (1<<7)
  229. #define AUTO_ANNEX_A (1<<8)
  230. #define AUTO_ANNEX_B (1<<9)
  231. enum {
  232. ADI930 = 0,
  233. EAGLE_I,
  234. EAGLE_II,
  235. EAGLE_III,
  236. EAGLE_IV
  237. };
  238. /* macros for both struct usb_device_id and struct uea_softc */
  239. #define UEA_IS_PREFIRM(x) \
  240. (!((x)->driver_info & PSTFIRM))
  241. #define UEA_CHIP_VERSION(x) \
  242. ((x)->driver_info & 0xf)
  243. #define IS_ISDN(x) \
  244. ((x)->annex & ANNEXB)
  245. #define INS_TO_USBDEV(ins) ins->usb_dev
  246. #define GET_STATUS(data) \
  247. ((data >> 8) & 0xf)
  248. #define IS_OPERATIONAL(sc) \
  249. ((UEA_CHIP_VERSION(sc) != EAGLE_IV) ? \
  250. (GET_STATUS(sc->stats.phy.state) == 2) : \
  251. (sc->stats.phy.state == 7))
  252. /*
  253. * Set of macros to handle unaligned data in the firmware blob.
  254. * The FW_GET_BYTE() macro is provided only for consistency.
  255. */
  256. #define FW_GET_BYTE(p) *((__u8 *) (p))
  257. #define FW_DIR "ueagle-atm/"
  258. #define UEA_FW_NAME_MAX 30
  259. #define NB_MODEM 4
  260. #define BULK_TIMEOUT 300
  261. #define CTRL_TIMEOUT 1000
  262. #define ACK_TIMEOUT msecs_to_jiffies(3000)
  263. #define UEA_INTR_IFACE_NO 0
  264. #define UEA_US_IFACE_NO 1
  265. #define UEA_DS_IFACE_NO 2
  266. #define FASTEST_ISO_INTF 8
  267. #define UEA_BULK_DATA_PIPE 0x02
  268. #define UEA_IDMA_PIPE 0x04
  269. #define UEA_INTR_PIPE 0x04
  270. #define UEA_ISO_DATA_PIPE 0x08
  271. #define UEA_E1_SET_BLOCK 0x0001
  272. #define UEA_E4_SET_BLOCK 0x002c
  273. #define UEA_SET_MODE 0x0003
  274. #define UEA_SET_2183_DATA 0x0004
  275. #define UEA_SET_TIMEOUT 0x0011
  276. #define UEA_LOOPBACK_OFF 0x0002
  277. #define UEA_LOOPBACK_ON 0x0003
  278. #define UEA_BOOT_IDMA 0x0006
  279. #define UEA_START_RESET 0x0007
  280. #define UEA_END_RESET 0x0008
  281. #define UEA_SWAP_MAILBOX (0x3fcd | 0x4000)
  282. #define UEA_MPTX_START (0x3fce | 0x4000)
  283. #define UEA_MPTX_MAILBOX (0x3fd6 | 0x4000)
  284. #define UEA_MPRX_MAILBOX (0x3fdf | 0x4000)
  285. /* block information in eagle4 dsp firmware */
  286. struct block_index {
  287. __le32 PageOffset;
  288. __le32 NotLastBlock;
  289. __le32 dummy;
  290. __le32 PageSize;
  291. __le32 PageAddress;
  292. __le16 dummy1;
  293. __le16 PageNumber;
  294. } __attribute__ ((packed));
  295. #define E4_IS_BOOT_PAGE(PageSize) ((le32_to_cpu(PageSize)) & 0x80000000)
  296. #define E4_PAGE_BYTES(PageSize) ((le32_to_cpu(PageSize) & 0x7fffffff) * 4)
  297. #define E4_L1_STRING_HEADER 0x10
  298. #define E4_MAX_PAGE_NUMBER 0x58
  299. #define E4_NO_SWAPPAGE_HEADERS 0x31
  300. /* l1_code is eagle4 dsp firmware format */
  301. struct l1_code {
  302. u8 string_header[E4_L1_STRING_HEADER];
  303. u8 page_number_to_block_index[E4_MAX_PAGE_NUMBER];
  304. struct block_index page_header[E4_NO_SWAPPAGE_HEADERS];
  305. u8 code [0];
  306. } __attribute__ ((packed));
  307. /* structures describing a block within a DSP page */
  308. struct block_info_e1 {
  309. __le16 wHdr;
  310. __le16 wAddress;
  311. __le16 wSize;
  312. __le16 wOvlOffset;
  313. __le16 wOvl; /* overlay */
  314. __le16 wLast;
  315. } __attribute__ ((packed));
  316. #define E1_BLOCK_INFO_SIZE 12
  317. struct block_info_e4 {
  318. __be16 wHdr;
  319. __u8 bBootPage;
  320. __u8 bPageNumber;
  321. __be32 dwSize;
  322. __be32 dwAddress;
  323. __be16 wReserved;
  324. } __attribute__ ((packed));
  325. #define E4_BLOCK_INFO_SIZE 14
  326. #define UEA_BIHDR 0xabcd
  327. #define UEA_RESERVED 0xffff
  328. /* constants describing cmv type */
  329. #define E1_PREAMBLE 0x535c
  330. #define E1_MODEMTOHOST 0x01
  331. #define E1_HOSTTOMODEM 0x10
  332. #define E1_MEMACCESS 0x1
  333. #define E1_ADSLDIRECTIVE 0x7
  334. #define E1_FUNCTION_TYPE(f) ((f) >> 4)
  335. #define E1_FUNCTION_SUBTYPE(f) ((f) & 0x0f)
  336. #define E4_MEMACCESS 0
  337. #define E4_ADSLDIRECTIVE 0xf
  338. #define E4_FUNCTION_TYPE(f) ((f) >> 8)
  339. #define E4_FUNCTION_SIZE(f) ((f) & 0x0f)
  340. #define E4_FUNCTION_SUBTYPE(f) (((f) >> 4) & 0x0f)
  341. /* for MEMACCESS */
  342. #define E1_REQUESTREAD 0x0
  343. #define E1_REQUESTWRITE 0x1
  344. #define E1_REPLYREAD 0x2
  345. #define E1_REPLYWRITE 0x3
  346. #define E4_REQUESTREAD 0x0
  347. #define E4_REQUESTWRITE 0x4
  348. #define E4_REPLYREAD (E4_REQUESTREAD | 1)
  349. #define E4_REPLYWRITE (E4_REQUESTWRITE | 1)
  350. /* for ADSLDIRECTIVE */
  351. #define E1_KERNELREADY 0x0
  352. #define E1_MODEMREADY 0x1
  353. #define E4_KERNELREADY 0x0
  354. #define E4_MODEMREADY 0x1
  355. #define E1_MAKEFUNCTION(t, s) (((t) & 0xf) << 4 | ((s) & 0xf))
  356. #define E4_MAKEFUNCTION(t, st, s) (((t) & 0xf) << 8 | ((st) & 0xf) << 4 | ((s) & 0xf))
  357. #define E1_MAKESA(a, b, c, d) \
  358. (((c) & 0xff) << 24 | \
  359. ((d) & 0xff) << 16 | \
  360. ((a) & 0xff) << 8 | \
  361. ((b) & 0xff))
  362. #define E1_GETSA1(a) ((a >> 8) & 0xff)
  363. #define E1_GETSA2(a) (a & 0xff)
  364. #define E1_GETSA3(a) ((a >> 24) & 0xff)
  365. #define E1_GETSA4(a) ((a >> 16) & 0xff)
  366. #define E1_SA_CNTL E1_MAKESA('C', 'N', 'T', 'L')
  367. #define E1_SA_DIAG E1_MAKESA('D', 'I', 'A', 'G')
  368. #define E1_SA_INFO E1_MAKESA('I', 'N', 'F', 'O')
  369. #define E1_SA_OPTN E1_MAKESA('O', 'P', 'T', 'N')
  370. #define E1_SA_RATE E1_MAKESA('R', 'A', 'T', 'E')
  371. #define E1_SA_STAT E1_MAKESA('S', 'T', 'A', 'T')
  372. #define E4_SA_CNTL 1
  373. #define E4_SA_STAT 2
  374. #define E4_SA_INFO 3
  375. #define E4_SA_TEST 4
  376. #define E4_SA_OPTN 5
  377. #define E4_SA_RATE 6
  378. #define E4_SA_DIAG 7
  379. #define E4_SA_CNFG 8
  380. /* structures representing a CMV (Configuration and Management Variable) */
  381. struct cmv_e1 {
  382. __le16 wPreamble;
  383. __u8 bDirection;
  384. __u8 bFunction;
  385. __le16 wIndex;
  386. __le32 dwSymbolicAddress;
  387. __le16 wOffsetAddress;
  388. __le32 dwData;
  389. } __attribute__ ((packed));
  390. struct cmv_e4 {
  391. __be16 wGroup;
  392. __be16 wFunction;
  393. __be16 wOffset;
  394. __be16 wAddress;
  395. __be32 dwData [6];
  396. } __attribute__ ((packed));
  397. /* structures representing swap information */
  398. struct swap_info_e1 {
  399. __u8 bSwapPageNo;
  400. __u8 bOvl; /* overlay */
  401. } __attribute__ ((packed));
  402. struct swap_info_e4 {
  403. __u8 bSwapPageNo;
  404. } __attribute__ ((packed));
  405. /* structures representing interrupt data */
  406. #define e1_bSwapPageNo u.e1.s1.swapinfo.bSwapPageNo
  407. #define e1_bOvl u.e1.s1.swapinfo.bOvl
  408. #define e4_bSwapPageNo u.e4.s1.swapinfo.bSwapPageNo
  409. #define INT_LOADSWAPPAGE 0x0001
  410. #define INT_INCOMINGCMV 0x0002
  411. union intr_data_e1 {
  412. struct {
  413. struct swap_info_e1 swapinfo;
  414. __le16 wDataSize;
  415. } __attribute__ ((packed)) s1;
  416. struct {
  417. struct cmv_e1 cmv;
  418. __le16 wDataSize;
  419. } __attribute__ ((packed)) s2;
  420. } __attribute__ ((packed));
  421. union intr_data_e4 {
  422. struct {
  423. struct swap_info_e4 swapinfo;
  424. __le16 wDataSize;
  425. } __attribute__ ((packed)) s1;
  426. struct {
  427. struct cmv_e4 cmv;
  428. __le16 wDataSize;
  429. } __attribute__ ((packed)) s2;
  430. } __attribute__ ((packed));
  431. struct intr_pkt {
  432. __u8 bType;
  433. __u8 bNotification;
  434. __le16 wValue;
  435. __le16 wIndex;
  436. __le16 wLength;
  437. __le16 wInterrupt;
  438. union {
  439. union intr_data_e1 e1;
  440. union intr_data_e4 e4;
  441. } u;
  442. } __attribute__ ((packed));
  443. #define E1_INTR_PKT_SIZE 28
  444. #define E4_INTR_PKT_SIZE 64
  445. static struct usb_driver uea_driver;
  446. static DEFINE_MUTEX(uea_mutex);
  447. static const char *chip_name[] = {"ADI930", "Eagle I", "Eagle II", "Eagle III", "Eagle IV"};
  448. static int modem_index;
  449. static unsigned int debug;
  450. static unsigned int altsetting[NB_MODEM] = {[0 ... (NB_MODEM - 1)] = FASTEST_ISO_INTF};
  451. static int sync_wait[NB_MODEM];
  452. static char *cmv_file[NB_MODEM];
  453. static int annex[NB_MODEM];
  454. module_param(debug, uint, 0644);
  455. MODULE_PARM_DESC(debug, "module debug level (0=off,1=on,2=verbose)");
  456. module_param_array(altsetting, uint, NULL, 0644);
  457. MODULE_PARM_DESC(altsetting, "alternate setting for incoming traffic: 0=bulk, "
  458. "1=isoc slowest, ... , 8=isoc fastest (default)");
  459. module_param_array(sync_wait, bool, NULL, 0644);
  460. MODULE_PARM_DESC(sync_wait, "wait the synchronisation before starting ATM");
  461. module_param_array(cmv_file, charp, NULL, 0644);
  462. MODULE_PARM_DESC(cmv_file,
  463. "file name with configuration and management variables");
  464. module_param_array(annex, uint, NULL, 0644);
  465. MODULE_PARM_DESC(annex,
  466. "manually set annex a/b (0=auto, 1=annex a, 2=annex b)");
  467. #define uea_wait(sc, cond, timeo) \
  468. ({ \
  469. int _r = wait_event_interruptible_timeout(sc->sync_q, \
  470. (cond) || kthread_should_stop(), timeo); \
  471. if (kthread_should_stop()) \
  472. _r = -ENODEV; \
  473. _r; \
  474. })
  475. #define UPDATE_ATM_STAT(type, val) \
  476. do { \
  477. if (sc->usbatm->atm_dev) \
  478. sc->usbatm->atm_dev->type = val; \
  479. } while (0)
  480. /* Firmware loading */
  481. #define LOAD_INTERNAL 0xA0
  482. #define F8051_USBCS 0x7f92
  483. /**
  484. * uea_send_modem_cmd - Send a command for pre-firmware devices.
  485. */
  486. static int uea_send_modem_cmd(struct usb_device *usb,
  487. u16 addr, u16 size, const u8 *buff)
  488. {
  489. int ret = -ENOMEM;
  490. u8 *xfer_buff;
  491. xfer_buff = kmemdup(buff, size, GFP_KERNEL);
  492. if (xfer_buff) {
  493. ret = usb_control_msg(usb,
  494. usb_sndctrlpipe(usb, 0),
  495. LOAD_INTERNAL,
  496. USB_DIR_OUT | USB_TYPE_VENDOR |
  497. USB_RECIP_DEVICE, addr, 0, xfer_buff,
  498. size, CTRL_TIMEOUT);
  499. kfree(xfer_buff);
  500. }
  501. if (ret < 0)
  502. return ret;
  503. return (ret == size) ? 0 : -EIO;
  504. }
  505. static void uea_upload_pre_firmware(const struct firmware *fw_entry, void *context)
  506. {
  507. struct usb_device *usb = context;
  508. const u8 *pfw;
  509. u8 value;
  510. u32 crc = 0;
  511. int ret, size;
  512. uea_enters(usb);
  513. if (!fw_entry) {
  514. uea_err(usb, "firmware is not available\n");
  515. goto err;
  516. }
  517. pfw = fw_entry->data;
  518. size = fw_entry->size;
  519. if (size < 4)
  520. goto err_fw_corrupted;
  521. crc = get_unaligned_le32(pfw);
  522. pfw += 4;
  523. size -= 4;
  524. if (crc32_be(0, pfw, size) != crc)
  525. goto err_fw_corrupted;
  526. /*
  527. * Start to upload firmware : send reset
  528. */
  529. value = 1;
  530. ret = uea_send_modem_cmd(usb, F8051_USBCS, sizeof(value), &value);
  531. if (ret < 0) {
  532. uea_err(usb, "modem reset failed with error %d\n", ret);
  533. goto err;
  534. }
  535. while (size > 3) {
  536. u8 len = FW_GET_BYTE(pfw);
  537. u16 add = get_unaligned_le16(pfw + 1);
  538. size -= len + 3;
  539. if (size < 0)
  540. goto err_fw_corrupted;
  541. ret = uea_send_modem_cmd(usb, add, len, pfw + 3);
  542. if (ret < 0) {
  543. uea_err(usb, "uploading firmware data failed "
  544. "with error %d\n", ret);
  545. goto err;
  546. }
  547. pfw += len + 3;
  548. }
  549. if (size != 0)
  550. goto err_fw_corrupted;
  551. /*
  552. * Tell the modem we finish : de-assert reset
  553. */
  554. value = 0;
  555. ret = uea_send_modem_cmd(usb, F8051_USBCS, 1, &value);
  556. if (ret < 0)
  557. uea_err(usb, "modem de-assert failed with error %d\n", ret);
  558. else
  559. uea_info(usb, "firmware uploaded\n");
  560. uea_leaves(usb);
  561. return;
  562. err_fw_corrupted:
  563. uea_err(usb, "firmware is corrupted\n");
  564. err:
  565. uea_leaves(usb);
  566. }
  567. /**
  568. * uea_load_firmware - Load usb firmware for pre-firmware devices.
  569. */
  570. static int uea_load_firmware(struct usb_device *usb, unsigned int ver)
  571. {
  572. int ret;
  573. char *fw_name = FW_DIR "eagle.fw";
  574. uea_enters(usb);
  575. uea_info(usb, "pre-firmware device, uploading firmware\n");
  576. switch (ver) {
  577. case ADI930:
  578. fw_name = FW_DIR "adi930.fw";
  579. break;
  580. case EAGLE_I:
  581. fw_name = FW_DIR "eagleI.fw";
  582. break;
  583. case EAGLE_II:
  584. fw_name = FW_DIR "eagleII.fw";
  585. break;
  586. case EAGLE_III:
  587. fw_name = FW_DIR "eagleIII.fw";
  588. break;
  589. case EAGLE_IV:
  590. fw_name = FW_DIR "eagleIV.fw";
  591. break;
  592. }
  593. ret = request_firmware_nowait(THIS_MODULE, 1, fw_name, &usb->dev, usb, uea_upload_pre_firmware);
  594. if (ret)
  595. uea_err(usb, "firmware %s is not available\n", fw_name);
  596. else
  597. uea_info(usb, "loading firmware %s\n", fw_name);
  598. uea_leaves(usb);
  599. return ret;
  600. }
  601. /* modem management : dsp firmware, send/read CMV, monitoring statistic
  602. */
  603. /*
  604. * Make sure that the DSP code provided is safe to use.
  605. */
  606. static int check_dsp_e1(const u8 *dsp, unsigned int len)
  607. {
  608. u8 pagecount, blockcount;
  609. u16 blocksize;
  610. u32 pageoffset;
  611. unsigned int i, j, p, pp;
  612. pagecount = FW_GET_BYTE(dsp);
  613. p = 1;
  614. /* enough space for page offsets? */
  615. if (p + 4 * pagecount > len)
  616. return 1;
  617. for (i = 0; i < pagecount; i++) {
  618. pageoffset = get_unaligned_le32(dsp + p);
  619. p += 4;
  620. if (pageoffset == 0)
  621. continue;
  622. /* enough space for blockcount? */
  623. if (pageoffset >= len)
  624. return 1;
  625. pp = pageoffset;
  626. blockcount = FW_GET_BYTE(dsp + pp);
  627. pp += 1;
  628. for (j = 0; j < blockcount; j++) {
  629. /* enough space for block header? */
  630. if (pp + 4 > len)
  631. return 1;
  632. pp += 2; /* skip blockaddr */
  633. blocksize = get_unaligned_le16(dsp + pp);
  634. pp += 2;
  635. /* enough space for block data? */
  636. if (pp + blocksize > len)
  637. return 1;
  638. pp += blocksize;
  639. }
  640. }
  641. return 0;
  642. }
  643. static int check_dsp_e4(const u8 *dsp, int len)
  644. {
  645. int i;
  646. struct l1_code *p = (struct l1_code *) dsp;
  647. unsigned int sum = p->code - dsp;
  648. if (len < sum)
  649. return 1;
  650. if (strcmp("STRATIPHY ANEXA", p->string_header) != 0 &&
  651. strcmp("STRATIPHY ANEXB", p->string_header) != 0)
  652. return 1;
  653. for (i = 0; i < E4_MAX_PAGE_NUMBER; i++) {
  654. struct block_index *blockidx;
  655. u8 blockno = p->page_number_to_block_index[i];
  656. if (blockno >= E4_NO_SWAPPAGE_HEADERS)
  657. continue;
  658. do {
  659. u64 l;
  660. if (blockno >= E4_NO_SWAPPAGE_HEADERS)
  661. return 1;
  662. blockidx = &p->page_header[blockno++];
  663. if ((u8 *)(blockidx + 1) - dsp >= len)
  664. return 1;
  665. if (le16_to_cpu(blockidx->PageNumber) != i)
  666. return 1;
  667. l = E4_PAGE_BYTES(blockidx->PageSize);
  668. sum += l;
  669. l += le32_to_cpu(blockidx->PageOffset);
  670. if (l > len)
  671. return 1;
  672. /* zero is zero regardless endianes */
  673. } while (blockidx->NotLastBlock);
  674. }
  675. return (sum == len) ? 0 : 1;
  676. }
  677. /*
  678. * send data to the idma pipe
  679. * */
  680. static int uea_idma_write(struct uea_softc *sc, const void *data, u32 size)
  681. {
  682. int ret = -ENOMEM;
  683. u8 *xfer_buff;
  684. int bytes_read;
  685. xfer_buff = kmemdup(data, size, GFP_KERNEL);
  686. if (!xfer_buff) {
  687. uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
  688. return ret;
  689. }
  690. ret = usb_bulk_msg(sc->usb_dev,
  691. usb_sndbulkpipe(sc->usb_dev, UEA_IDMA_PIPE),
  692. xfer_buff, size, &bytes_read, BULK_TIMEOUT);
  693. kfree(xfer_buff);
  694. if (ret < 0)
  695. return ret;
  696. if (size != bytes_read) {
  697. uea_err(INS_TO_USBDEV(sc), "size != bytes_read %d %d\n", size,
  698. bytes_read);
  699. return -EIO;
  700. }
  701. return 0;
  702. }
  703. static int request_dsp(struct uea_softc *sc)
  704. {
  705. int ret;
  706. char *dsp_name;
  707. if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
  708. if (IS_ISDN(sc))
  709. dsp_name = FW_DIR "DSP4i.bin";
  710. else
  711. dsp_name = FW_DIR "DSP4p.bin";
  712. } else if (UEA_CHIP_VERSION(sc) == ADI930) {
  713. if (IS_ISDN(sc))
  714. dsp_name = FW_DIR "DSP9i.bin";
  715. else
  716. dsp_name = FW_DIR "DSP9p.bin";
  717. } else {
  718. if (IS_ISDN(sc))
  719. dsp_name = FW_DIR "DSPei.bin";
  720. else
  721. dsp_name = FW_DIR "DSPep.bin";
  722. }
  723. ret = request_firmware(&sc->dsp_firm, dsp_name, &sc->usb_dev->dev);
  724. if (ret < 0) {
  725. uea_err(INS_TO_USBDEV(sc),
  726. "requesting firmware %s failed with error %d\n",
  727. dsp_name, ret);
  728. return ret;
  729. }
  730. if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
  731. ret = check_dsp_e4(sc->dsp_firm->data, sc->dsp_firm->size);
  732. else
  733. ret = check_dsp_e1(sc->dsp_firm->data, sc->dsp_firm->size);
  734. if (ret) {
  735. uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
  736. dsp_name);
  737. release_firmware(sc->dsp_firm);
  738. sc->dsp_firm = NULL;
  739. return -EILSEQ;
  740. }
  741. return 0;
  742. }
  743. /*
  744. * The uea_load_page() function must be called within a process context
  745. */
  746. static void uea_load_page_e1(struct work_struct *work)
  747. {
  748. struct uea_softc *sc = container_of(work, struct uea_softc, task);
  749. u16 pageno = sc->pageno;
  750. u16 ovl = sc->ovl;
  751. struct block_info_e1 bi;
  752. const u8 *p;
  753. u8 pagecount, blockcount;
  754. u16 blockaddr, blocksize;
  755. u32 pageoffset;
  756. int i;
  757. /* reload firmware when reboot start and it's loaded already */
  758. if (ovl == 0 && pageno == 0 && sc->dsp_firm) {
  759. release_firmware(sc->dsp_firm);
  760. sc->dsp_firm = NULL;
  761. }
  762. if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
  763. return;
  764. p = sc->dsp_firm->data;
  765. pagecount = FW_GET_BYTE(p);
  766. p += 1;
  767. if (pageno >= pagecount)
  768. goto bad1;
  769. p += 4 * pageno;
  770. pageoffset = get_unaligned_le32(p);
  771. if (pageoffset == 0)
  772. goto bad1;
  773. p = sc->dsp_firm->data + pageoffset;
  774. blockcount = FW_GET_BYTE(p);
  775. p += 1;
  776. uea_dbg(INS_TO_USBDEV(sc),
  777. "sending %u blocks for DSP page %u\n", blockcount, pageno);
  778. bi.wHdr = cpu_to_le16(UEA_BIHDR);
  779. bi.wOvl = cpu_to_le16(ovl);
  780. bi.wOvlOffset = cpu_to_le16(ovl | 0x8000);
  781. for (i = 0; i < blockcount; i++) {
  782. blockaddr = get_unaligned_le16(p);
  783. p += 2;
  784. blocksize = get_unaligned_le16(p);
  785. p += 2;
  786. bi.wSize = cpu_to_le16(blocksize);
  787. bi.wAddress = cpu_to_le16(blockaddr);
  788. bi.wLast = cpu_to_le16((i == blockcount - 1) ? 1 : 0);
  789. /* send block info through the IDMA pipe */
  790. if (uea_idma_write(sc, &bi, E1_BLOCK_INFO_SIZE))
  791. goto bad2;
  792. /* send block data through the IDMA pipe */
  793. if (uea_idma_write(sc, p, blocksize))
  794. goto bad2;
  795. p += blocksize;
  796. }
  797. return;
  798. bad2:
  799. uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", i);
  800. return;
  801. bad1:
  802. uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
  803. }
  804. static void __uea_load_page_e4(struct uea_softc *sc, u8 pageno, int boot)
  805. {
  806. struct block_info_e4 bi;
  807. struct block_index *blockidx;
  808. struct l1_code *p = (struct l1_code *) sc->dsp_firm->data;
  809. u8 blockno = p->page_number_to_block_index[pageno];
  810. bi.wHdr = cpu_to_be16(UEA_BIHDR);
  811. bi.bBootPage = boot;
  812. bi.bPageNumber = pageno;
  813. bi.wReserved = cpu_to_be16(UEA_RESERVED);
  814. do {
  815. const u8 *blockoffset;
  816. unsigned int blocksize;
  817. blockidx = &p->page_header[blockno];
  818. blocksize = E4_PAGE_BYTES(blockidx->PageSize);
  819. blockoffset = sc->dsp_firm->data + le32_to_cpu(blockidx->PageOffset);
  820. bi.dwSize = cpu_to_be32(blocksize);
  821. bi.dwAddress = cpu_to_be32(le32_to_cpu(blockidx->PageAddress));
  822. uea_dbg(INS_TO_USBDEV(sc),
  823. "sending block %u for DSP page %u size %u address %x\n",
  824. blockno, pageno, blocksize, le32_to_cpu(blockidx->PageAddress));
  825. /* send block info through the IDMA pipe */
  826. if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
  827. goto bad;
  828. /* send block data through the IDMA pipe */
  829. if (uea_idma_write(sc, blockoffset, blocksize))
  830. goto bad;
  831. blockno++;
  832. } while (blockidx->NotLastBlock);
  833. return;
  834. bad:
  835. uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", blockno);
  836. return;
  837. }
  838. static void uea_load_page_e4(struct work_struct *work)
  839. {
  840. struct uea_softc *sc = container_of(work, struct uea_softc, task);
  841. u8 pageno = sc->pageno;
  842. int i;
  843. struct block_info_e4 bi;
  844. struct l1_code *p;
  845. uea_dbg(INS_TO_USBDEV(sc), "sending DSP page %u\n", pageno);
  846. /* reload firmware when reboot start and it's loaded already */
  847. if (pageno == 0 && sc->dsp_firm) {
  848. release_firmware(sc->dsp_firm);
  849. sc->dsp_firm = NULL;
  850. }
  851. if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
  852. return;
  853. p = (struct l1_code *) sc->dsp_firm->data;
  854. if (pageno >= le16_to_cpu(p->page_header[0].PageNumber)) {
  855. uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
  856. return;
  857. }
  858. if (pageno != 0) {
  859. __uea_load_page_e4(sc, pageno, 0);
  860. return;
  861. }
  862. uea_dbg(INS_TO_USBDEV(sc),
  863. "sending Main DSP page %u\n", p->page_header[0].PageNumber);
  864. for (i = 0; i < le16_to_cpu(p->page_header[0].PageNumber); i++) {
  865. if (E4_IS_BOOT_PAGE(p->page_header[i].PageSize))
  866. __uea_load_page_e4(sc, i, 1);
  867. }
  868. uea_dbg(INS_TO_USBDEV(sc),"sending start bi\n");
  869. bi.wHdr = cpu_to_be16(UEA_BIHDR);
  870. bi.bBootPage = 0;
  871. bi.bPageNumber = 0xff;
  872. bi.wReserved = cpu_to_be16(UEA_RESERVED);
  873. bi.dwSize = cpu_to_be32(E4_PAGE_BYTES(p->page_header[0].PageSize));
  874. bi.dwAddress = cpu_to_be32(le32_to_cpu(p->page_header[0].PageAddress));
  875. /* send block info through the IDMA pipe */
  876. if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
  877. uea_err(INS_TO_USBDEV(sc), "sending DSP start bi failed\n");
  878. }
  879. static inline void wake_up_cmv_ack(struct uea_softc *sc)
  880. {
  881. BUG_ON(sc->cmv_ack);
  882. sc->cmv_ack = 1;
  883. wake_up(&sc->sync_q);
  884. }
  885. static inline int wait_cmv_ack(struct uea_softc *sc)
  886. {
  887. int ret = uea_wait(sc, sc->cmv_ack , ACK_TIMEOUT);
  888. sc->cmv_ack = 0;
  889. uea_dbg(INS_TO_USBDEV(sc), "wait_event_timeout : %d ms\n",
  890. jiffies_to_msecs(ret));
  891. if (ret < 0)
  892. return ret;
  893. return (ret == 0) ? -ETIMEDOUT : 0;
  894. }
  895. #define UCDC_SEND_ENCAPSULATED_COMMAND 0x00
  896. static int uea_request(struct uea_softc *sc,
  897. u16 value, u16 index, u16 size, const void *data)
  898. {
  899. u8 *xfer_buff;
  900. int ret = -ENOMEM;
  901. xfer_buff = kmemdup(data, size, GFP_KERNEL);
  902. if (!xfer_buff) {
  903. uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
  904. return ret;
  905. }
  906. ret = usb_control_msg(sc->usb_dev, usb_sndctrlpipe(sc->usb_dev, 0),
  907. UCDC_SEND_ENCAPSULATED_COMMAND,
  908. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  909. value, index, xfer_buff, size, CTRL_TIMEOUT);
  910. kfree(xfer_buff);
  911. if (ret < 0) {
  912. uea_err(INS_TO_USBDEV(sc), "usb_control_msg error %d\n", ret);
  913. return ret;
  914. }
  915. if (ret != size) {
  916. uea_err(INS_TO_USBDEV(sc),
  917. "usb_control_msg send only %d bytes (instead of %d)\n",
  918. ret, size);
  919. return -EIO;
  920. }
  921. return 0;
  922. }
  923. static int uea_cmv_e1(struct uea_softc *sc,
  924. u8 function, u32 address, u16 offset, u32 data)
  925. {
  926. struct cmv_e1 cmv;
  927. int ret;
  928. uea_enters(INS_TO_USBDEV(sc));
  929. uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Address : %c%c%c%c, "
  930. "offset : 0x%04x, data : 0x%08x\n",
  931. E1_FUNCTION_TYPE(function), E1_FUNCTION_SUBTYPE(function),
  932. E1_GETSA1(address), E1_GETSA2(address), E1_GETSA3(address),
  933. E1_GETSA4(address), offset, data);
  934. /* we send a request, but we expect a reply */
  935. sc->cmv_dsc.e1.function = function | 0x2;
  936. sc->cmv_dsc.e1.idx++;
  937. sc->cmv_dsc.e1.address = address;
  938. sc->cmv_dsc.e1.offset = offset;
  939. cmv.wPreamble = cpu_to_le16(E1_PREAMBLE);
  940. cmv.bDirection = E1_HOSTTOMODEM;
  941. cmv.bFunction = function;
  942. cmv.wIndex = cpu_to_le16(sc->cmv_dsc.e1.idx);
  943. put_unaligned_le32(address, &cmv.dwSymbolicAddress);
  944. cmv.wOffsetAddress = cpu_to_le16(offset);
  945. put_unaligned_le32(data >> 16 | data << 16, &cmv.dwData);
  946. ret = uea_request(sc, UEA_E1_SET_BLOCK, UEA_MPTX_START, sizeof(cmv), &cmv);
  947. if (ret < 0)
  948. return ret;
  949. ret = wait_cmv_ack(sc);
  950. uea_leaves(INS_TO_USBDEV(sc));
  951. return ret;
  952. }
  953. static int uea_cmv_e4(struct uea_softc *sc,
  954. u16 function, u16 group, u16 address, u16 offset, u32 data)
  955. {
  956. struct cmv_e4 cmv;
  957. int ret;
  958. uea_enters(INS_TO_USBDEV(sc));
  959. memset(&cmv, 0, sizeof(cmv));
  960. uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Group : 0x%04x, "
  961. "Address : 0x%04x, offset : 0x%04x, data : 0x%08x\n",
  962. E4_FUNCTION_TYPE(function), E4_FUNCTION_SUBTYPE(function),
  963. group, address, offset, data);
  964. /* we send a request, but we expect a reply */
  965. sc->cmv_dsc.e4.function = function | (0x1 << 4);
  966. sc->cmv_dsc.e4.offset = offset;
  967. sc->cmv_dsc.e4.address = address;
  968. sc->cmv_dsc.e4.group = group;
  969. cmv.wFunction = cpu_to_be16(function);
  970. cmv.wGroup = cpu_to_be16(group);
  971. cmv.wAddress = cpu_to_be16(address);
  972. cmv.wOffset = cpu_to_be16(offset);
  973. cmv.dwData[0] = cpu_to_be32(data);
  974. ret = uea_request(sc, UEA_E4_SET_BLOCK, UEA_MPTX_START, sizeof(cmv), &cmv);
  975. if (ret < 0)
  976. return ret;
  977. ret = wait_cmv_ack(sc);
  978. uea_leaves(INS_TO_USBDEV(sc));
  979. return ret;
  980. }
  981. static inline int uea_read_cmv_e1(struct uea_softc *sc,
  982. u32 address, u16 offset, u32 *data)
  983. {
  984. int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTREAD),
  985. address, offset, 0);
  986. if (ret < 0)
  987. uea_err(INS_TO_USBDEV(sc),
  988. "reading cmv failed with error %d\n", ret);
  989. else
  990. *data = sc->data;
  991. return ret;
  992. }
  993. static inline int uea_read_cmv_e4(struct uea_softc *sc,
  994. u8 size, u16 group, u16 address, u16 offset, u32 *data)
  995. {
  996. int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS, E4_REQUESTREAD, size),
  997. group, address, offset, 0);
  998. if (ret < 0)
  999. uea_err(INS_TO_USBDEV(sc),
  1000. "reading cmv failed with error %d\n", ret);
  1001. else {
  1002. *data = sc->data;
  1003. /* size is in 16-bit word quantities */
  1004. if (size > 2)
  1005. *(data + 1) = sc->data1;
  1006. }
  1007. return ret;
  1008. }
  1009. static inline int uea_write_cmv_e1(struct uea_softc *sc,
  1010. u32 address, u16 offset, u32 data)
  1011. {
  1012. int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTWRITE),
  1013. address, offset, data);
  1014. if (ret < 0)
  1015. uea_err(INS_TO_USBDEV(sc),
  1016. "writing cmv failed with error %d\n", ret);
  1017. return ret;
  1018. }
  1019. static inline int uea_write_cmv_e4(struct uea_softc *sc,
  1020. u8 size, u16 group, u16 address, u16 offset, u32 data)
  1021. {
  1022. int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS, E4_REQUESTWRITE, size),
  1023. group, address, offset, data);
  1024. if (ret < 0)
  1025. uea_err(INS_TO_USBDEV(sc),
  1026. "writing cmv failed with error %d\n", ret);
  1027. return ret;
  1028. }
  1029. static void uea_set_bulk_timeout(struct uea_softc *sc, u32 dsrate)
  1030. {
  1031. int ret;
  1032. u16 timeout;
  1033. /* in bulk mode the modem have problem with high rate
  1034. * changing internal timing could improve things, but the
  1035. * value is misterious.
  1036. * ADI930 don't support it (-EPIPE error).
  1037. */
  1038. if (UEA_CHIP_VERSION(sc) == ADI930 ||
  1039. altsetting[sc->modem_index] > 0 ||
  1040. sc->stats.phy.dsrate == dsrate)
  1041. return;
  1042. /* Original timming (1Mbit/s) from ADI (used in windows driver) */
  1043. timeout = (dsrate <= 1024*1024) ? 0 : 1;
  1044. ret = uea_request(sc, UEA_SET_TIMEOUT, timeout, 0, NULL);
  1045. uea_info(INS_TO_USBDEV(sc), "setting new timeout %d%s\n",
  1046. timeout, ret < 0 ? " failed" : "");
  1047. }
  1048. /*
  1049. * Monitor the modem and update the stat
  1050. * return 0 if everything is ok
  1051. * return < 0 if an error occurs (-EAGAIN reboot needed)
  1052. */
  1053. static int uea_stat_e1(struct uea_softc *sc)
  1054. {
  1055. u32 data;
  1056. int ret;
  1057. uea_enters(INS_TO_USBDEV(sc));
  1058. data = sc->stats.phy.state;
  1059. ret = uea_read_cmv_e1(sc, E1_SA_STAT, 0, &sc->stats.phy.state);
  1060. if (ret < 0)
  1061. return ret;
  1062. switch (GET_STATUS(sc->stats.phy.state)) {
  1063. case 0: /* not yet synchronized */
  1064. uea_dbg(INS_TO_USBDEV(sc),
  1065. "modem not yet synchronized\n");
  1066. return 0;
  1067. case 1: /* initialization */
  1068. uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
  1069. return 0;
  1070. case 2: /* operational */
  1071. uea_vdbg(INS_TO_USBDEV(sc), "modem operational\n");
  1072. break;
  1073. case 3: /* fail ... */
  1074. uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
  1075. " (may be try other cmv/dsp)\n");
  1076. return -EAGAIN;
  1077. case 4 ... 6: /* test state */
  1078. uea_warn(INS_TO_USBDEV(sc),
  1079. "modem in test mode - not supported\n");
  1080. return -EAGAIN;
  1081. case 7: /* fast-retain ... */
  1082. uea_info(INS_TO_USBDEV(sc), "modem in fast-retain mode\n");
  1083. return 0;
  1084. default:
  1085. uea_err(INS_TO_USBDEV(sc), "modem invalid SW mode %d\n",
  1086. GET_STATUS(sc->stats.phy.state));
  1087. return -EAGAIN;
  1088. }
  1089. if (GET_STATUS(data) != 2) {
  1090. uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
  1091. uea_info(INS_TO_USBDEV(sc), "modem operational\n");
  1092. /* release the dsp firmware as it is not needed until
  1093. * the next failure
  1094. */
  1095. if (sc->dsp_firm) {
  1096. release_firmware(sc->dsp_firm);
  1097. sc->dsp_firm = NULL;
  1098. }
  1099. }
  1100. /* always update it as atm layer could not be init when we switch to
  1101. * operational state
  1102. */
  1103. UPDATE_ATM_STAT(signal, ATM_PHY_SIG_FOUND);
  1104. /* wake up processes waiting for synchronization */
  1105. wake_up(&sc->sync_q);
  1106. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 2, &sc->stats.phy.flags);
  1107. if (ret < 0)
  1108. return ret;
  1109. sc->stats.phy.mflags |= sc->stats.phy.flags;
  1110. /* in case of a flags ( for example delineation LOSS (& 0x10)),
  1111. * we check the status again in order to detect the failure earlier
  1112. */
  1113. if (sc->stats.phy.flags) {
  1114. uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
  1115. sc->stats.phy.flags);
  1116. return 0;
  1117. }
  1118. ret = uea_read_cmv_e1(sc, E1_SA_RATE, 0, &data);
  1119. if (ret < 0)
  1120. return ret;
  1121. uea_set_bulk_timeout(sc, (data >> 16) * 32);
  1122. sc->stats.phy.dsrate = (data >> 16) * 32;
  1123. sc->stats.phy.usrate = (data & 0xffff) * 32;
  1124. UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
  1125. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 23, &data);
  1126. if (ret < 0)
  1127. return ret;
  1128. sc->stats.phy.dsattenuation = (data & 0xff) / 2;
  1129. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 47, &data);
  1130. if (ret < 0)
  1131. return ret;
  1132. sc->stats.phy.usattenuation = (data & 0xff) / 2;
  1133. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 25, &sc->stats.phy.dsmargin);
  1134. if (ret < 0)
  1135. return ret;
  1136. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 49, &sc->stats.phy.usmargin);
  1137. if (ret < 0)
  1138. return ret;
  1139. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 51, &sc->stats.phy.rxflow);
  1140. if (ret < 0)
  1141. return ret;
  1142. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 52, &sc->stats.phy.txflow);
  1143. if (ret < 0)
  1144. return ret;
  1145. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 54, &sc->stats.phy.dsunc);
  1146. if (ret < 0)
  1147. return ret;
  1148. /* only for atu-c */
  1149. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 58, &sc->stats.phy.usunc);
  1150. if (ret < 0)
  1151. return ret;
  1152. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 53, &sc->stats.phy.dscorr);
  1153. if (ret < 0)
  1154. return ret;
  1155. /* only for atu-c */
  1156. ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 57, &sc->stats.phy.uscorr);
  1157. if (ret < 0)
  1158. return ret;
  1159. ret = uea_read_cmv_e1(sc, E1_SA_INFO, 8, &sc->stats.phy.vidco);
  1160. if (ret < 0)
  1161. return ret;
  1162. ret = uea_read_cmv_e1(sc, E1_SA_INFO, 13, &sc->stats.phy.vidcpe);
  1163. if (ret < 0)
  1164. return ret;
  1165. return 0;
  1166. }
  1167. static int uea_stat_e4(struct uea_softc *sc)
  1168. {
  1169. u32 data;
  1170. u32 tmp_arr[2];
  1171. int ret;
  1172. uea_enters(INS_TO_USBDEV(sc));
  1173. data = sc->stats.phy.state;
  1174. /* XXX only need to be done before operationnal... */
  1175. ret = uea_read_cmv_e4(sc, 1, E4_SA_STAT, 0, 0, &sc->stats.phy.state);
  1176. if (ret < 0)
  1177. return ret;
  1178. switch (sc->stats.phy.state) {
  1179. case 0x0: /* not yet synchronized */
  1180. case 0x1:
  1181. case 0x3:
  1182. case 0x4:
  1183. uea_dbg(INS_TO_USBDEV(sc), "modem not yet synchronized\n");
  1184. return 0;
  1185. case 0x5: /* initialization */
  1186. case 0x6:
  1187. case 0x9:
  1188. case 0xa:
  1189. uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
  1190. return 0;
  1191. case 0x2: /* fail ... */
  1192. uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
  1193. " (may be try other cmv/dsp)\n");
  1194. return -EAGAIN;
  1195. case 0x7: /* operational */
  1196. break;
  1197. default:
  1198. uea_warn(INS_TO_USBDEV(sc), "unknown state: %x\n", sc->stats.phy.state);
  1199. return 0;
  1200. }
  1201. if (data != 7) {
  1202. uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
  1203. uea_info(INS_TO_USBDEV(sc), "modem operational\n");
  1204. /* release the dsp firmware as it is not needed until
  1205. * the next failure
  1206. */
  1207. if (sc->dsp_firm) {
  1208. release_firmware(sc->dsp_firm);
  1209. sc->dsp_firm = NULL;
  1210. }
  1211. }
  1212. /* always update it as atm layer could not be init when we switch to
  1213. * operational state
  1214. */
  1215. UPDATE_ATM_STAT(signal, ATM_PHY_SIG_FOUND);
  1216. /* wake up processes waiting for synchronization */
  1217. wake_up(&sc->sync_q);
  1218. /* TODO improve this state machine :
  1219. * we need some CMV info : what they do and their unit
  1220. * we should find the equivalent of eagle3- CMV
  1221. */
  1222. /* check flags */
  1223. ret = uea_read_cmv_e4(sc, 1, E4_SA_DIAG, 0, 0, &sc->stats.phy.flags);
  1224. if (ret < 0)
  1225. return ret;
  1226. sc->stats.phy.mflags |= sc->stats.phy.flags;
  1227. /* in case of a flags ( for example delineation LOSS (& 0x10)),
  1228. * we check the status again in order to detect the failure earlier
  1229. */
  1230. if (sc->stats.phy.flags) {
  1231. uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
  1232. sc->stats.phy.flags);
  1233. if (sc->stats.phy.flags & 1) //delineation LOSS
  1234. return -EAGAIN;
  1235. if (sc->stats.phy.flags & 0x4000) //Reset Flag
  1236. return -EAGAIN;
  1237. return 0;
  1238. }
  1239. /* rate data may be in upper or lower half of 64 bit word, strange */
  1240. ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 0, 0, tmp_arr);
  1241. if (ret < 0)
  1242. return ret;
  1243. data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
  1244. sc->stats.phy.usrate = data / 1000;
  1245. ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 1, 0, tmp_arr);
  1246. if (ret < 0)
  1247. return ret;
  1248. data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
  1249. uea_set_bulk_timeout(sc, data / 1000);
  1250. sc->stats.phy.dsrate = data / 1000;
  1251. UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
  1252. ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 1, &data);
  1253. if (ret < 0)
  1254. return ret;
  1255. sc->stats.phy.dsattenuation = data / 10;
  1256. ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 1, &data);
  1257. if (ret < 0)
  1258. return ret;
  1259. sc->stats.phy.usattenuation = data / 10;
  1260. ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 3, &data);
  1261. if (ret < 0)
  1262. return ret;
  1263. sc->stats.phy.dsmargin = data / 2;
  1264. ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 3, &data);
  1265. if (ret < 0)
  1266. return ret;
  1267. sc->stats.phy.usmargin = data / 10;
  1268. return 0;
  1269. }
  1270. static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
  1271. {
  1272. char file_arr[] = "CMVxy.bin";
  1273. char *file;
  1274. /* set proper name corresponding modem version and line type */
  1275. if (cmv_file[sc->modem_index] == NULL) {
  1276. if (UEA_CHIP_VERSION(sc) == ADI930)
  1277. file_arr[3] = '9';
  1278. else if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
  1279. file_arr[3] = '4';
  1280. else
  1281. file_arr[3] = 'e';
  1282. file_arr[4] = IS_ISDN(sc) ? 'i' : 'p';
  1283. file = file_arr;
  1284. } else
  1285. file = cmv_file[sc->modem_index];
  1286. strcpy(cmv_name, FW_DIR);
  1287. strlcat(cmv_name, file, UEA_FW_NAME_MAX);
  1288. if (ver == 2)
  1289. strlcat(cmv_name, ".v2", UEA_FW_NAME_MAX);
  1290. }
  1291. static int request_cmvs_old(struct uea_softc *sc,
  1292. void **cmvs, const struct firmware **fw)
  1293. {
  1294. int ret, size;
  1295. u8 *data;
  1296. char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
  1297. cmvs_file_name(sc, cmv_name, 1);
  1298. ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
  1299. if (ret < 0) {
  1300. uea_err(INS_TO_USBDEV(sc),
  1301. "requesting firmware %s failed with error %d\n",
  1302. cmv_name, ret);
  1303. return ret;
  1304. }
  1305. data = (u8 *) (*fw)->data;
  1306. size = (*fw)->size;
  1307. if (size < 1)
  1308. goto err_fw_corrupted;
  1309. if (size != *data * sizeof(struct uea_cmvs_v1) + 1)
  1310. goto err_fw_corrupted;
  1311. *cmvs = (void *)(data + 1);
  1312. return *data;
  1313. err_fw_corrupted:
  1314. uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
  1315. release_firmware(*fw);
  1316. return -EILSEQ;
  1317. }
  1318. static int request_cmvs(struct uea_softc *sc,
  1319. void **cmvs, const struct firmware **fw, int *ver)
  1320. {
  1321. int ret, size;
  1322. u32 crc;
  1323. u8 *data;
  1324. char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
  1325. cmvs_file_name(sc, cmv_name, 2);
  1326. ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
  1327. if (ret < 0) {
  1328. /* if caller can handle old version, try to provide it */
  1329. if (*ver == 1) {
  1330. uea_warn(INS_TO_USBDEV(sc), "requesting firmware %s failed, "
  1331. "try to get older cmvs\n", cmv_name);
  1332. return request_cmvs_old(sc, cmvs, fw);
  1333. }
  1334. uea_err(INS_TO_USBDEV(sc),
  1335. "requesting firmware %s failed with error %d\n",
  1336. cmv_name, ret);
  1337. return ret;
  1338. }
  1339. size = (*fw)->size;
  1340. data = (u8 *) (*fw)->data;
  1341. if (size < 4 || strncmp(data, "cmv2", 4) != 0) {
  1342. if (*ver == 1) {
  1343. uea_warn(INS_TO_USBDEV(sc), "firmware %s is corrupted, "
  1344. "try to get older cmvs\n", cmv_name);
  1345. release_firmware(*fw);
  1346. return request_cmvs_old(sc, cmvs, fw);
  1347. }
  1348. goto err_fw_corrupted;
  1349. }
  1350. *ver = 2;
  1351. data += 4;
  1352. size -= 4;
  1353. if (size < 5)
  1354. goto err_fw_corrupted;
  1355. crc = get_unaligned_le32(data);
  1356. data += 4;
  1357. size -= 4;
  1358. if (crc32_be(0, data, size) != crc)
  1359. goto err_fw_corrupted;
  1360. if (size != *data * sizeof(struct uea_cmvs_v2) + 1)
  1361. goto err_fw_corrupted;
  1362. *cmvs = (void *) (data + 1);
  1363. return *data;
  1364. err_fw_corrupted:
  1365. uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
  1366. release_firmware(*fw);
  1367. return -EILSEQ;
  1368. }
  1369. static int uea_send_cmvs_e1(struct uea_softc *sc)
  1370. {
  1371. int i, ret, len;
  1372. void *cmvs_ptr;
  1373. const struct firmware *cmvs_fw;
  1374. int ver = 1; // we can handle v1 cmv firmware version;
  1375. /* Enter in R-IDLE (cmv) until instructed otherwise */
  1376. ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 1);
  1377. if (ret < 0)
  1378. return ret;
  1379. /* Dump firmware version */
  1380. ret = uea_read_cmv_e1(sc, E1_SA_INFO, 10, &sc->stats.phy.firmid);
  1381. if (ret < 0)
  1382. return ret;
  1383. uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
  1384. sc->stats.phy.firmid);
  1385. /* get options */
  1386. ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
  1387. if (ret < 0)
  1388. return ret;
  1389. /* send options */
  1390. if (ver == 1) {
  1391. struct uea_cmvs_v1 *cmvs_v1 = cmvs_ptr;
  1392. uea_warn(INS_TO_USBDEV(sc), "use deprecated cmvs version, "
  1393. "please update your firmware\n");
  1394. for (i = 0; i < len; i++) {
  1395. ret = uea_write_cmv_e1(sc, get_unaligned_le32(&cmvs_v1[i].address),
  1396. get_unaligned_le16(&cmvs_v1[i].offset),
  1397. get_unaligned_le32(&cmvs_v1[i].data));
  1398. if (ret < 0)
  1399. goto out;
  1400. }
  1401. } else if (ver == 2) {
  1402. struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;
  1403. for (i = 0; i < len; i++) {
  1404. ret = uea_write_cmv_e1(sc, get_unaligned_le32(&cmvs_v2[i].address),
  1405. (u16) get_unaligned_le32(&cmvs_v2[i].offset),
  1406. get_unaligned_le32(&cmvs_v2[i].data));
  1407. if (ret < 0)
  1408. goto out;
  1409. }
  1410. } else {
  1411. /* This realy should not happen */
  1412. uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
  1413. goto out;
  1414. }
  1415. /* Enter in R-ACT-REQ */
  1416. ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 2);
  1417. uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
  1418. uea_info(INS_TO_USBDEV(sc), "modem started, waiting synchronization...\n");
  1419. out:
  1420. release_firmware(cmvs_fw);
  1421. return ret;
  1422. }
  1423. static int uea_send_cmvs_e4(struct uea_softc *sc)
  1424. {
  1425. int i, ret, len;
  1426. void *cmvs_ptr;
  1427. const struct firmware *cmvs_fw;
  1428. int ver = 2; // we can only handle v2 cmv firmware version;
  1429. /* Enter in R-IDLE (cmv) until instructed otherwise */
  1430. ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 1);
  1431. if (ret < 0)
  1432. return ret;
  1433. /* Dump firmware version */
  1434. /* XXX don't read the 3th byte as it is always 6 */
  1435. ret = uea_read_cmv_e4(sc, 2, E4_SA_INFO, 55, 0, &sc->stats.phy.firmid);
  1436. if (ret < 0)
  1437. return ret;
  1438. uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
  1439. sc->stats.phy.firmid);
  1440. /* get options */
  1441. ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
  1442. if (ret < 0)
  1443. return ret;
  1444. /* send options */
  1445. if (ver == 2) {
  1446. struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;
  1447. for (i = 0; i < len; i++) {
  1448. ret = uea_write_cmv_e4(sc, 1,
  1449. get_unaligned_le32(&cmvs_v2[i].group),
  1450. get_unaligned_le32(&cmvs_v2[i].address),
  1451. get_unaligned_le32(&cmvs_v2[i].offset),
  1452. get_unaligned_le32(&cmvs_v2[i].data));
  1453. if (ret < 0)
  1454. goto out;
  1455. }
  1456. } else {
  1457. /* This realy should not happen */
  1458. uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
  1459. goto out;
  1460. }
  1461. /* Enter in R-ACT-REQ */
  1462. ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 2);
  1463. uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
  1464. uea_info(INS_TO_USBDEV(sc), "modem started, waiting synchronization...\n");
  1465. out:
  1466. release_firmware(cmvs_fw);
  1467. return ret;
  1468. }
  1469. /* Start boot post firmware modem:
  1470. * - send reset commands through usb control pipe
  1471. * - start workqueue for DSP loading
  1472. * - send CMV options to modem
  1473. */
  1474. static int uea_start_reset(struct uea_softc *sc)
  1475. {
  1476. u16 zero = 0; /* ;-) */
  1477. int ret;
  1478. uea_enters(INS_TO_USBDEV(sc));
  1479. uea_info(INS_TO_USBDEV(sc), "(re)booting started\n");
  1480. /* mask interrupt */
  1481. sc->booting = 1;
  1482. /* We need to set this here because, a ack timeout could have occured,
  1483. * but before we start the reboot, the ack occurs and set this to 1.
  1484. * So we will failed to wait Ready CMV.
  1485. */
  1486. sc->cmv_ack = 0;
  1487. UPDATE_ATM_STAT(signal, ATM_PHY_SIG_LOST);
  1488. /* reset statistics */
  1489. memset(&sc->stats, 0, sizeof(struct uea_stats));
  1490. /* tell the modem that we want to boot in IDMA mode */
  1491. uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
  1492. uea_request(sc, UEA_SET_MODE, UEA_BOOT_IDMA, 0, NULL);
  1493. /* enter reset mode */
  1494. uea_request(sc, UEA_SET_MODE, UEA_START_RESET, 0, NULL);
  1495. /* original driver use 200ms, but windows driver use 100ms */
  1496. ret = uea_wait(sc, 0, msecs_to_jiffies(100));
  1497. if (ret < 0)
  1498. return ret;
  1499. /* leave reset mode */
  1500. uea_request(sc, UEA_SET_MODE, UEA_END_RESET, 0, NULL);
  1501. if (UEA_CHIP_VERSION(sc) != EAGLE_IV) {
  1502. /* clear tx and rx mailboxes */
  1503. uea_request(sc, UEA_SET_2183_DATA, UEA_MPTX_MAILBOX, 2, &zero);
  1504. uea_request(sc, UEA_SET_2183_DATA, UEA_MPRX_MAILBOX, 2, &zero);
  1505. uea_request(sc, UEA_SET_2183_DATA, UEA_SWAP_MAILBOX, 2, &zero);
  1506. }
  1507. ret = uea_wait(sc, 0, msecs_to_jiffies(1000));
  1508. if (ret < 0)
  1509. return ret;
  1510. if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
  1511. sc->cmv_dsc.e4.function = E4_MAKEFUNCTION(E4_ADSLDIRECTIVE, E4_MODEMREADY, 1);
  1512. else
  1513. sc->cmv_dsc.e1.function = E1_MAKEFUNCTION(E1_ADSLDIRECTIVE, E1_MODEMREADY);
  1514. /* demask interrupt */
  1515. sc->booting = 0;
  1516. /* start loading DSP */
  1517. sc->pageno = 0;
  1518. sc->ovl = 0;
  1519. queue_work(sc->work_q, &sc->task);
  1520. /* wait for modem ready CMV */
  1521. ret = wait_cmv_ack(sc);
  1522. if (ret < 0)
  1523. return ret;
  1524. uea_vdbg(INS_TO_USBDEV(sc), "Ready CMV received\n");
  1525. ret = sc->send_cmvs(sc);
  1526. if (ret < 0)
  1527. return ret;
  1528. sc->reset = 0;
  1529. uea_leaves(INS_TO_USBDEV(sc));
  1530. return ret;
  1531. }
  1532. /*
  1533. * In case of an error wait 1s before rebooting the modem
  1534. * if the modem don't request reboot (-EAGAIN).
  1535. * Monitor the modem every 1s.
  1536. */
  1537. static int uea_kthread(void *data)
  1538. {
  1539. struct uea_softc *sc = data;
  1540. int ret = -EAGAIN;
  1541. set_freezable();
  1542. uea_enters(INS_TO_USBDEV(sc));
  1543. while (!kthread_should_stop()) {
  1544. if (ret < 0 || sc->reset)
  1545. ret = uea_start_reset(sc);
  1546. if (!ret)
  1547. ret = sc->stat(sc);
  1548. if (ret != -EAGAIN)
  1549. uea_wait(sc, 0, msecs_to_jiffies(1000));
  1550. try_to_freeze();
  1551. }
  1552. uea_leaves(INS_TO_USBDEV(sc));
  1553. return ret;
  1554. }
  1555. /* Load second usb firmware for ADI930 chip */
  1556. static int load_XILINX_firmware(struct uea_softc *sc)
  1557. {
  1558. const struct firmware *fw_entry;
  1559. int ret, size, u, ln;
  1560. const u8 *pfw;
  1561. u8 value;
  1562. char *fw_name = FW_DIR "930-fpga.bin";
  1563. uea_enters(INS_TO_USBDEV(sc));
  1564. ret = request_firmware(&fw_entry, fw_name, &sc->usb_dev->dev);
  1565. if (ret) {
  1566. uea_err(INS_TO_USBDEV(sc), "firmware %s is not available\n",
  1567. fw_name);
  1568. goto err0;
  1569. }
  1570. pfw = fw_entry->data;
  1571. size = fw_entry->size;
  1572. if (size != 0x577B) {
  1573. uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
  1574. fw_name);
  1575. ret = -EILSEQ;
  1576. goto err1;
  1577. }
  1578. for (u = 0; u < size; u += ln) {
  1579. ln = min(size - u, 64);
  1580. ret = uea_request(sc, 0xe, 0, ln, pfw + u);
  1581. if (ret < 0) {
  1582. uea_err(INS_TO_USBDEV(sc),
  1583. "elsa download data failed (%d)\n", ret);
  1584. goto err1;
  1585. }
  1586. }
  1587. /* finish to send the fpga */
  1588. ret = uea_request(sc, 0xe, 1, 0, NULL);
  1589. if (ret < 0) {
  1590. uea_err(INS_TO_USBDEV(sc),
  1591. "elsa download data failed (%d)\n", ret);
  1592. goto err1;
  1593. }
  1594. /* Tell the modem we finish : de-assert reset */
  1595. value = 0;
  1596. ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
  1597. if (ret < 0)
  1598. uea_err(sc->usb_dev, "elsa de-assert failed with error %d\n", ret);
  1599. err1:
  1600. release_firmware(fw_entry);
  1601. err0:
  1602. uea_leaves(INS_TO_USBDEV(sc));
  1603. return ret;
  1604. }
  1605. /* The modem send us an ack. First with check if it right */
  1606. static void uea_dispatch_cmv_e1(struct uea_softc *sc, struct intr_pkt *intr)
  1607. {
  1608. struct cmv_dsc_e1 *dsc = &sc->cmv_dsc.e1;
  1609. struct cmv_e1 *cmv = &intr->u.e1.s2.cmv;
  1610. uea_enters(INS_TO_USBDEV(sc));
  1611. if (le16_to_cpu(cmv->wPreamble) != E1_PREAMBLE)
  1612. goto bad1;
  1613. if (cmv->bDirection != E1_MODEMTOHOST)
  1614. goto bad1;
  1615. /* FIXME : ADI930 reply wrong preambule (func = 2, sub = 2) to
  1616. * the first MEMACESS cmv. Ignore it...
  1617. */
  1618. if (cmv->bFunction != dsc->function) {
  1619. if (UEA_CHIP_VERSION(sc) == ADI930
  1620. && cmv->bFunction == E1_MAKEFUNCTION(2, 2)) {
  1621. cmv->wIndex = cpu_to_le16(dsc->idx);
  1622. put_unaligned_le32(dsc->address, &cmv->dwSymbolicAddress);
  1623. cmv->wOffsetAddress = cpu_to_le16(dsc->offset);
  1624. } else
  1625. goto bad2;
  1626. }
  1627. if (cmv->bFunction == E1_MAKEFUNCTION(E1_ADSLDIRECTIVE, E1_MODEMREADY)) {
  1628. wake_up_cmv_ack(sc);
  1629. uea_leaves(INS_TO_USBDEV(sc));
  1630. return;
  1631. }
  1632. /* in case of MEMACCESS */
  1633. if (le16_to_cpu(cmv->wIndex) != dsc->idx ||
  1634. get_unaligned_le32(&cmv->dwSymbolicAddress) != dsc->address ||
  1635. le16_to_cpu(cmv->wOffsetAddress) != dsc->offset)
  1636. goto bad2;
  1637. sc->data = get_unaligned_le32(&cmv->dwData);
  1638. sc->data = sc->data << 16 | sc->data >> 16;
  1639. wake_up_cmv_ack(sc);
  1640. uea_leaves(INS_TO_USBDEV(sc));
  1641. return;
  1642. bad2:
  1643. uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
  1644. "Function : %d, Subfunction : %d\n",
  1645. E1_FUNCTION_TYPE(cmv->bFunction),
  1646. E1_FUNCTION_SUBTYPE(cmv->bFunction));
  1647. uea_leaves(INS_TO_USBDEV(sc));
  1648. return;
  1649. bad1:
  1650. uea_err(INS_TO_USBDEV(sc), "invalid cmv received, "
  1651. "wPreamble %d, bDirection %d\n",
  1652. le16_to_cpu(cmv->wPreamble), cmv->bDirection);
  1653. uea_leaves(INS_TO_USBDEV(sc));
  1654. }
  1655. /* The modem send us an ack. First with check if it right */
  1656. static void uea_dispatch_cmv_e4(struct uea_softc *sc, struct intr_pkt *intr)
  1657. {
  1658. struct cmv_dsc_e4 *dsc = &sc->cmv_dsc.e4;
  1659. struct cmv_e4 *cmv = &intr->u.e4.s2.cmv;
  1660. uea_enters(INS_TO_USBDEV(sc));
  1661. uea_dbg(INS_TO_USBDEV(sc), "cmv %x %x %x %x %x %x\n",
  1662. be16_to_cpu(cmv->wGroup), be16_to_cpu(cmv->wFunction),
  1663. be16_to_cpu(cmv->wOffset), be16_to_cpu(cmv->wAddress),
  1664. be32_to_cpu(cmv->dwData[0]), be32_to_cpu(cmv->dwData[1]));
  1665. if (be16_to_cpu(cmv->wFunction) != dsc->function)
  1666. goto bad2;
  1667. if (be16_to_cpu(cmv->wFunction) == E4_MAKEFUNCTION(E4_ADSLDIRECTIVE, E4_MODEMREADY, 1)) {
  1668. wake_up_cmv_ack(sc);
  1669. uea_leaves(INS_TO_USBDEV(sc));
  1670. return;
  1671. }
  1672. /* in case of MEMACCESS */
  1673. if (be16_to_cpu(cmv->wOffset) != dsc->offset ||
  1674. be16_to_cpu(cmv->wGroup) != dsc->group ||
  1675. be16_to_cpu(cmv->wAddress) != dsc->address)
  1676. goto bad2;
  1677. sc->data = be32_to_cpu(cmv->dwData[0]);
  1678. sc->data1 = be32_to_cpu(cmv->dwData[1]);
  1679. wake_up_cmv_ack(sc);
  1680. uea_leaves(INS_TO_USBDEV(sc));
  1681. return;
  1682. bad2:
  1683. uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
  1684. "Function : %d, Subfunction : %d\n",
  1685. E4_FUNCTION_TYPE(cmv->wFunction),
  1686. E4_FUNCTION_SUBTYPE(cmv->wFunction));
  1687. uea_leaves(INS_TO_USBDEV(sc));
  1688. return;
  1689. }
  1690. static void uea_schedule_load_page_e1(struct uea_softc *sc, struct intr_pkt *intr)
  1691. {
  1692. sc->pageno = intr->e1_bSwapPageNo;
  1693. sc->ovl = intr->e1_bOvl >> 4 | intr->e1_bOvl << 4;
  1694. queue_work(sc->work_q, &sc->task);
  1695. }
  1696. static void uea_schedule_load_page_e4(struct uea_softc *sc, struct intr_pkt *intr)
  1697. {
  1698. sc->pageno = intr->e4_bSwapPageNo;
  1699. queue_work(sc->work_q, &sc->task);
  1700. }
  1701. /*
  1702. * interrupt handler
  1703. */
  1704. static void uea_intr(struct urb *urb)
  1705. {
  1706. struct uea_softc *sc = urb->context;
  1707. struct intr_pkt *intr = urb->transfer_buffer;
  1708. int status = urb->status;
  1709. uea_enters(INS_TO_USBDEV(sc));
  1710. if (unlikely(status < 0)) {
  1711. uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
  1712. status);
  1713. return;
  1714. }
  1715. /* device-to-host interrupt */
  1716. if (intr->bType != 0x08 || sc->booting) {
  1717. uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
  1718. goto resubmit;
  1719. }
  1720. switch (le16_to_cpu(intr->wInterrupt)) {
  1721. case INT_LOADSWAPPAGE:
  1722. sc->schedule_load_page(sc, intr);
  1723. break;
  1724. case INT_INCOMINGCMV:
  1725. sc->dispatch_cmv(sc, intr);
  1726. break;
  1727. default:
  1728. uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
  1729. le16_to_cpu(intr->wInterrupt));
  1730. }
  1731. resubmit:
  1732. usb_submit_urb(sc->urb_int, GFP_ATOMIC);
  1733. }
  1734. /*
  1735. * Start the modem : init the data and start kernel thread
  1736. */
  1737. static int uea_boot(struct uea_softc *sc)
  1738. {
  1739. int ret, size;
  1740. struct intr_pkt *intr;
  1741. uea_enters(INS_TO_USBDEV(sc));
  1742. if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
  1743. size = E4_INTR_PKT_SIZE;
  1744. sc->dispatch_cmv = uea_dispatch_cmv_e4;
  1745. sc->schedule_load_page = uea_schedule_load_page_e4;
  1746. sc->stat = uea_stat_e4;
  1747. sc->send_cmvs = uea_send_cmvs_e4;
  1748. INIT_WORK(&sc->task, uea_load_page_e4);
  1749. } else {
  1750. size = E1_INTR_PKT_SIZE;
  1751. sc->dispatch_cmv = uea_dispatch_cmv_e1;
  1752. sc->schedule_load_page = uea_schedule_load_page_e1;
  1753. sc->stat = uea_stat_e1;
  1754. sc->send_cmvs = uea_send_cmvs_e1;
  1755. INIT_WORK(&sc->task, uea_load_page_e1);
  1756. }
  1757. init_waitqueue_head(&sc->sync_q);
  1758. sc->work_q = create_workqueue("ueagle-dsp");
  1759. if (!sc->work_q) {
  1760. uea_err(INS_TO_USBDEV(sc), "cannot allocate workqueue\n");
  1761. uea_leaves(INS_TO_USBDEV(sc));
  1762. return -ENOMEM;
  1763. }
  1764. if (UEA_CHIP_VERSION(sc) == ADI930)
  1765. load_XILINX_firmware(sc);
  1766. intr = kmalloc(size, GFP_KERNEL);
  1767. if (!intr) {
  1768. uea_err(INS_TO_USBDEV(sc),
  1769. "cannot allocate interrupt package\n");
  1770. goto err0;
  1771. }
  1772. sc->urb_int = usb_alloc_urb(0, GFP_KERNEL);
  1773. if (!sc->urb_int) {
  1774. uea_err(INS_TO_USBDEV(sc), "cannot allocate interrupt URB\n");
  1775. goto err1;
  1776. }
  1777. usb_fill_int_urb(sc->urb_int, sc->usb_dev,
  1778. usb_rcvintpipe(sc->usb_dev, UEA_INTR_PIPE),
  1779. intr, size, uea_intr, sc,
  1780. sc->usb_dev->actconfig->interface[0]->altsetting[0].
  1781. endpoint[0].desc.bInterval);
  1782. ret = usb_submit_urb(sc->urb_int, GFP_KERNEL);
  1783. if (ret < 0) {
  1784. uea_err(INS_TO_USBDEV(sc),
  1785. "urb submition failed with error %d\n", ret);
  1786. goto err1;
  1787. }
  1788. sc->kthread = kthread_run(uea_kthread, sc, "ueagle-atm");
  1789. if (sc->kthread == ERR_PTR(-ENOMEM)) {
  1790. uea_err(INS_TO_USBDEV(sc), "failed to create thread\n");
  1791. goto err2;
  1792. }
  1793. uea_leaves(INS_TO_USBDEV(sc));
  1794. return 0;
  1795. err2:
  1796. usb_kill_urb(sc->urb_int);
  1797. err1:
  1798. usb_free_urb(sc->urb_int);
  1799. sc->urb_int = NULL;
  1800. kfree(intr);
  1801. err0:
  1802. destroy_workqueue(sc->work_q);
  1803. uea_leaves(INS_TO_USBDEV(sc));
  1804. return -ENOMEM;
  1805. }
  1806. /*
  1807. * Stop the modem : kill kernel thread and free data
  1808. */
  1809. static void uea_stop(struct uea_softc *sc)
  1810. {
  1811. int ret;
  1812. uea_enters(INS_TO_USBDEV(sc));
  1813. ret = kthread_stop(sc->kthread);
  1814. uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
  1815. uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
  1816. usb_kill_urb(sc->urb_int);
  1817. kfree(sc->urb_int->transfer_buffer);
  1818. usb_free_urb(sc->urb_int);
  1819. /* stop any pending boot process, when no one can schedule work */
  1820. destroy_workqueue(sc->work_q);
  1821. if (sc->dsp_firm)
  1822. release_firmware(sc->dsp_firm);
  1823. uea_leaves(INS_TO_USBDEV(sc));
  1824. }
  1825. /* syfs interface */
  1826. static struct uea_softc *dev_to_uea(struct device *dev)
  1827. {
  1828. struct usb_interface *intf;
  1829. struct usbatm_data *usbatm;
  1830. intf = to_usb_interface(dev);
  1831. if (!intf)
  1832. return NULL;
  1833. usbatm = usb_get_intfdata(intf);
  1834. if (!usbatm)
  1835. return NULL;
  1836. return usbatm->driver_data;
  1837. }
  1838. static ssize_t read_status(struct device *dev, struct device_attribute *attr,
  1839. char *buf)
  1840. {
  1841. int ret = -ENODEV;
  1842. struct uea_softc *sc;
  1843. mutex_lock(&uea_mutex);
  1844. sc = dev_to_uea(dev);
  1845. if (!sc)
  1846. goto out;
  1847. ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state);
  1848. out:
  1849. mutex_unlock(&uea_mutex);
  1850. return ret;
  1851. }
  1852. static ssize_t reboot(struct device *dev, struct device_attribute *attr,
  1853. const char *buf, size_t count)
  1854. {
  1855. int ret = -ENODEV;
  1856. struct uea_softc *sc;
  1857. mutex_lock(&uea_mutex);
  1858. sc = dev_to_uea(dev);
  1859. if (!sc)
  1860. goto out;
  1861. sc->reset = 1;
  1862. ret = count;
  1863. out:
  1864. mutex_unlock(&uea_mutex);
  1865. return ret;
  1866. }
  1867. static DEVICE_ATTR(stat_status, S_IWUGO | S_IRUGO, read_status, reboot);
  1868. static ssize_t read_human_status(struct device *dev, struct device_attribute *attr,
  1869. char *buf)
  1870. {
  1871. int ret = -ENODEV;
  1872. int modem_state;
  1873. struct uea_softc *sc;
  1874. mutex_lock(&uea_mutex);
  1875. sc = dev_to_uea(dev);
  1876. if (!sc)
  1877. goto out;
  1878. if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
  1879. switch (sc->stats.phy.state) {
  1880. case 0x0: /* not yet synchronized */
  1881. case 0x1:
  1882. case 0x3:
  1883. case 0x4:
  1884. modem_state = 0;
  1885. break;
  1886. case 0x5: /* initialization */
  1887. case 0x6:
  1888. case 0x9:
  1889. case 0xa:
  1890. modem_state = 1;
  1891. break;
  1892. case 0x7: /* operational */
  1893. modem_state = 2;
  1894. break;
  1895. case 0x2: /* fail ... */
  1896. modem_state = 3;
  1897. break;
  1898. default: /* unknown */
  1899. modem_state = 4;
  1900. break;
  1901. }
  1902. } else
  1903. modem_state = GET_STATUS(sc->stats.phy.state);
  1904. switch (modem_state) {
  1905. case 0:
  1906. ret = sprintf(buf, "Modem is booting\n");
  1907. break;
  1908. case 1:
  1909. ret = sprintf(buf, "Modem is initializing\n");
  1910. break;
  1911. case 2:
  1912. ret = sprintf(buf, "Modem is operational\n");
  1913. break;
  1914. case 3:
  1915. ret = sprintf(buf, "Modem synchronization failed\n");
  1916. break;
  1917. default:
  1918. ret = sprintf(buf, "Modem state is unknown\n");
  1919. break;
  1920. }
  1921. out:
  1922. mutex_unlock(&uea_mutex);
  1923. return ret;
  1924. }
  1925. static DEVICE_ATTR(stat_human_status, S_IWUGO | S_IRUGO, read_human_status, NULL);
  1926. static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
  1927. char *buf)
  1928. {
  1929. int ret = -ENODEV;
  1930. struct uea_softc *sc;
  1931. char *delin = "GOOD";
  1932. mutex_lock(&uea_mutex);
  1933. sc = dev_to_uea(dev);
  1934. if (!sc)
  1935. goto out;
  1936. if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
  1937. if (sc->stats.phy.flags & 0x4000)
  1938. delin = "RESET";
  1939. else if (sc->stats.phy.flags & 0x0001)
  1940. delin = "LOSS";
  1941. } else {
  1942. if (sc->stats.phy.flags & 0x0C00)
  1943. delin = "ERROR";
  1944. else if (sc->stats.phy.flags & 0x0030)
  1945. delin = "LOSS";
  1946. }
  1947. ret = sprintf(buf, "%s\n", delin);
  1948. out:
  1949. mutex_unlock(&uea_mutex);
  1950. return ret;
  1951. }
  1952. static DEVICE_ATTR(stat_delin, S_IWUGO | S_IRUGO, read_delin, NULL);
  1953. #define UEA_ATTR(name, reset) \
  1954. \
  1955. static ssize_t read_##name(struct device *dev, \
  1956. struct device_attribute *attr, char *buf) \
  1957. { \
  1958. int ret = -ENODEV; \
  1959. struct uea_softc *sc; \
  1960. \
  1961. mutex_lock(&uea_mutex); \
  1962. sc = dev_to_uea(dev); \
  1963. if (!sc) \
  1964. goto out; \
  1965. ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.name); \
  1966. if (reset) \
  1967. sc->stats.phy.name = 0; \
  1968. out: \
  1969. mutex_unlock(&uea_mutex); \
  1970. return ret; \
  1971. } \
  1972. \
  1973. static DEVICE_ATTR(stat_##name, S_IRUGO, read_##name, NULL)
  1974. UEA_ATTR(mflags, 1);
  1975. UEA_ATTR(vidcpe, 0);
  1976. UEA_ATTR(usrate, 0);
  1977. UEA_ATTR(dsrate, 0);
  1978. UEA_ATTR(usattenuation, 0);
  1979. UEA_ATTR(dsattenuation, 0);
  1980. UEA_ATTR(usmargin, 0);
  1981. UEA_ATTR(dsmargin, 0);
  1982. UEA_ATTR(txflow, 0);
  1983. UEA_ATTR(rxflow, 0);
  1984. UEA_ATTR(uscorr, 0);
  1985. UEA_ATTR(dscorr, 0);
  1986. UEA_ATTR(usunc, 0);
  1987. UEA_ATTR(dsunc, 0);
  1988. UEA_ATTR(firmid, 0);
  1989. /* Retrieve the device End System Identifier (MAC) */
  1990. #define htoi(x) (isdigit(x) ? x-'0' : toupper(x)-'A'+10)
  1991. static int uea_getesi(struct uea_softc *sc, u_char * esi)
  1992. {
  1993. unsigned char mac_str[2 * ETH_ALEN + 1];
  1994. int i;
  1995. if (usb_string
  1996. (sc->usb_dev, sc->usb_dev->descriptor.iSerialNumber, mac_str,
  1997. sizeof(mac_str)) != 2 * ETH_ALEN)
  1998. return 1;
  1999. for (i = 0; i < ETH_ALEN; i++)
  2000. esi[i] = htoi(mac_str[2 * i]) * 16 + htoi(mac_str[2 * i + 1]);
  2001. return 0;
  2002. }
  2003. /* ATM stuff */
  2004. static int uea_atm_open(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
  2005. {
  2006. struct uea_softc *sc = usbatm->driver_data;
  2007. return uea_getesi(sc, atm_dev->esi);
  2008. }
  2009. static int uea_heavy(struct usbatm_data *usbatm, struct usb_interface *intf)
  2010. {
  2011. struct uea_softc *sc = usbatm->driver_data;
  2012. wait_event_interruptible(sc->sync_q, IS_OPERATIONAL(sc));
  2013. return 0;
  2014. }
  2015. static int claim_interface(struct usb_device *usb_dev,
  2016. struct usbatm_data *usbatm, int ifnum)
  2017. {
  2018. int ret;
  2019. struct usb_interface *intf = usb_ifnum_to_if(usb_dev, ifnum);
  2020. if (!intf) {
  2021. uea_err(usb_dev, "interface %d not found\n", ifnum);
  2022. return -ENODEV;
  2023. }
  2024. ret = usb_driver_claim_interface(&uea_driver, intf, usbatm);
  2025. if (ret != 0)
  2026. uea_err(usb_dev, "can't claim interface %d, error %d\n", ifnum,
  2027. ret);
  2028. return ret;
  2029. }
  2030. static struct attribute *attrs[] = {
  2031. &dev_attr_stat_status.attr,
  2032. &dev_attr_stat_mflags.attr,
  2033. &dev_attr_stat_human_status.attr,
  2034. &dev_attr_stat_delin.attr,
  2035. &dev_attr_stat_vidcpe.attr,
  2036. &dev_attr_stat_usrate.attr,
  2037. &dev_attr_stat_dsrate.attr,
  2038. &dev_attr_stat_usattenuation.attr,
  2039. &dev_attr_stat_dsattenuation.attr,
  2040. &dev_attr_stat_usmargin.attr,
  2041. &dev_attr_stat_dsmargin.attr,
  2042. &dev_attr_stat_txflow.attr,
  2043. &dev_attr_stat_rxflow.attr,
  2044. &dev_attr_stat_uscorr.attr,
  2045. &dev_attr_stat_dscorr.attr,
  2046. &dev_attr_stat_usunc.attr,
  2047. &dev_attr_stat_dsunc.attr,
  2048. &dev_attr_stat_firmid.attr,
  2049. NULL,
  2050. };
  2051. static struct attribute_group attr_grp = {
  2052. .attrs = attrs,
  2053. };
  2054. static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
  2055. const struct usb_device_id *id)
  2056. {
  2057. struct usb_device *usb = interface_to_usbdev(intf);
  2058. struct uea_softc *sc;
  2059. int ret, ifnum = intf->altsetting->desc.bInterfaceNumber;
  2060. unsigned int alt;
  2061. uea_enters(usb);
  2062. /* interface 0 is for firmware/monitoring */
  2063. if (ifnum != UEA_INTR_IFACE_NO)
  2064. return -ENODEV;
  2065. usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
  2066. /* interface 1 is for outbound traffic */
  2067. ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
  2068. if (ret < 0)
  2069. return ret;
  2070. /* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
  2071. if (UEA_CHIP_VERSION(id) != ADI930) {
  2072. /* interface 2 is for inbound traffic */
  2073. ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
  2074. if (ret < 0)
  2075. return ret;
  2076. }
  2077. sc = kzalloc(sizeof(struct uea_softc), GFP_KERNEL);
  2078. if (!sc) {
  2079. uea_err(usb, "uea_init: not enough memory !\n");
  2080. return -ENOMEM;
  2081. }
  2082. sc->usb_dev = usb;
  2083. usbatm->driver_data = sc;
  2084. sc->usbatm = usbatm;
  2085. sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
  2086. sc->driver_info = id->driver_info;
  2087. /* first try to use module parameter */
  2088. if (annex[sc->modem_index] == 1)
  2089. sc->annex = ANNEXA;
  2090. else if (annex[sc->modem_index] == 2)
  2091. sc->annex = ANNEXB;
  2092. /* try to autodetect annex */
  2093. else if (sc->driver_info & AUTO_ANNEX_A)
  2094. sc->annex = ANNEXA;
  2095. else if (sc->driver_info & AUTO_ANNEX_B)
  2096. sc->annex = ANNEXB;
  2097. else
  2098. sc->annex = (le16_to_cpu(sc->usb_dev->descriptor.bcdDevice) & 0x80)?ANNEXB:ANNEXA;
  2099. alt = altsetting[sc->modem_index];
  2100. /* ADI930 don't support iso */
  2101. if (UEA_CHIP_VERSION(id) != ADI930 && alt > 0) {
  2102. if (alt <= 8 && usb_set_interface(usb, UEA_DS_IFACE_NO, alt) == 0) {
  2103. uea_dbg(usb, "set alternate %u for 2 interface\n", alt);
  2104. uea_info(usb, "using iso mode\n");
  2105. usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
  2106. } else {
  2107. uea_err(usb, "setting alternate %u failed for "
  2108. "2 interface, using bulk mode\n", alt);
  2109. }
  2110. }
  2111. ret = sysfs_create_group(&intf->dev.kobj, &attr_grp);
  2112. if (ret < 0)
  2113. goto error;
  2114. ret = uea_boot(sc);
  2115. if (ret < 0)
  2116. goto error_rm_grp;
  2117. return 0;
  2118. error_rm_grp:
  2119. sysfs_remove_group(&intf->dev.kobj, &attr_grp);
  2120. error:
  2121. kfree(sc);
  2122. return ret;
  2123. }
  2124. static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
  2125. {
  2126. struct uea_softc *sc = usbatm->driver_data;
  2127. sysfs_remove_group(&intf->dev.kobj, &attr_grp);
  2128. uea_stop(sc);
  2129. kfree(sc);
  2130. }
  2131. static struct usbatm_driver uea_usbatm_driver = {
  2132. .driver_name = "ueagle-atm",
  2133. .bind = uea_bind,
  2134. .atm_start = uea_atm_open,
  2135. .unbind = uea_unbind,
  2136. .heavy_init = uea_heavy,
  2137. .bulk_in = UEA_BULK_DATA_PIPE,
  2138. .bulk_out = UEA_BULK_DATA_PIPE,
  2139. .isoc_in = UEA_ISO_DATA_PIPE,
  2140. };
  2141. static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
  2142. {
  2143. struct usb_device *usb = interface_to_usbdev(intf);
  2144. uea_enters(usb);
  2145. uea_info(usb, "ADSL device founded vid (%#X) pid (%#X) Rev (%#X): %s\n",
  2146. le16_to_cpu(usb->descriptor.idVendor),
  2147. le16_to_cpu(usb->descriptor.idProduct),
  2148. le16_to_cpu(usb->descriptor.bcdDevice),
  2149. chip_name[UEA_CHIP_VERSION(id)]);
  2150. usb_reset_device(usb);
  2151. if (UEA_IS_PREFIRM(id))
  2152. return uea_load_firmware(usb, UEA_CHIP_VERSION(id));
  2153. return usbatm_usb_probe(intf, id, &uea_usbatm_driver);
  2154. }
  2155. static void uea_disconnect(struct usb_interface *intf)
  2156. {
  2157. struct usb_device *usb = interface_to_usbdev(intf);
  2158. int ifnum = intf->altsetting->desc.bInterfaceNumber;
  2159. uea_enters(usb);
  2160. /* ADI930 has 2 interfaces and eagle 3 interfaces.
  2161. * Pre-firmware device has one interface
  2162. */
  2163. if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
  2164. mutex_lock(&uea_mutex);
  2165. usbatm_usb_disconnect(intf);
  2166. mutex_unlock(&uea_mutex);
  2167. uea_info(usb, "ADSL device removed\n");
  2168. }
  2169. uea_leaves(usb);
  2170. }
  2171. /*
  2172. * List of supported VID/PID
  2173. */
  2174. static const struct usb_device_id uea_ids[] = {
  2175. {USB_DEVICE(ANALOG_VID, ADI930_PID_PREFIRM), .driver_info = ADI930 | PREFIRM},
  2176. {USB_DEVICE(ANALOG_VID, ADI930_PID_PSTFIRM), .driver_info = ADI930 | PSTFIRM},
  2177. {USB_DEVICE(ANALOG_VID, EAGLE_I_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
  2178. {USB_DEVICE(ANALOG_VID, EAGLE_I_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM},
  2179. {USB_DEVICE(ANALOG_VID, EAGLE_II_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
  2180. {USB_DEVICE(ANALOG_VID, EAGLE_II_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
  2181. {USB_DEVICE(ANALOG_VID, EAGLE_IIC_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
  2182. {USB_DEVICE(ANALOG_VID, EAGLE_IIC_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
  2183. {USB_DEVICE(ANALOG_VID, EAGLE_III_PID_PREFIRM), .driver_info = EAGLE_III | PREFIRM},
  2184. {USB_DEVICE(ANALOG_VID, EAGLE_III_PID_PSTFIRM), .driver_info = EAGLE_III | PSTFIRM},
  2185. {USB_DEVICE(ANALOG_VID, EAGLE_IV_PID_PREFIRM), .driver_info = EAGLE_IV | PREFIRM},
  2186. {USB_DEVICE(ANALOG_VID, EAGLE_IV_PID_PSTFIRM), .driver_info = EAGLE_IV | PSTFIRM},
  2187. {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_A_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
  2188. {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_A_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
  2189. {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_B_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
  2190. {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_B_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
  2191. {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_A_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
  2192. {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_A_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_A},
  2193. {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_B_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
  2194. {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_B_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_B},
  2195. {USB_DEVICE(ELSA_VID, ELSA_PID_PREFIRM), .driver_info = ADI930 | PREFIRM},
  2196. {USB_DEVICE(ELSA_VID, ELSA_PID_PSTFIRM), .driver_info = ADI930 | PSTFIRM},
  2197. {USB_DEVICE(ELSA_VID, ELSA_PID_A_PREFIRM), .driver_info = ADI930 | PREFIRM},
  2198. {USB_DEVICE(ELSA_VID, ELSA_PID_A_PSTFIRM), .driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_A},
  2199. {USB_DEVICE(ELSA_VID, ELSA_PID_B_PREFIRM), .driver_info = ADI930 | PREFIRM},
  2200. {USB_DEVICE(ELSA_VID, ELSA_PID_B_PSTFIRM), .driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_B},
  2201. {USB_DEVICE(USR_VID, MILLER_A_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
  2202. {USB_DEVICE(USR_VID, MILLER_A_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
  2203. {USB_DEVICE(USR_VID, MILLER_B_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
  2204. {USB_DEVICE(USR_VID, MILLER_B_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
  2205. {USB_DEVICE(USR_VID, HEINEKEN_A_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
  2206. {USB_DEVICE(USR_VID, HEINEKEN_A_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
  2207. {USB_DEVICE(USR_VID, HEINEKEN_B_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
  2208. {USB_DEVICE(USR_VID, HEINEKEN_B_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
  2209. {}
  2210. };
  2211. /*
  2212. * USB driver descriptor
  2213. */
  2214. static struct usb_driver uea_driver = {
  2215. .name = "ueagle-atm",
  2216. .id_table = uea_ids,
  2217. .probe = uea_probe,
  2218. .disconnect = uea_disconnect,
  2219. };
  2220. MODULE_DEVICE_TABLE(usb, uea_ids);
  2221. /**
  2222. * uea_init - Initialize the module.
  2223. * Register to USB subsystem
  2224. */
  2225. static int __init uea_init(void)
  2226. {
  2227. printk(KERN_INFO "[ueagle-atm] driver " EAGLEUSBVERSION " loaded\n");
  2228. usb_register(&uea_driver);
  2229. return 0;
  2230. }
  2231. module_init(uea_init);
  2232. /**
  2233. * uea_exit - Destroy module
  2234. * Deregister with USB subsystem
  2235. */
  2236. static void __exit uea_exit(void)
  2237. {
  2238. /*
  2239. * This calls automatically the uea_disconnect method if necessary:
  2240. */
  2241. usb_deregister(&uea_driver);
  2242. printk(KERN_INFO "[ueagle-atm] driver unloaded\n");
  2243. }
  2244. module_exit(uea_exit);
  2245. MODULE_AUTHOR("Damien Bergamini/Matthieu Castet/Stanislaw W. Gruszka");
  2246. MODULE_DESCRIPTION("ADI 930/Eagle USB ADSL Modem driver");
  2247. MODULE_LICENSE("Dual BSD/GPL");