ueagle-atm.c 67 KB

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