ueagle-atm.c 67 KB

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