ueagle-atm.c 67 KB

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