ueagle-atm.c 67 KB

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