ueagle-atm.c 67 KB

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