ueagle-atm.c 67 KB

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