ueagle-atm.c 67 KB

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