ueagle-atm.c 68 KB

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