ueagle-atm.c 67 KB

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