ueagle-atm.c 67 KB

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