ueagle-atm.c 68 KB

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