hfcsusb.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  1. /* hfcsusb.c
  2. * mISDN driver for Colognechip HFC-S USB chip
  3. *
  4. * Copyright 2001 by Peter Sprenger (sprenger@moving-bytes.de)
  5. * Copyright 2008 by Martin Bachem (info@bachem-it.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. *
  22. * module params
  23. * debug=<n>, default=0, with n=0xHHHHGGGG
  24. * H - l1 driver flags described in hfcsusb.h
  25. * G - common mISDN debug flags described at mISDNhw.h
  26. *
  27. * poll=<n>, default 128
  28. * n : burst size of PH_DATA_IND at transparent rx data
  29. *
  30. */
  31. #include <linux/module.h>
  32. #include <linux/delay.h>
  33. #include <linux/usb.h>
  34. #include <linux/mISDNhw.h>
  35. #include <linux/slab.h>
  36. #include "hfcsusb.h"
  37. static const char *hfcsusb_rev = "Revision: 0.3.3 (socket), 2008-11-05";
  38. static unsigned int debug;
  39. static int poll = DEFAULT_TRANSP_BURST_SZ;
  40. static LIST_HEAD(HFClist);
  41. static DEFINE_RWLOCK(HFClock);
  42. MODULE_AUTHOR("Martin Bachem");
  43. MODULE_LICENSE("GPL");
  44. module_param(debug, uint, S_IRUGO | S_IWUSR);
  45. module_param(poll, int, 0);
  46. static int hfcsusb_cnt;
  47. /* some function prototypes */
  48. static void hfcsusb_ph_command(struct hfcsusb *hw, u_char command);
  49. static void release_hw(struct hfcsusb *hw);
  50. static void reset_hfcsusb(struct hfcsusb *hw);
  51. static void setPortMode(struct hfcsusb *hw);
  52. static void hfcsusb_start_endpoint(struct hfcsusb *hw, int channel);
  53. static void hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel);
  54. static int hfcsusb_setup_bch(struct bchannel *bch, int protocol);
  55. static void deactivate_bchannel(struct bchannel *bch);
  56. static void hfcsusb_ph_info(struct hfcsusb *hw);
  57. /* start next background transfer for control channel */
  58. static void
  59. ctrl_start_transfer(struct hfcsusb *hw)
  60. {
  61. if (debug & DBG_HFC_CALL_TRACE)
  62. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  63. if (hw->ctrl_cnt) {
  64. hw->ctrl_urb->pipe = hw->ctrl_out_pipe;
  65. hw->ctrl_urb->setup_packet = (u_char *)&hw->ctrl_write;
  66. hw->ctrl_urb->transfer_buffer = NULL;
  67. hw->ctrl_urb->transfer_buffer_length = 0;
  68. hw->ctrl_write.wIndex =
  69. cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].hfcs_reg);
  70. hw->ctrl_write.wValue =
  71. cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].reg_val);
  72. usb_submit_urb(hw->ctrl_urb, GFP_ATOMIC);
  73. }
  74. }
  75. /*
  76. * queue a control transfer request to write HFC-S USB
  77. * chip register using CTRL resuest queue
  78. */
  79. static int write_reg(struct hfcsusb *hw, __u8 reg, __u8 val)
  80. {
  81. struct ctrl_buf *buf;
  82. if (debug & DBG_HFC_CALL_TRACE)
  83. printk(KERN_DEBUG "%s: %s reg(0x%02x) val(0x%02x)\n",
  84. hw->name, __func__, reg, val);
  85. spin_lock(&hw->ctrl_lock);
  86. if (hw->ctrl_cnt >= HFC_CTRL_BUFSIZE) {
  87. spin_unlock(&hw->ctrl_lock);
  88. return 1;
  89. }
  90. buf = &hw->ctrl_buff[hw->ctrl_in_idx];
  91. buf->hfcs_reg = reg;
  92. buf->reg_val = val;
  93. if (++hw->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
  94. hw->ctrl_in_idx = 0;
  95. if (++hw->ctrl_cnt == 1)
  96. ctrl_start_transfer(hw);
  97. spin_unlock(&hw->ctrl_lock);
  98. return 0;
  99. }
  100. /* control completion routine handling background control cmds */
  101. static void
  102. ctrl_complete(struct urb *urb)
  103. {
  104. struct hfcsusb *hw = (struct hfcsusb *) urb->context;
  105. if (debug & DBG_HFC_CALL_TRACE)
  106. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  107. urb->dev = hw->dev;
  108. if (hw->ctrl_cnt) {
  109. hw->ctrl_cnt--; /* decrement actual count */
  110. if (++hw->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
  111. hw->ctrl_out_idx = 0; /* pointer wrap */
  112. ctrl_start_transfer(hw); /* start next transfer */
  113. }
  114. }
  115. /* handle LED bits */
  116. static void
  117. set_led_bit(struct hfcsusb *hw, signed short led_bits, int set_on)
  118. {
  119. if (set_on) {
  120. if (led_bits < 0)
  121. hw->led_state &= ~abs(led_bits);
  122. else
  123. hw->led_state |= led_bits;
  124. } else {
  125. if (led_bits < 0)
  126. hw->led_state |= abs(led_bits);
  127. else
  128. hw->led_state &= ~led_bits;
  129. }
  130. }
  131. /* handle LED requests */
  132. static void
  133. handle_led(struct hfcsusb *hw, int event)
  134. {
  135. struct hfcsusb_vdata *driver_info = (struct hfcsusb_vdata *)
  136. hfcsusb_idtab[hw->vend_idx].driver_info;
  137. __u8 tmpled;
  138. if (driver_info->led_scheme == LED_OFF)
  139. return;
  140. tmpled = hw->led_state;
  141. switch (event) {
  142. case LED_POWER_ON:
  143. set_led_bit(hw, driver_info->led_bits[0], 1);
  144. set_led_bit(hw, driver_info->led_bits[1], 0);
  145. set_led_bit(hw, driver_info->led_bits[2], 0);
  146. set_led_bit(hw, driver_info->led_bits[3], 0);
  147. break;
  148. case LED_POWER_OFF:
  149. set_led_bit(hw, driver_info->led_bits[0], 0);
  150. set_led_bit(hw, driver_info->led_bits[1], 0);
  151. set_led_bit(hw, driver_info->led_bits[2], 0);
  152. set_led_bit(hw, driver_info->led_bits[3], 0);
  153. break;
  154. case LED_S0_ON:
  155. set_led_bit(hw, driver_info->led_bits[1], 1);
  156. break;
  157. case LED_S0_OFF:
  158. set_led_bit(hw, driver_info->led_bits[1], 0);
  159. break;
  160. case LED_B1_ON:
  161. set_led_bit(hw, driver_info->led_bits[2], 1);
  162. break;
  163. case LED_B1_OFF:
  164. set_led_bit(hw, driver_info->led_bits[2], 0);
  165. break;
  166. case LED_B2_ON:
  167. set_led_bit(hw, driver_info->led_bits[3], 1);
  168. break;
  169. case LED_B2_OFF:
  170. set_led_bit(hw, driver_info->led_bits[3], 0);
  171. break;
  172. }
  173. if (hw->led_state != tmpled) {
  174. if (debug & DBG_HFC_CALL_TRACE)
  175. printk(KERN_DEBUG "%s: %s reg(0x%02x) val(x%02x)\n",
  176. hw->name, __func__,
  177. HFCUSB_P_DATA, hw->led_state);
  178. write_reg(hw, HFCUSB_P_DATA, hw->led_state);
  179. }
  180. }
  181. /*
  182. * Layer2 -> Layer 1 Bchannel data
  183. */
  184. static int
  185. hfcusb_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
  186. {
  187. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  188. struct hfcsusb *hw = bch->hw;
  189. int ret = -EINVAL;
  190. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  191. u_long flags;
  192. if (debug & DBG_HFC_CALL_TRACE)
  193. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  194. switch (hh->prim) {
  195. case PH_DATA_REQ:
  196. spin_lock_irqsave(&hw->lock, flags);
  197. ret = bchannel_senddata(bch, skb);
  198. spin_unlock_irqrestore(&hw->lock, flags);
  199. if (debug & DBG_HFC_CALL_TRACE)
  200. printk(KERN_DEBUG "%s: %s PH_DATA_REQ ret(%i)\n",
  201. hw->name, __func__, ret);
  202. if (ret > 0) {
  203. /*
  204. * other l1 drivers don't send early confirms on
  205. * transp data, but hfcsusb does because tx_next
  206. * skb is needed in tx_iso_complete()
  207. */
  208. queue_ch_frame(ch, PH_DATA_CNF, hh->id, NULL);
  209. ret = 0;
  210. }
  211. return ret;
  212. case PH_ACTIVATE_REQ:
  213. if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags)) {
  214. hfcsusb_start_endpoint(hw, bch->nr);
  215. ret = hfcsusb_setup_bch(bch, ch->protocol);
  216. } else
  217. ret = 0;
  218. if (!ret)
  219. _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
  220. 0, NULL, GFP_KERNEL);
  221. break;
  222. case PH_DEACTIVATE_REQ:
  223. deactivate_bchannel(bch);
  224. _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY,
  225. 0, NULL, GFP_KERNEL);
  226. ret = 0;
  227. break;
  228. }
  229. if (!ret)
  230. dev_kfree_skb(skb);
  231. return ret;
  232. }
  233. /*
  234. * send full D/B channel status information
  235. * as MPH_INFORMATION_IND
  236. */
  237. static void
  238. hfcsusb_ph_info(struct hfcsusb *hw)
  239. {
  240. struct ph_info *phi;
  241. struct dchannel *dch = &hw->dch;
  242. int i;
  243. phi = kzalloc(sizeof(struct ph_info) +
  244. dch->dev.nrbchan * sizeof(struct ph_info_ch), GFP_ATOMIC);
  245. phi->dch.ch.protocol = hw->protocol;
  246. phi->dch.ch.Flags = dch->Flags;
  247. phi->dch.state = dch->state;
  248. phi->dch.num_bch = dch->dev.nrbchan;
  249. for (i = 0; i < dch->dev.nrbchan; i++) {
  250. phi->bch[i].protocol = hw->bch[i].ch.protocol;
  251. phi->bch[i].Flags = hw->bch[i].Flags;
  252. }
  253. _queue_data(&dch->dev.D, MPH_INFORMATION_IND, MISDN_ID_ANY,
  254. sizeof(struct ph_info_dch) + dch->dev.nrbchan *
  255. sizeof(struct ph_info_ch), phi, GFP_ATOMIC);
  256. kfree(phi);
  257. }
  258. /*
  259. * Layer2 -> Layer 1 Dchannel data
  260. */
  261. static int
  262. hfcusb_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
  263. {
  264. struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
  265. struct dchannel *dch = container_of(dev, struct dchannel, dev);
  266. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  267. struct hfcsusb *hw = dch->hw;
  268. int ret = -EINVAL;
  269. u_long flags;
  270. switch (hh->prim) {
  271. case PH_DATA_REQ:
  272. if (debug & DBG_HFC_CALL_TRACE)
  273. printk(KERN_DEBUG "%s: %s: PH_DATA_REQ\n",
  274. hw->name, __func__);
  275. spin_lock_irqsave(&hw->lock, flags);
  276. ret = dchannel_senddata(dch, skb);
  277. spin_unlock_irqrestore(&hw->lock, flags);
  278. if (ret > 0) {
  279. ret = 0;
  280. queue_ch_frame(ch, PH_DATA_CNF, hh->id, NULL);
  281. }
  282. break;
  283. case PH_ACTIVATE_REQ:
  284. if (debug & DBG_HFC_CALL_TRACE)
  285. printk(KERN_DEBUG "%s: %s: PH_ACTIVATE_REQ %s\n",
  286. hw->name, __func__,
  287. (hw->protocol == ISDN_P_NT_S0) ? "NT" : "TE");
  288. if (hw->protocol == ISDN_P_NT_S0) {
  289. ret = 0;
  290. if (test_bit(FLG_ACTIVE, &dch->Flags)) {
  291. _queue_data(&dch->dev.D,
  292. PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
  293. NULL, GFP_ATOMIC);
  294. } else {
  295. hfcsusb_ph_command(hw,
  296. HFC_L1_ACTIVATE_NT);
  297. test_and_set_bit(FLG_L2_ACTIVATED,
  298. &dch->Flags);
  299. }
  300. } else {
  301. hfcsusb_ph_command(hw, HFC_L1_ACTIVATE_TE);
  302. ret = l1_event(dch->l1, hh->prim);
  303. }
  304. break;
  305. case PH_DEACTIVATE_REQ:
  306. if (debug & DBG_HFC_CALL_TRACE)
  307. printk(KERN_DEBUG "%s: %s: PH_DEACTIVATE_REQ\n",
  308. hw->name, __func__);
  309. test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
  310. if (hw->protocol == ISDN_P_NT_S0) {
  311. hfcsusb_ph_command(hw, HFC_L1_DEACTIVATE_NT);
  312. spin_lock_irqsave(&hw->lock, flags);
  313. skb_queue_purge(&dch->squeue);
  314. if (dch->tx_skb) {
  315. dev_kfree_skb(dch->tx_skb);
  316. dch->tx_skb = NULL;
  317. }
  318. dch->tx_idx = 0;
  319. if (dch->rx_skb) {
  320. dev_kfree_skb(dch->rx_skb);
  321. dch->rx_skb = NULL;
  322. }
  323. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  324. spin_unlock_irqrestore(&hw->lock, flags);
  325. #ifdef FIXME
  326. if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
  327. dchannel_sched_event(&hc->dch, D_CLEARBUSY);
  328. #endif
  329. ret = 0;
  330. } else
  331. ret = l1_event(dch->l1, hh->prim);
  332. break;
  333. case MPH_INFORMATION_REQ:
  334. hfcsusb_ph_info(hw);
  335. ret = 0;
  336. break;
  337. }
  338. return ret;
  339. }
  340. /*
  341. * Layer 1 callback function
  342. */
  343. static int
  344. hfc_l1callback(struct dchannel *dch, u_int cmd)
  345. {
  346. struct hfcsusb *hw = dch->hw;
  347. if (debug & DBG_HFC_CALL_TRACE)
  348. printk(KERN_DEBUG "%s: %s cmd 0x%x\n",
  349. hw->name, __func__, cmd);
  350. switch (cmd) {
  351. case INFO3_P8:
  352. case INFO3_P10:
  353. case HW_RESET_REQ:
  354. case HW_POWERUP_REQ:
  355. break;
  356. case HW_DEACT_REQ:
  357. skb_queue_purge(&dch->squeue);
  358. if (dch->tx_skb) {
  359. dev_kfree_skb(dch->tx_skb);
  360. dch->tx_skb = NULL;
  361. }
  362. dch->tx_idx = 0;
  363. if (dch->rx_skb) {
  364. dev_kfree_skb(dch->rx_skb);
  365. dch->rx_skb = NULL;
  366. }
  367. test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
  368. break;
  369. case PH_ACTIVATE_IND:
  370. test_and_set_bit(FLG_ACTIVE, &dch->Flags);
  371. _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
  372. GFP_ATOMIC);
  373. break;
  374. case PH_DEACTIVATE_IND:
  375. test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
  376. _queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
  377. GFP_ATOMIC);
  378. break;
  379. default:
  380. if (dch->debug & DEBUG_HW)
  381. printk(KERN_DEBUG "%s: %s: unknown cmd %x\n",
  382. hw->name, __func__, cmd);
  383. return -1;
  384. }
  385. hfcsusb_ph_info(hw);
  386. return 0;
  387. }
  388. static int
  389. open_dchannel(struct hfcsusb *hw, struct mISDNchannel *ch,
  390. struct channel_req *rq)
  391. {
  392. int err = 0;
  393. if (debug & DEBUG_HW_OPEN)
  394. printk(KERN_DEBUG "%s: %s: dev(%d) open addr(%i) from %p\n",
  395. hw->name, __func__, hw->dch.dev.id, rq->adr.channel,
  396. __builtin_return_address(0));
  397. if (rq->protocol == ISDN_P_NONE)
  398. return -EINVAL;
  399. test_and_clear_bit(FLG_ACTIVE, &hw->dch.Flags);
  400. test_and_clear_bit(FLG_ACTIVE, &hw->ech.Flags);
  401. hfcsusb_start_endpoint(hw, HFC_CHAN_D);
  402. /* E-Channel logging */
  403. if (rq->adr.channel == 1) {
  404. if (hw->fifos[HFCUSB_PCM_RX].pipe) {
  405. hfcsusb_start_endpoint(hw, HFC_CHAN_E);
  406. set_bit(FLG_ACTIVE, &hw->ech.Flags);
  407. _queue_data(&hw->ech.dev.D, PH_ACTIVATE_IND,
  408. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  409. } else
  410. return -EINVAL;
  411. }
  412. if (!hw->initdone) {
  413. hw->protocol = rq->protocol;
  414. if (rq->protocol == ISDN_P_TE_S0) {
  415. err = create_l1(&hw->dch, hfc_l1callback);
  416. if (err)
  417. return err;
  418. }
  419. setPortMode(hw);
  420. ch->protocol = rq->protocol;
  421. hw->initdone = 1;
  422. } else {
  423. if (rq->protocol != ch->protocol)
  424. return -EPROTONOSUPPORT;
  425. }
  426. if (((ch->protocol == ISDN_P_NT_S0) && (hw->dch.state == 3)) ||
  427. ((ch->protocol == ISDN_P_TE_S0) && (hw->dch.state == 7)))
  428. _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
  429. 0, NULL, GFP_KERNEL);
  430. rq->ch = ch;
  431. if (!try_module_get(THIS_MODULE))
  432. printk(KERN_WARNING "%s: %s: cannot get module\n",
  433. hw->name, __func__);
  434. return 0;
  435. }
  436. static int
  437. open_bchannel(struct hfcsusb *hw, struct channel_req *rq)
  438. {
  439. struct bchannel *bch;
  440. if (rq->adr.channel > 2)
  441. return -EINVAL;
  442. if (rq->protocol == ISDN_P_NONE)
  443. return -EINVAL;
  444. if (debug & DBG_HFC_CALL_TRACE)
  445. printk(KERN_DEBUG "%s: %s B%i\n",
  446. hw->name, __func__, rq->adr.channel);
  447. bch = &hw->bch[rq->adr.channel - 1];
  448. if (test_and_set_bit(FLG_OPEN, &bch->Flags))
  449. return -EBUSY; /* b-channel can be only open once */
  450. test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
  451. bch->ch.protocol = rq->protocol;
  452. rq->ch = &bch->ch;
  453. /* start USB endpoint for bchannel */
  454. if (rq->adr.channel == 1)
  455. hfcsusb_start_endpoint(hw, HFC_CHAN_B1);
  456. else
  457. hfcsusb_start_endpoint(hw, HFC_CHAN_B2);
  458. if (!try_module_get(THIS_MODULE))
  459. printk(KERN_WARNING "%s: %s:cannot get module\n",
  460. hw->name, __func__);
  461. return 0;
  462. }
  463. static int
  464. channel_ctrl(struct hfcsusb *hw, struct mISDN_ctrl_req *cq)
  465. {
  466. int ret = 0;
  467. if (debug & DBG_HFC_CALL_TRACE)
  468. printk(KERN_DEBUG "%s: %s op(0x%x) channel(0x%x)\n",
  469. hw->name, __func__, (cq->op), (cq->channel));
  470. switch (cq->op) {
  471. case MISDN_CTRL_GETOP:
  472. cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
  473. MISDN_CTRL_DISCONNECT;
  474. break;
  475. default:
  476. printk(KERN_WARNING "%s: %s: unknown Op %x\n",
  477. hw->name, __func__, cq->op);
  478. ret = -EINVAL;
  479. break;
  480. }
  481. return ret;
  482. }
  483. /*
  484. * device control function
  485. */
  486. static int
  487. hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  488. {
  489. struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
  490. struct dchannel *dch = container_of(dev, struct dchannel, dev);
  491. struct hfcsusb *hw = dch->hw;
  492. struct channel_req *rq;
  493. int err = 0;
  494. if (dch->debug & DEBUG_HW)
  495. printk(KERN_DEBUG "%s: %s: cmd:%x %p\n",
  496. hw->name, __func__, cmd, arg);
  497. switch (cmd) {
  498. case OPEN_CHANNEL:
  499. rq = arg;
  500. if ((rq->protocol == ISDN_P_TE_S0) ||
  501. (rq->protocol == ISDN_P_NT_S0))
  502. err = open_dchannel(hw, ch, rq);
  503. else
  504. err = open_bchannel(hw, rq);
  505. if (!err)
  506. hw->open++;
  507. break;
  508. case CLOSE_CHANNEL:
  509. hw->open--;
  510. if (debug & DEBUG_HW_OPEN)
  511. printk(KERN_DEBUG
  512. "%s: %s: dev(%d) close from %p (open %d)\n",
  513. hw->name, __func__, hw->dch.dev.id,
  514. __builtin_return_address(0), hw->open);
  515. if (!hw->open) {
  516. hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
  517. if (hw->fifos[HFCUSB_PCM_RX].pipe)
  518. hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
  519. handle_led(hw, LED_POWER_ON);
  520. }
  521. module_put(THIS_MODULE);
  522. break;
  523. case CONTROL_CHANNEL:
  524. err = channel_ctrl(hw, arg);
  525. break;
  526. default:
  527. if (dch->debug & DEBUG_HW)
  528. printk(KERN_DEBUG "%s: %s: unknown command %x\n",
  529. hw->name, __func__, cmd);
  530. return -EINVAL;
  531. }
  532. return err;
  533. }
  534. /*
  535. * S0 TE state change event handler
  536. */
  537. static void
  538. ph_state_te(struct dchannel *dch)
  539. {
  540. struct hfcsusb *hw = dch->hw;
  541. if (debug & DEBUG_HW) {
  542. if (dch->state <= HFC_MAX_TE_LAYER1_STATE)
  543. printk(KERN_DEBUG "%s: %s: %s\n", hw->name, __func__,
  544. HFC_TE_LAYER1_STATES[dch->state]);
  545. else
  546. printk(KERN_DEBUG "%s: %s: TE F%d\n",
  547. hw->name, __func__, dch->state);
  548. }
  549. switch (dch->state) {
  550. case 0:
  551. l1_event(dch->l1, HW_RESET_IND);
  552. break;
  553. case 3:
  554. l1_event(dch->l1, HW_DEACT_IND);
  555. break;
  556. case 5:
  557. case 8:
  558. l1_event(dch->l1, ANYSIGNAL);
  559. break;
  560. case 6:
  561. l1_event(dch->l1, INFO2);
  562. break;
  563. case 7:
  564. l1_event(dch->l1, INFO4_P8);
  565. break;
  566. }
  567. if (dch->state == 7)
  568. handle_led(hw, LED_S0_ON);
  569. else
  570. handle_led(hw, LED_S0_OFF);
  571. }
  572. /*
  573. * S0 NT state change event handler
  574. */
  575. static void
  576. ph_state_nt(struct dchannel *dch)
  577. {
  578. struct hfcsusb *hw = dch->hw;
  579. if (debug & DEBUG_HW) {
  580. if (dch->state <= HFC_MAX_NT_LAYER1_STATE)
  581. printk(KERN_DEBUG "%s: %s: %s\n",
  582. hw->name, __func__,
  583. HFC_NT_LAYER1_STATES[dch->state]);
  584. else
  585. printk(KERN_INFO DRIVER_NAME "%s: %s: NT G%d\n",
  586. hw->name, __func__, dch->state);
  587. }
  588. switch (dch->state) {
  589. case (1):
  590. test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
  591. test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
  592. hw->nt_timer = 0;
  593. hw->timers &= ~NT_ACTIVATION_TIMER;
  594. handle_led(hw, LED_S0_OFF);
  595. break;
  596. case (2):
  597. if (hw->nt_timer < 0) {
  598. hw->nt_timer = 0;
  599. hw->timers &= ~NT_ACTIVATION_TIMER;
  600. hfcsusb_ph_command(dch->hw, HFC_L1_DEACTIVATE_NT);
  601. } else {
  602. hw->timers |= NT_ACTIVATION_TIMER;
  603. hw->nt_timer = NT_T1_COUNT;
  604. /* allow G2 -> G3 transition */
  605. write_reg(hw, HFCUSB_STATES, 2 | HFCUSB_NT_G2_G3);
  606. }
  607. break;
  608. case (3):
  609. hw->nt_timer = 0;
  610. hw->timers &= ~NT_ACTIVATION_TIMER;
  611. test_and_set_bit(FLG_ACTIVE, &dch->Flags);
  612. _queue_data(&dch->dev.D, PH_ACTIVATE_IND,
  613. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  614. handle_led(hw, LED_S0_ON);
  615. break;
  616. case (4):
  617. hw->nt_timer = 0;
  618. hw->timers &= ~NT_ACTIVATION_TIMER;
  619. break;
  620. default:
  621. break;
  622. }
  623. hfcsusb_ph_info(hw);
  624. }
  625. static void
  626. ph_state(struct dchannel *dch)
  627. {
  628. struct hfcsusb *hw = dch->hw;
  629. if (hw->protocol == ISDN_P_NT_S0)
  630. ph_state_nt(dch);
  631. else if (hw->protocol == ISDN_P_TE_S0)
  632. ph_state_te(dch);
  633. }
  634. /*
  635. * disable/enable BChannel for desired protocoll
  636. */
  637. static int
  638. hfcsusb_setup_bch(struct bchannel *bch, int protocol)
  639. {
  640. struct hfcsusb *hw = bch->hw;
  641. __u8 conhdlc, sctrl, sctrl_r;
  642. if (debug & DEBUG_HW)
  643. printk(KERN_DEBUG "%s: %s: protocol %x-->%x B%d\n",
  644. hw->name, __func__, bch->state, protocol,
  645. bch->nr);
  646. /* setup val for CON_HDLC */
  647. conhdlc = 0;
  648. if (protocol > ISDN_P_NONE)
  649. conhdlc = 8; /* enable FIFO */
  650. switch (protocol) {
  651. case (-1): /* used for init */
  652. bch->state = -1;
  653. /* fall through */
  654. case (ISDN_P_NONE):
  655. if (bch->state == ISDN_P_NONE)
  656. return 0; /* already in idle state */
  657. bch->state = ISDN_P_NONE;
  658. clear_bit(FLG_HDLC, &bch->Flags);
  659. clear_bit(FLG_TRANSPARENT, &bch->Flags);
  660. break;
  661. case (ISDN_P_B_RAW):
  662. conhdlc |= 2;
  663. bch->state = protocol;
  664. set_bit(FLG_TRANSPARENT, &bch->Flags);
  665. break;
  666. case (ISDN_P_B_HDLC):
  667. bch->state = protocol;
  668. set_bit(FLG_HDLC, &bch->Flags);
  669. break;
  670. default:
  671. if (debug & DEBUG_HW)
  672. printk(KERN_DEBUG "%s: %s: prot not known %x\n",
  673. hw->name, __func__, protocol);
  674. return -ENOPROTOOPT;
  675. }
  676. if (protocol >= ISDN_P_NONE) {
  677. write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 0 : 2);
  678. write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
  679. write_reg(hw, HFCUSB_INC_RES_F, 2);
  680. write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 1 : 3);
  681. write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
  682. write_reg(hw, HFCUSB_INC_RES_F, 2);
  683. sctrl = 0x40 + ((hw->protocol == ISDN_P_TE_S0) ? 0x00 : 0x04);
  684. sctrl_r = 0x0;
  685. if (test_bit(FLG_ACTIVE, &hw->bch[0].Flags)) {
  686. sctrl |= 1;
  687. sctrl_r |= 1;
  688. }
  689. if (test_bit(FLG_ACTIVE, &hw->bch[1].Flags)) {
  690. sctrl |= 2;
  691. sctrl_r |= 2;
  692. }
  693. write_reg(hw, HFCUSB_SCTRL, sctrl);
  694. write_reg(hw, HFCUSB_SCTRL_R, sctrl_r);
  695. if (protocol > ISDN_P_NONE)
  696. handle_led(hw, (bch->nr == 1) ? LED_B1_ON : LED_B2_ON);
  697. else
  698. handle_led(hw, (bch->nr == 1) ? LED_B1_OFF :
  699. LED_B2_OFF);
  700. }
  701. hfcsusb_ph_info(hw);
  702. return 0;
  703. }
  704. static void
  705. hfcsusb_ph_command(struct hfcsusb *hw, u_char command)
  706. {
  707. if (debug & DEBUG_HW)
  708. printk(KERN_DEBUG "%s: %s: %x\n",
  709. hw->name, __func__, command);
  710. switch (command) {
  711. case HFC_L1_ACTIVATE_TE:
  712. /* force sending sending INFO1 */
  713. write_reg(hw, HFCUSB_STATES, 0x14);
  714. /* start l1 activation */
  715. write_reg(hw, HFCUSB_STATES, 0x04);
  716. break;
  717. case HFC_L1_FORCE_DEACTIVATE_TE:
  718. write_reg(hw, HFCUSB_STATES, 0x10);
  719. write_reg(hw, HFCUSB_STATES, 0x03);
  720. break;
  721. case HFC_L1_ACTIVATE_NT:
  722. if (hw->dch.state == 3)
  723. _queue_data(&hw->dch.dev.D, PH_ACTIVATE_IND,
  724. MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
  725. else
  726. write_reg(hw, HFCUSB_STATES, HFCUSB_ACTIVATE |
  727. HFCUSB_DO_ACTION | HFCUSB_NT_G2_G3);
  728. break;
  729. case HFC_L1_DEACTIVATE_NT:
  730. write_reg(hw, HFCUSB_STATES,
  731. HFCUSB_DO_ACTION);
  732. break;
  733. }
  734. }
  735. /*
  736. * Layer 1 B-channel hardware access
  737. */
  738. static int
  739. channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
  740. {
  741. int ret = 0;
  742. switch (cq->op) {
  743. case MISDN_CTRL_GETOP:
  744. cq->op = MISDN_CTRL_FILL_EMPTY;
  745. break;
  746. case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */
  747. test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);
  748. if (debug & DEBUG_HW_OPEN)
  749. printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "
  750. "off=%d)\n", __func__, bch->nr, !!cq->p1);
  751. break;
  752. default:
  753. printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);
  754. ret = -EINVAL;
  755. break;
  756. }
  757. return ret;
  758. }
  759. /* collect data from incoming interrupt or isochron USB data */
  760. static void
  761. hfcsusb_rx_frame(struct usb_fifo *fifo, __u8 *data, unsigned int len,
  762. int finish)
  763. {
  764. struct hfcsusb *hw = fifo->hw;
  765. struct sk_buff *rx_skb = NULL;
  766. int maxlen = 0;
  767. int fifon = fifo->fifonum;
  768. int i;
  769. int hdlc = 0;
  770. if (debug & DBG_HFC_CALL_TRACE)
  771. printk(KERN_DEBUG "%s: %s: fifo(%i) len(%i) "
  772. "dch(%p) bch(%p) ech(%p)\n",
  773. hw->name, __func__, fifon, len,
  774. fifo->dch, fifo->bch, fifo->ech);
  775. if (!len)
  776. return;
  777. if ((!!fifo->dch + !!fifo->bch + !!fifo->ech) != 1) {
  778. printk(KERN_DEBUG "%s: %s: undefined channel\n",
  779. hw->name, __func__);
  780. return;
  781. }
  782. spin_lock(&hw->lock);
  783. if (fifo->dch) {
  784. rx_skb = fifo->dch->rx_skb;
  785. maxlen = fifo->dch->maxlen;
  786. hdlc = 1;
  787. }
  788. if (fifo->bch) {
  789. rx_skb = fifo->bch->rx_skb;
  790. maxlen = fifo->bch->maxlen;
  791. hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
  792. }
  793. if (fifo->ech) {
  794. rx_skb = fifo->ech->rx_skb;
  795. maxlen = fifo->ech->maxlen;
  796. hdlc = 1;
  797. }
  798. if (!rx_skb) {
  799. rx_skb = mI_alloc_skb(maxlen, GFP_ATOMIC);
  800. if (rx_skb) {
  801. if (fifo->dch)
  802. fifo->dch->rx_skb = rx_skb;
  803. if (fifo->bch)
  804. fifo->bch->rx_skb = rx_skb;
  805. if (fifo->ech)
  806. fifo->ech->rx_skb = rx_skb;
  807. skb_trim(rx_skb, 0);
  808. } else {
  809. printk(KERN_DEBUG "%s: %s: No mem for rx_skb\n",
  810. hw->name, __func__);
  811. spin_unlock(&hw->lock);
  812. return;
  813. }
  814. }
  815. if (fifo->dch || fifo->ech) {
  816. /* D/E-Channel SKB range check */
  817. if ((rx_skb->len + len) >= MAX_DFRAME_LEN_L1) {
  818. printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
  819. "for fifo(%d) HFCUSB_D_RX\n",
  820. hw->name, __func__, fifon);
  821. skb_trim(rx_skb, 0);
  822. spin_unlock(&hw->lock);
  823. return;
  824. }
  825. } else if (fifo->bch) {
  826. /* B-Channel SKB range check */
  827. if ((rx_skb->len + len) >= (MAX_BCH_SIZE + 3)) {
  828. printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
  829. "for fifo(%d) HFCUSB_B_RX\n",
  830. hw->name, __func__, fifon);
  831. skb_trim(rx_skb, 0);
  832. spin_unlock(&hw->lock);
  833. return;
  834. }
  835. }
  836. memcpy(skb_put(rx_skb, len), data, len);
  837. if (hdlc) {
  838. /* we have a complete hdlc packet */
  839. if (finish) {
  840. if ((rx_skb->len > 3) &&
  841. (!(rx_skb->data[rx_skb->len - 1]))) {
  842. if (debug & DBG_HFC_FIFO_VERBOSE) {
  843. printk(KERN_DEBUG "%s: %s: fifon(%i)"
  844. " new RX len(%i): ",
  845. hw->name, __func__, fifon,
  846. rx_skb->len);
  847. i = 0;
  848. while (i < rx_skb->len)
  849. printk("%02x ",
  850. rx_skb->data[i++]);
  851. printk("\n");
  852. }
  853. /* remove CRC & status */
  854. skb_trim(rx_skb, rx_skb->len - 3);
  855. if (fifo->dch)
  856. recv_Dchannel(fifo->dch);
  857. if (fifo->bch)
  858. recv_Bchannel(fifo->bch, MISDN_ID_ANY);
  859. if (fifo->ech)
  860. recv_Echannel(fifo->ech,
  861. &hw->dch);
  862. } else {
  863. if (debug & DBG_HFC_FIFO_VERBOSE) {
  864. printk(KERN_DEBUG
  865. "%s: CRC or minlen ERROR fifon(%i) "
  866. "RX len(%i): ",
  867. hw->name, fifon, rx_skb->len);
  868. i = 0;
  869. while (i < rx_skb->len)
  870. printk("%02x ",
  871. rx_skb->data[i++]);
  872. printk("\n");
  873. }
  874. skb_trim(rx_skb, 0);
  875. }
  876. }
  877. } else {
  878. /* deliver transparent data to layer2 */
  879. if (rx_skb->len >= poll)
  880. recv_Bchannel(fifo->bch, MISDN_ID_ANY);
  881. }
  882. spin_unlock(&hw->lock);
  883. }
  884. static void
  885. fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
  886. void *buf, int num_packets, int packet_size, int interval,
  887. usb_complete_t complete, void *context)
  888. {
  889. int k;
  890. usb_fill_bulk_urb(urb, dev, pipe, buf, packet_size * num_packets,
  891. complete, context);
  892. urb->number_of_packets = num_packets;
  893. urb->transfer_flags = URB_ISO_ASAP;
  894. urb->actual_length = 0;
  895. urb->interval = interval;
  896. for (k = 0; k < num_packets; k++) {
  897. urb->iso_frame_desc[k].offset = packet_size * k;
  898. urb->iso_frame_desc[k].length = packet_size;
  899. urb->iso_frame_desc[k].actual_length = 0;
  900. }
  901. }
  902. /* receive completion routine for all ISO tx fifos */
  903. static void
  904. rx_iso_complete(struct urb *urb)
  905. {
  906. struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
  907. struct usb_fifo *fifo = context_iso_urb->owner_fifo;
  908. struct hfcsusb *hw = fifo->hw;
  909. int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
  910. status, iso_status, i;
  911. __u8 *buf;
  912. static __u8 eof[8];
  913. __u8 s0_state;
  914. fifon = fifo->fifonum;
  915. status = urb->status;
  916. spin_lock(&hw->lock);
  917. if (fifo->stop_gracefull) {
  918. fifo->stop_gracefull = 0;
  919. fifo->active = 0;
  920. spin_unlock(&hw->lock);
  921. return;
  922. }
  923. spin_unlock(&hw->lock);
  924. /*
  925. * ISO transfer only partially completed,
  926. * look at individual frame status for details
  927. */
  928. if (status == -EXDEV) {
  929. if (debug & DEBUG_HW)
  930. printk(KERN_DEBUG "%s: %s: with -EXDEV "
  931. "urb->status %d, fifonum %d\n",
  932. hw->name, __func__, status, fifon);
  933. /* clear status, so go on with ISO transfers */
  934. status = 0;
  935. }
  936. s0_state = 0;
  937. if (fifo->active && !status) {
  938. num_isoc_packets = iso_packets[fifon];
  939. maxlen = fifo->usb_packet_maxlen;
  940. for (k = 0; k < num_isoc_packets; ++k) {
  941. len = urb->iso_frame_desc[k].actual_length;
  942. offset = urb->iso_frame_desc[k].offset;
  943. buf = context_iso_urb->buffer + offset;
  944. iso_status = urb->iso_frame_desc[k].status;
  945. if (iso_status && (debug & DBG_HFC_FIFO_VERBOSE)) {
  946. printk(KERN_DEBUG "%s: %s: "
  947. "ISO packet %i, status: %i\n",
  948. hw->name, __func__, k, iso_status);
  949. }
  950. /* USB data log for every D ISO in */
  951. if ((fifon == HFCUSB_D_RX) &&
  952. (debug & DBG_HFC_USB_VERBOSE)) {
  953. printk(KERN_DEBUG
  954. "%s: %s: %d (%d/%d) len(%d) ",
  955. hw->name, __func__, urb->start_frame,
  956. k, num_isoc_packets-1,
  957. len);
  958. for (i = 0; i < len; i++)
  959. printk("%x ", buf[i]);
  960. printk("\n");
  961. }
  962. if (!iso_status) {
  963. if (fifo->last_urblen != maxlen) {
  964. /*
  965. * save fifo fill-level threshold bits
  966. * to use them later in TX ISO URB
  967. * completions
  968. */
  969. hw->threshold_mask = buf[1];
  970. if (fifon == HFCUSB_D_RX)
  971. s0_state = (buf[0] >> 4);
  972. eof[fifon] = buf[0] & 1;
  973. if (len > 2)
  974. hfcsusb_rx_frame(fifo, buf + 2,
  975. len - 2, (len < maxlen)
  976. ? eof[fifon] : 0);
  977. } else
  978. hfcsusb_rx_frame(fifo, buf, len,
  979. (len < maxlen) ?
  980. eof[fifon] : 0);
  981. fifo->last_urblen = len;
  982. }
  983. }
  984. /* signal S0 layer1 state change */
  985. if ((s0_state) && (hw->initdone) &&
  986. (s0_state != hw->dch.state)) {
  987. hw->dch.state = s0_state;
  988. schedule_event(&hw->dch, FLG_PHCHANGE);
  989. }
  990. fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
  991. context_iso_urb->buffer, num_isoc_packets,
  992. fifo->usb_packet_maxlen, fifo->intervall,
  993. (usb_complete_t)rx_iso_complete, urb->context);
  994. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  995. if (errcode < 0) {
  996. if (debug & DEBUG_HW)
  997. printk(KERN_DEBUG "%s: %s: error submitting "
  998. "ISO URB: %d\n",
  999. hw->name, __func__, errcode);
  1000. }
  1001. } else {
  1002. if (status && (debug & DBG_HFC_URB_INFO))
  1003. printk(KERN_DEBUG "%s: %s: rx_iso_complete : "
  1004. "urb->status %d, fifonum %d\n",
  1005. hw->name, __func__, status, fifon);
  1006. }
  1007. }
  1008. /* receive completion routine for all interrupt rx fifos */
  1009. static void
  1010. rx_int_complete(struct urb *urb)
  1011. {
  1012. int len, status, i;
  1013. __u8 *buf, maxlen, fifon;
  1014. struct usb_fifo *fifo = (struct usb_fifo *) urb->context;
  1015. struct hfcsusb *hw = fifo->hw;
  1016. static __u8 eof[8];
  1017. spin_lock(&hw->lock);
  1018. if (fifo->stop_gracefull) {
  1019. fifo->stop_gracefull = 0;
  1020. fifo->active = 0;
  1021. spin_unlock(&hw->lock);
  1022. return;
  1023. }
  1024. spin_unlock(&hw->lock);
  1025. fifon = fifo->fifonum;
  1026. if ((!fifo->active) || (urb->status)) {
  1027. if (debug & DBG_HFC_URB_ERROR)
  1028. printk(KERN_DEBUG
  1029. "%s: %s: RX-Fifo %i is going down (%i)\n",
  1030. hw->name, __func__, fifon, urb->status);
  1031. fifo->urb->interval = 0; /* cancel automatic rescheduling */
  1032. return;
  1033. }
  1034. len = urb->actual_length;
  1035. buf = fifo->buffer;
  1036. maxlen = fifo->usb_packet_maxlen;
  1037. /* USB data log for every D INT in */
  1038. if ((fifon == HFCUSB_D_RX) && (debug & DBG_HFC_USB_VERBOSE)) {
  1039. printk(KERN_DEBUG "%s: %s: D RX INT len(%d) ",
  1040. hw->name, __func__, len);
  1041. for (i = 0; i < len; i++)
  1042. printk("%02x ", buf[i]);
  1043. printk("\n");
  1044. }
  1045. if (fifo->last_urblen != fifo->usb_packet_maxlen) {
  1046. /* the threshold mask is in the 2nd status byte */
  1047. hw->threshold_mask = buf[1];
  1048. /* signal S0 layer1 state change */
  1049. if (hw->initdone && ((buf[0] >> 4) != hw->dch.state)) {
  1050. hw->dch.state = (buf[0] >> 4);
  1051. schedule_event(&hw->dch, FLG_PHCHANGE);
  1052. }
  1053. eof[fifon] = buf[0] & 1;
  1054. /* if we have more than the 2 status bytes -> collect data */
  1055. if (len > 2)
  1056. hfcsusb_rx_frame(fifo, buf + 2,
  1057. urb->actual_length - 2,
  1058. (len < maxlen) ? eof[fifon] : 0);
  1059. } else {
  1060. hfcsusb_rx_frame(fifo, buf, urb->actual_length,
  1061. (len < maxlen) ? eof[fifon] : 0);
  1062. }
  1063. fifo->last_urblen = urb->actual_length;
  1064. status = usb_submit_urb(urb, GFP_ATOMIC);
  1065. if (status) {
  1066. if (debug & DEBUG_HW)
  1067. printk(KERN_DEBUG "%s: %s: error resubmitting USB\n",
  1068. hw->name, __func__);
  1069. }
  1070. }
  1071. /* transmit completion routine for all ISO tx fifos */
  1072. static void
  1073. tx_iso_complete(struct urb *urb)
  1074. {
  1075. struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
  1076. struct usb_fifo *fifo = context_iso_urb->owner_fifo;
  1077. struct hfcsusb *hw = fifo->hw;
  1078. struct sk_buff *tx_skb;
  1079. int k, tx_offset, num_isoc_packets, sink, remain, current_len,
  1080. errcode, hdlc, i;
  1081. int *tx_idx;
  1082. int frame_complete, fifon, status;
  1083. __u8 threshbit;
  1084. spin_lock(&hw->lock);
  1085. if (fifo->stop_gracefull) {
  1086. fifo->stop_gracefull = 0;
  1087. fifo->active = 0;
  1088. spin_unlock(&hw->lock);
  1089. return;
  1090. }
  1091. if (fifo->dch) {
  1092. tx_skb = fifo->dch->tx_skb;
  1093. tx_idx = &fifo->dch->tx_idx;
  1094. hdlc = 1;
  1095. } else if (fifo->bch) {
  1096. tx_skb = fifo->bch->tx_skb;
  1097. tx_idx = &fifo->bch->tx_idx;
  1098. hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
  1099. } else {
  1100. printk(KERN_DEBUG "%s: %s: neither BCH nor DCH\n",
  1101. hw->name, __func__);
  1102. spin_unlock(&hw->lock);
  1103. return;
  1104. }
  1105. fifon = fifo->fifonum;
  1106. status = urb->status;
  1107. tx_offset = 0;
  1108. /*
  1109. * ISO transfer only partially completed,
  1110. * look at individual frame status for details
  1111. */
  1112. if (status == -EXDEV) {
  1113. if (debug & DBG_HFC_URB_ERROR)
  1114. printk(KERN_DEBUG "%s: %s: "
  1115. "-EXDEV (%i) fifon (%d)\n",
  1116. hw->name, __func__, status, fifon);
  1117. /* clear status, so go on with ISO transfers */
  1118. status = 0;
  1119. }
  1120. if (fifo->active && !status) {
  1121. /* is FifoFull-threshold set for our channel? */
  1122. threshbit = (hw->threshold_mask & (1 << fifon));
  1123. num_isoc_packets = iso_packets[fifon];
  1124. /* predict dataflow to avoid fifo overflow */
  1125. if (fifon >= HFCUSB_D_TX)
  1126. sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
  1127. else
  1128. sink = (threshbit) ? SINK_MIN : SINK_MAX;
  1129. fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
  1130. context_iso_urb->buffer, num_isoc_packets,
  1131. fifo->usb_packet_maxlen, fifo->intervall,
  1132. (usb_complete_t)tx_iso_complete, urb->context);
  1133. memset(context_iso_urb->buffer, 0,
  1134. sizeof(context_iso_urb->buffer));
  1135. frame_complete = 0;
  1136. for (k = 0; k < num_isoc_packets; ++k) {
  1137. /* analyze tx success of previous ISO packets */
  1138. if (debug & DBG_HFC_URB_ERROR) {
  1139. errcode = urb->iso_frame_desc[k].status;
  1140. if (errcode) {
  1141. printk(KERN_DEBUG "%s: %s: "
  1142. "ISO packet %i, status: %i\n",
  1143. hw->name, __func__, k, errcode);
  1144. }
  1145. }
  1146. /* Generate next ISO Packets */
  1147. if (tx_skb)
  1148. remain = tx_skb->len - *tx_idx;
  1149. else
  1150. remain = 0;
  1151. if (remain > 0) {
  1152. fifo->bit_line -= sink;
  1153. current_len = (0 - fifo->bit_line) / 8;
  1154. if (current_len > 14)
  1155. current_len = 14;
  1156. if (current_len < 0)
  1157. current_len = 0;
  1158. if (remain < current_len)
  1159. current_len = remain;
  1160. /* how much bit do we put on the line? */
  1161. fifo->bit_line += current_len * 8;
  1162. context_iso_urb->buffer[tx_offset] = 0;
  1163. if (current_len == remain) {
  1164. if (hdlc) {
  1165. /* signal frame completion */
  1166. context_iso_urb->
  1167. buffer[tx_offset] = 1;
  1168. /* add 2 byte flags and 16bit
  1169. * CRC at end of ISDN frame */
  1170. fifo->bit_line += 32;
  1171. }
  1172. frame_complete = 1;
  1173. }
  1174. /* copy tx data to iso-urb buffer */
  1175. memcpy(context_iso_urb->buffer + tx_offset + 1,
  1176. (tx_skb->data + *tx_idx), current_len);
  1177. *tx_idx += current_len;
  1178. urb->iso_frame_desc[k].offset = tx_offset;
  1179. urb->iso_frame_desc[k].length = current_len + 1;
  1180. /* USB data log for every D ISO out */
  1181. if ((fifon == HFCUSB_D_RX) &&
  1182. (debug & DBG_HFC_USB_VERBOSE)) {
  1183. printk(KERN_DEBUG
  1184. "%s: %s (%d/%d) offs(%d) len(%d) ",
  1185. hw->name, __func__,
  1186. k, num_isoc_packets-1,
  1187. urb->iso_frame_desc[k].offset,
  1188. urb->iso_frame_desc[k].length);
  1189. for (i = urb->iso_frame_desc[k].offset;
  1190. i < (urb->iso_frame_desc[k].offset
  1191. + urb->iso_frame_desc[k].length);
  1192. i++)
  1193. printk("%x ",
  1194. context_iso_urb->buffer[i]);
  1195. printk(" skb->len(%i) tx-idx(%d)\n",
  1196. tx_skb->len, *tx_idx);
  1197. }
  1198. tx_offset += (current_len + 1);
  1199. } else {
  1200. urb->iso_frame_desc[k].offset = tx_offset++;
  1201. urb->iso_frame_desc[k].length = 1;
  1202. /* we lower data margin every msec */
  1203. fifo->bit_line -= sink;
  1204. if (fifo->bit_line < BITLINE_INF)
  1205. fifo->bit_line = BITLINE_INF;
  1206. }
  1207. if (frame_complete) {
  1208. frame_complete = 0;
  1209. if (debug & DBG_HFC_FIFO_VERBOSE) {
  1210. printk(KERN_DEBUG "%s: %s: "
  1211. "fifon(%i) new TX len(%i): ",
  1212. hw->name, __func__,
  1213. fifon, tx_skb->len);
  1214. i = 0;
  1215. while (i < tx_skb->len)
  1216. printk("%02x ",
  1217. tx_skb->data[i++]);
  1218. printk("\n");
  1219. }
  1220. dev_kfree_skb(tx_skb);
  1221. tx_skb = NULL;
  1222. if (fifo->dch && get_next_dframe(fifo->dch))
  1223. tx_skb = fifo->dch->tx_skb;
  1224. else if (fifo->bch &&
  1225. get_next_bframe(fifo->bch)) {
  1226. if (test_bit(FLG_TRANSPARENT,
  1227. &fifo->bch->Flags))
  1228. confirm_Bsend(fifo->bch);
  1229. tx_skb = fifo->bch->tx_skb;
  1230. }
  1231. }
  1232. }
  1233. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  1234. if (errcode < 0) {
  1235. if (debug & DEBUG_HW)
  1236. printk(KERN_DEBUG
  1237. "%s: %s: error submitting ISO URB: %d \n",
  1238. hw->name, __func__, errcode);
  1239. }
  1240. /*
  1241. * abuse DChannel tx iso completion to trigger NT mode state
  1242. * changes tx_iso_complete is assumed to be called every
  1243. * fifo->intervall (ms)
  1244. */
  1245. if ((fifon == HFCUSB_D_TX) && (hw->protocol == ISDN_P_NT_S0)
  1246. && (hw->timers & NT_ACTIVATION_TIMER)) {
  1247. if ((--hw->nt_timer) < 0)
  1248. schedule_event(&hw->dch, FLG_PHCHANGE);
  1249. }
  1250. } else {
  1251. if (status && (debug & DBG_HFC_URB_ERROR))
  1252. printk(KERN_DEBUG "%s: %s: urb->status %s (%i)"
  1253. "fifonum=%d\n",
  1254. hw->name, __func__,
  1255. symbolic(urb_errlist, status), status, fifon);
  1256. }
  1257. spin_unlock(&hw->lock);
  1258. }
  1259. /*
  1260. * allocs urbs and start isoc transfer with two pending urbs to avoid
  1261. * gaps in the transfer chain
  1262. */
  1263. static int
  1264. start_isoc_chain(struct usb_fifo *fifo, int num_packets_per_urb,
  1265. usb_complete_t complete, int packet_size)
  1266. {
  1267. struct hfcsusb *hw = fifo->hw;
  1268. int i, k, errcode;
  1269. if (debug)
  1270. printk(KERN_DEBUG "%s: %s: fifo %i\n",
  1271. hw->name, __func__, fifo->fifonum);
  1272. /* allocate Memory for Iso out Urbs */
  1273. for (i = 0; i < 2; i++) {
  1274. if (!(fifo->iso[i].urb)) {
  1275. fifo->iso[i].urb =
  1276. usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
  1277. if (!(fifo->iso[i].urb)) {
  1278. printk(KERN_DEBUG
  1279. "%s: %s: alloc urb for fifo %i failed",
  1280. hw->name, __func__, fifo->fifonum);
  1281. }
  1282. fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
  1283. fifo->iso[i].indx = i;
  1284. /* Init the first iso */
  1285. if (ISO_BUFFER_SIZE >=
  1286. (fifo->usb_packet_maxlen *
  1287. num_packets_per_urb)) {
  1288. fill_isoc_urb(fifo->iso[i].urb,
  1289. fifo->hw->dev, fifo->pipe,
  1290. fifo->iso[i].buffer,
  1291. num_packets_per_urb,
  1292. fifo->usb_packet_maxlen,
  1293. fifo->intervall, complete,
  1294. &fifo->iso[i]);
  1295. memset(fifo->iso[i].buffer, 0,
  1296. sizeof(fifo->iso[i].buffer));
  1297. for (k = 0; k < num_packets_per_urb; k++) {
  1298. fifo->iso[i].urb->
  1299. iso_frame_desc[k].offset =
  1300. k * packet_size;
  1301. fifo->iso[i].urb->
  1302. iso_frame_desc[k].length =
  1303. packet_size;
  1304. }
  1305. } else {
  1306. printk(KERN_DEBUG
  1307. "%s: %s: ISO Buffer size to small!\n",
  1308. hw->name, __func__);
  1309. }
  1310. }
  1311. fifo->bit_line = BITLINE_INF;
  1312. errcode = usb_submit_urb(fifo->iso[i].urb, GFP_KERNEL);
  1313. fifo->active = (errcode >= 0) ? 1 : 0;
  1314. fifo->stop_gracefull = 0;
  1315. if (errcode < 0) {
  1316. printk(KERN_DEBUG "%s: %s: %s URB nr:%d\n",
  1317. hw->name, __func__,
  1318. symbolic(urb_errlist, errcode), i);
  1319. }
  1320. }
  1321. return fifo->active;
  1322. }
  1323. static void
  1324. stop_iso_gracefull(struct usb_fifo *fifo)
  1325. {
  1326. struct hfcsusb *hw = fifo->hw;
  1327. int i, timeout;
  1328. u_long flags;
  1329. for (i = 0; i < 2; i++) {
  1330. spin_lock_irqsave(&hw->lock, flags);
  1331. if (debug)
  1332. printk(KERN_DEBUG "%s: %s for fifo %i.%i\n",
  1333. hw->name, __func__, fifo->fifonum, i);
  1334. fifo->stop_gracefull = 1;
  1335. spin_unlock_irqrestore(&hw->lock, flags);
  1336. }
  1337. for (i = 0; i < 2; i++) {
  1338. timeout = 3;
  1339. while (fifo->stop_gracefull && timeout--)
  1340. schedule_timeout_interruptible((HZ/1000)*16);
  1341. if (debug && fifo->stop_gracefull)
  1342. printk(KERN_DEBUG "%s: ERROR %s for fifo %i.%i\n",
  1343. hw->name, __func__, fifo->fifonum, i);
  1344. }
  1345. }
  1346. static void
  1347. stop_int_gracefull(struct usb_fifo *fifo)
  1348. {
  1349. struct hfcsusb *hw = fifo->hw;
  1350. int timeout;
  1351. u_long flags;
  1352. spin_lock_irqsave(&hw->lock, flags);
  1353. if (debug)
  1354. printk(KERN_DEBUG "%s: %s for fifo %i\n",
  1355. hw->name, __func__, fifo->fifonum);
  1356. fifo->stop_gracefull = 1;
  1357. spin_unlock_irqrestore(&hw->lock, flags);
  1358. timeout = 3;
  1359. while (fifo->stop_gracefull && timeout--)
  1360. schedule_timeout_interruptible((HZ/1000)*3);
  1361. if (debug && fifo->stop_gracefull)
  1362. printk(KERN_DEBUG "%s: ERROR %s for fifo %i\n",
  1363. hw->name, __func__, fifo->fifonum);
  1364. }
  1365. /* start the interrupt transfer for the given fifo */
  1366. static void
  1367. start_int_fifo(struct usb_fifo *fifo)
  1368. {
  1369. struct hfcsusb *hw = fifo->hw;
  1370. int errcode;
  1371. if (debug)
  1372. printk(KERN_DEBUG "%s: %s: INT IN fifo:%d\n",
  1373. hw->name, __func__, fifo->fifonum);
  1374. if (!fifo->urb) {
  1375. fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
  1376. if (!fifo->urb)
  1377. return;
  1378. }
  1379. usb_fill_int_urb(fifo->urb, fifo->hw->dev, fifo->pipe,
  1380. fifo->buffer, fifo->usb_packet_maxlen,
  1381. (usb_complete_t)rx_int_complete, fifo, fifo->intervall);
  1382. fifo->active = 1;
  1383. fifo->stop_gracefull = 0;
  1384. errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
  1385. if (errcode) {
  1386. printk(KERN_DEBUG "%s: %s: submit URB: status:%i\n",
  1387. hw->name, __func__, errcode);
  1388. fifo->active = 0;
  1389. }
  1390. }
  1391. static void
  1392. setPortMode(struct hfcsusb *hw)
  1393. {
  1394. if (debug & DEBUG_HW)
  1395. printk(KERN_DEBUG "%s: %s %s\n", hw->name, __func__,
  1396. (hw->protocol == ISDN_P_TE_S0) ? "TE" : "NT");
  1397. if (hw->protocol == ISDN_P_TE_S0) {
  1398. write_reg(hw, HFCUSB_SCTRL, 0x40);
  1399. write_reg(hw, HFCUSB_SCTRL_E, 0x00);
  1400. write_reg(hw, HFCUSB_CLKDEL, CLKDEL_TE);
  1401. write_reg(hw, HFCUSB_STATES, 3 | 0x10);
  1402. write_reg(hw, HFCUSB_STATES, 3);
  1403. } else {
  1404. write_reg(hw, HFCUSB_SCTRL, 0x44);
  1405. write_reg(hw, HFCUSB_SCTRL_E, 0x09);
  1406. write_reg(hw, HFCUSB_CLKDEL, CLKDEL_NT);
  1407. write_reg(hw, HFCUSB_STATES, 1 | 0x10);
  1408. write_reg(hw, HFCUSB_STATES, 1);
  1409. }
  1410. }
  1411. static void
  1412. reset_hfcsusb(struct hfcsusb *hw)
  1413. {
  1414. struct usb_fifo *fifo;
  1415. int i;
  1416. if (debug & DEBUG_HW)
  1417. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  1418. /* do Chip reset */
  1419. write_reg(hw, HFCUSB_CIRM, 8);
  1420. /* aux = output, reset off */
  1421. write_reg(hw, HFCUSB_CIRM, 0x10);
  1422. /* set USB_SIZE to match the wMaxPacketSize for INT or BULK transfers */
  1423. write_reg(hw, HFCUSB_USB_SIZE, (hw->packet_size / 8) |
  1424. ((hw->packet_size / 8) << 4));
  1425. /* set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers */
  1426. write_reg(hw, HFCUSB_USB_SIZE_I, hw->iso_packet_size);
  1427. /* enable PCM/GCI master mode */
  1428. write_reg(hw, HFCUSB_MST_MODE1, 0); /* set default values */
  1429. write_reg(hw, HFCUSB_MST_MODE0, 1); /* enable master mode */
  1430. /* init the fifos */
  1431. write_reg(hw, HFCUSB_F_THRES,
  1432. (HFCUSB_TX_THRESHOLD / 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
  1433. fifo = hw->fifos;
  1434. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1435. write_reg(hw, HFCUSB_FIFO, i); /* select the desired fifo */
  1436. fifo[i].max_size =
  1437. (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
  1438. fifo[i].last_urblen = 0;
  1439. /* set 2 bit for D- & E-channel */
  1440. write_reg(hw, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));
  1441. /* enable all fifos */
  1442. if (i == HFCUSB_D_TX)
  1443. write_reg(hw, HFCUSB_CON_HDLC,
  1444. (hw->protocol == ISDN_P_NT_S0) ? 0x08 : 0x09);
  1445. else
  1446. write_reg(hw, HFCUSB_CON_HDLC, 0x08);
  1447. write_reg(hw, HFCUSB_INC_RES_F, 2); /* reset the fifo */
  1448. }
  1449. write_reg(hw, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
  1450. handle_led(hw, LED_POWER_ON);
  1451. }
  1452. /* start USB data pipes dependand on device's endpoint configuration */
  1453. static void
  1454. hfcsusb_start_endpoint(struct hfcsusb *hw, int channel)
  1455. {
  1456. /* quick check if endpoint already running */
  1457. if ((channel == HFC_CHAN_D) && (hw->fifos[HFCUSB_D_RX].active))
  1458. return;
  1459. if ((channel == HFC_CHAN_B1) && (hw->fifos[HFCUSB_B1_RX].active))
  1460. return;
  1461. if ((channel == HFC_CHAN_B2) && (hw->fifos[HFCUSB_B2_RX].active))
  1462. return;
  1463. if ((channel == HFC_CHAN_E) && (hw->fifos[HFCUSB_PCM_RX].active))
  1464. return;
  1465. /* start rx endpoints using USB INT IN method */
  1466. if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
  1467. start_int_fifo(hw->fifos + channel*2 + 1);
  1468. /* start rx endpoints using USB ISO IN method */
  1469. if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO) {
  1470. switch (channel) {
  1471. case HFC_CHAN_D:
  1472. start_isoc_chain(hw->fifos + HFCUSB_D_RX,
  1473. ISOC_PACKETS_D,
  1474. (usb_complete_t)rx_iso_complete,
  1475. 16);
  1476. break;
  1477. case HFC_CHAN_E:
  1478. start_isoc_chain(hw->fifos + HFCUSB_PCM_RX,
  1479. ISOC_PACKETS_D,
  1480. (usb_complete_t)rx_iso_complete,
  1481. 16);
  1482. break;
  1483. case HFC_CHAN_B1:
  1484. start_isoc_chain(hw->fifos + HFCUSB_B1_RX,
  1485. ISOC_PACKETS_B,
  1486. (usb_complete_t)rx_iso_complete,
  1487. 16);
  1488. break;
  1489. case HFC_CHAN_B2:
  1490. start_isoc_chain(hw->fifos + HFCUSB_B2_RX,
  1491. ISOC_PACKETS_B,
  1492. (usb_complete_t)rx_iso_complete,
  1493. 16);
  1494. break;
  1495. }
  1496. }
  1497. /* start tx endpoints using USB ISO OUT method */
  1498. switch (channel) {
  1499. case HFC_CHAN_D:
  1500. start_isoc_chain(hw->fifos + HFCUSB_D_TX,
  1501. ISOC_PACKETS_B,
  1502. (usb_complete_t)tx_iso_complete, 1);
  1503. break;
  1504. case HFC_CHAN_B1:
  1505. start_isoc_chain(hw->fifos + HFCUSB_B1_TX,
  1506. ISOC_PACKETS_D,
  1507. (usb_complete_t)tx_iso_complete, 1);
  1508. break;
  1509. case HFC_CHAN_B2:
  1510. start_isoc_chain(hw->fifos + HFCUSB_B2_TX,
  1511. ISOC_PACKETS_B,
  1512. (usb_complete_t)tx_iso_complete, 1);
  1513. break;
  1514. }
  1515. }
  1516. /* stop USB data pipes dependand on device's endpoint configuration */
  1517. static void
  1518. hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel)
  1519. {
  1520. /* quick check if endpoint currently running */
  1521. if ((channel == HFC_CHAN_D) && (!hw->fifos[HFCUSB_D_RX].active))
  1522. return;
  1523. if ((channel == HFC_CHAN_B1) && (!hw->fifos[HFCUSB_B1_RX].active))
  1524. return;
  1525. if ((channel == HFC_CHAN_B2) && (!hw->fifos[HFCUSB_B2_RX].active))
  1526. return;
  1527. if ((channel == HFC_CHAN_E) && (!hw->fifos[HFCUSB_PCM_RX].active))
  1528. return;
  1529. /* rx endpoints using USB INT IN method */
  1530. if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
  1531. stop_int_gracefull(hw->fifos + channel*2 + 1);
  1532. /* rx endpoints using USB ISO IN method */
  1533. if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO)
  1534. stop_iso_gracefull(hw->fifos + channel*2 + 1);
  1535. /* tx endpoints using USB ISO OUT method */
  1536. if (channel != HFC_CHAN_E)
  1537. stop_iso_gracefull(hw->fifos + channel*2);
  1538. }
  1539. /* Hardware Initialization */
  1540. static int
  1541. setup_hfcsusb(struct hfcsusb *hw)
  1542. {
  1543. u_char b;
  1544. if (debug & DBG_HFC_CALL_TRACE)
  1545. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  1546. /* check the chip id */
  1547. if (read_reg_atomic(hw, HFCUSB_CHIP_ID, &b) != 1) {
  1548. printk(KERN_DEBUG "%s: %s: cannot read chip id\n",
  1549. hw->name, __func__);
  1550. return 1;
  1551. }
  1552. if (b != HFCUSB_CHIPID) {
  1553. printk(KERN_DEBUG "%s: %s: Invalid chip id 0x%02x\n",
  1554. hw->name, __func__, b);
  1555. return 1;
  1556. }
  1557. /* first set the needed config, interface and alternate */
  1558. (void) usb_set_interface(hw->dev, hw->if_used, hw->alt_used);
  1559. hw->led_state = 0;
  1560. /* init the background machinery for control requests */
  1561. hw->ctrl_read.bRequestType = 0xc0;
  1562. hw->ctrl_read.bRequest = 1;
  1563. hw->ctrl_read.wLength = cpu_to_le16(1);
  1564. hw->ctrl_write.bRequestType = 0x40;
  1565. hw->ctrl_write.bRequest = 0;
  1566. hw->ctrl_write.wLength = 0;
  1567. usb_fill_control_urb(hw->ctrl_urb, hw->dev, hw->ctrl_out_pipe,
  1568. (u_char *)&hw->ctrl_write, NULL, 0,
  1569. (usb_complete_t)ctrl_complete, hw);
  1570. reset_hfcsusb(hw);
  1571. return 0;
  1572. }
  1573. static void
  1574. release_hw(struct hfcsusb *hw)
  1575. {
  1576. if (debug & DBG_HFC_CALL_TRACE)
  1577. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  1578. /*
  1579. * stop all endpoints gracefully
  1580. * TODO: mISDN_core should generate CLOSE_CHANNEL
  1581. * signals after calling mISDN_unregister_device()
  1582. */
  1583. hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
  1584. hfcsusb_stop_endpoint(hw, HFC_CHAN_B1);
  1585. hfcsusb_stop_endpoint(hw, HFC_CHAN_B2);
  1586. if (hw->fifos[HFCUSB_PCM_RX].pipe)
  1587. hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
  1588. if (hw->protocol == ISDN_P_TE_S0)
  1589. l1_event(hw->dch.l1, CLOSE_CHANNEL);
  1590. mISDN_unregister_device(&hw->dch.dev);
  1591. mISDN_freebchannel(&hw->bch[1]);
  1592. mISDN_freebchannel(&hw->bch[0]);
  1593. mISDN_freedchannel(&hw->dch);
  1594. if (hw->ctrl_urb) {
  1595. usb_kill_urb(hw->ctrl_urb);
  1596. usb_free_urb(hw->ctrl_urb);
  1597. hw->ctrl_urb = NULL;
  1598. }
  1599. if (hw->intf)
  1600. usb_set_intfdata(hw->intf, NULL);
  1601. list_del(&hw->list);
  1602. kfree(hw);
  1603. hw = NULL;
  1604. }
  1605. static void
  1606. deactivate_bchannel(struct bchannel *bch)
  1607. {
  1608. struct hfcsusb *hw = bch->hw;
  1609. u_long flags;
  1610. if (bch->debug & DEBUG_HW)
  1611. printk(KERN_DEBUG "%s: %s: bch->nr(%i)\n",
  1612. hw->name, __func__, bch->nr);
  1613. spin_lock_irqsave(&hw->lock, flags);
  1614. mISDN_clear_bchannel(bch);
  1615. spin_unlock_irqrestore(&hw->lock, flags);
  1616. hfcsusb_setup_bch(bch, ISDN_P_NONE);
  1617. hfcsusb_stop_endpoint(hw, bch->nr);
  1618. }
  1619. /*
  1620. * Layer 1 B-channel hardware access
  1621. */
  1622. static int
  1623. hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1624. {
  1625. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  1626. int ret = -EINVAL;
  1627. if (bch->debug & DEBUG_HW)
  1628. printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
  1629. switch (cmd) {
  1630. case HW_TESTRX_RAW:
  1631. case HW_TESTRX_HDLC:
  1632. case HW_TESTRX_OFF:
  1633. ret = -EINVAL;
  1634. break;
  1635. case CLOSE_CHANNEL:
  1636. test_and_clear_bit(FLG_OPEN, &bch->Flags);
  1637. if (test_bit(FLG_ACTIVE, &bch->Flags))
  1638. deactivate_bchannel(bch);
  1639. ch->protocol = ISDN_P_NONE;
  1640. ch->peer = NULL;
  1641. module_put(THIS_MODULE);
  1642. ret = 0;
  1643. break;
  1644. case CONTROL_CHANNEL:
  1645. ret = channel_bctrl(bch, arg);
  1646. break;
  1647. default:
  1648. printk(KERN_WARNING "%s: unknown prim(%x)\n",
  1649. __func__, cmd);
  1650. }
  1651. return ret;
  1652. }
  1653. static int
  1654. setup_instance(struct hfcsusb *hw, struct device *parent)
  1655. {
  1656. u_long flags;
  1657. int err, i;
  1658. if (debug & DBG_HFC_CALL_TRACE)
  1659. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  1660. spin_lock_init(&hw->ctrl_lock);
  1661. spin_lock_init(&hw->lock);
  1662. mISDN_initdchannel(&hw->dch, MAX_DFRAME_LEN_L1, ph_state);
  1663. hw->dch.debug = debug & 0xFFFF;
  1664. hw->dch.hw = hw;
  1665. hw->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
  1666. hw->dch.dev.D.send = hfcusb_l2l1D;
  1667. hw->dch.dev.D.ctrl = hfc_dctrl;
  1668. /* enable E-Channel logging */
  1669. if (hw->fifos[HFCUSB_PCM_RX].pipe)
  1670. mISDN_initdchannel(&hw->ech, MAX_DFRAME_LEN_L1, NULL);
  1671. hw->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
  1672. (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
  1673. hw->dch.dev.nrbchan = 2;
  1674. for (i = 0; i < 2; i++) {
  1675. hw->bch[i].nr = i + 1;
  1676. set_channelmap(i + 1, hw->dch.dev.channelmap);
  1677. hw->bch[i].debug = debug;
  1678. mISDN_initbchannel(&hw->bch[i], MAX_DATA_MEM);
  1679. hw->bch[i].hw = hw;
  1680. hw->bch[i].ch.send = hfcusb_l2l1B;
  1681. hw->bch[i].ch.ctrl = hfc_bctrl;
  1682. hw->bch[i].ch.nr = i + 1;
  1683. list_add(&hw->bch[i].ch.list, &hw->dch.dev.bchannels);
  1684. }
  1685. hw->fifos[HFCUSB_B1_TX].bch = &hw->bch[0];
  1686. hw->fifos[HFCUSB_B1_RX].bch = &hw->bch[0];
  1687. hw->fifos[HFCUSB_B2_TX].bch = &hw->bch[1];
  1688. hw->fifos[HFCUSB_B2_RX].bch = &hw->bch[1];
  1689. hw->fifos[HFCUSB_D_TX].dch = &hw->dch;
  1690. hw->fifos[HFCUSB_D_RX].dch = &hw->dch;
  1691. hw->fifos[HFCUSB_PCM_RX].ech = &hw->ech;
  1692. hw->fifos[HFCUSB_PCM_TX].ech = &hw->ech;
  1693. err = setup_hfcsusb(hw);
  1694. if (err)
  1695. goto out;
  1696. snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s.%d", DRIVER_NAME,
  1697. hfcsusb_cnt + 1);
  1698. printk(KERN_INFO "%s: registered as '%s'\n",
  1699. DRIVER_NAME, hw->name);
  1700. err = mISDN_register_device(&hw->dch.dev, parent, hw->name);
  1701. if (err)
  1702. goto out;
  1703. hfcsusb_cnt++;
  1704. write_lock_irqsave(&HFClock, flags);
  1705. list_add_tail(&hw->list, &HFClist);
  1706. write_unlock_irqrestore(&HFClock, flags);
  1707. return 0;
  1708. out:
  1709. mISDN_freebchannel(&hw->bch[1]);
  1710. mISDN_freebchannel(&hw->bch[0]);
  1711. mISDN_freedchannel(&hw->dch);
  1712. kfree(hw);
  1713. return err;
  1714. }
  1715. static int
  1716. hfcsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1717. {
  1718. struct hfcsusb *hw;
  1719. struct usb_device *dev = interface_to_usbdev(intf);
  1720. struct usb_host_interface *iface = intf->cur_altsetting;
  1721. struct usb_host_interface *iface_used = NULL;
  1722. struct usb_host_endpoint *ep;
  1723. struct hfcsusb_vdata *driver_info;
  1724. int ifnum = iface->desc.bInterfaceNumber, i, idx, alt_idx,
  1725. probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found,
  1726. ep_addr, cmptbl[16], small_match, iso_packet_size, packet_size,
  1727. alt_used = 0;
  1728. vend_idx = 0xffff;
  1729. for (i = 0; hfcsusb_idtab[i].idVendor; i++) {
  1730. if ((le16_to_cpu(dev->descriptor.idVendor)
  1731. == hfcsusb_idtab[i].idVendor) &&
  1732. (le16_to_cpu(dev->descriptor.idProduct)
  1733. == hfcsusb_idtab[i].idProduct)) {
  1734. vend_idx = i;
  1735. continue;
  1736. }
  1737. }
  1738. printk(KERN_DEBUG
  1739. "%s: interface(%d) actalt(%d) minor(%d) vend_idx(%d)\n",
  1740. __func__, ifnum, iface->desc.bAlternateSetting,
  1741. intf->minor, vend_idx);
  1742. if (vend_idx == 0xffff) {
  1743. printk(KERN_WARNING
  1744. "%s: no valid vendor found in USB descriptor\n",
  1745. __func__);
  1746. return -EIO;
  1747. }
  1748. /* if vendor and product ID is OK, start probing alternate settings */
  1749. alt_idx = 0;
  1750. small_match = -1;
  1751. /* default settings */
  1752. iso_packet_size = 16;
  1753. packet_size = 64;
  1754. while (alt_idx < intf->num_altsetting) {
  1755. iface = intf->altsetting + alt_idx;
  1756. probe_alt_setting = iface->desc.bAlternateSetting;
  1757. cfg_used = 0;
  1758. while (validconf[cfg_used][0]) {
  1759. cfg_found = 1;
  1760. vcf = validconf[cfg_used];
  1761. ep = iface->endpoint;
  1762. memcpy(cmptbl, vcf, 16 * sizeof(int));
  1763. /* check for all endpoints in this alternate setting */
  1764. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1765. ep_addr = ep->desc.bEndpointAddress;
  1766. /* get endpoint base */
  1767. idx = ((ep_addr & 0x7f) - 1) * 2;
  1768. if (ep_addr & 0x80)
  1769. idx++;
  1770. attr = ep->desc.bmAttributes;
  1771. if (cmptbl[idx] != EP_NOP) {
  1772. if (cmptbl[idx] == EP_NUL)
  1773. cfg_found = 0;
  1774. if (attr == USB_ENDPOINT_XFER_INT
  1775. && cmptbl[idx] == EP_INT)
  1776. cmptbl[idx] = EP_NUL;
  1777. if (attr == USB_ENDPOINT_XFER_BULK
  1778. && cmptbl[idx] == EP_BLK)
  1779. cmptbl[idx] = EP_NUL;
  1780. if (attr == USB_ENDPOINT_XFER_ISOC
  1781. && cmptbl[idx] == EP_ISO)
  1782. cmptbl[idx] = EP_NUL;
  1783. if (attr == USB_ENDPOINT_XFER_INT &&
  1784. ep->desc.bInterval < vcf[17]) {
  1785. cfg_found = 0;
  1786. }
  1787. }
  1788. ep++;
  1789. }
  1790. for (i = 0; i < 16; i++)
  1791. if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
  1792. cfg_found = 0;
  1793. if (cfg_found) {
  1794. if (small_match < cfg_used) {
  1795. small_match = cfg_used;
  1796. alt_used = probe_alt_setting;
  1797. iface_used = iface;
  1798. }
  1799. }
  1800. cfg_used++;
  1801. }
  1802. alt_idx++;
  1803. } /* (alt_idx < intf->num_altsetting) */
  1804. /* not found a valid USB Ta Endpoint config */
  1805. if (small_match == -1)
  1806. return -EIO;
  1807. iface = iface_used;
  1808. hw = kzalloc(sizeof(struct hfcsusb), GFP_KERNEL);
  1809. if (!hw)
  1810. return -ENOMEM; /* got no mem */
  1811. snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s", DRIVER_NAME);
  1812. ep = iface->endpoint;
  1813. vcf = validconf[small_match];
  1814. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1815. struct usb_fifo *f;
  1816. ep_addr = ep->desc.bEndpointAddress;
  1817. /* get endpoint base */
  1818. idx = ((ep_addr & 0x7f) - 1) * 2;
  1819. if (ep_addr & 0x80)
  1820. idx++;
  1821. f = &hw->fifos[idx & 7];
  1822. /* init Endpoints */
  1823. if (vcf[idx] == EP_NOP || vcf[idx] == EP_NUL) {
  1824. ep++;
  1825. continue;
  1826. }
  1827. switch (ep->desc.bmAttributes) {
  1828. case USB_ENDPOINT_XFER_INT:
  1829. f->pipe = usb_rcvintpipe(dev,
  1830. ep->desc.bEndpointAddress);
  1831. f->usb_transfer_mode = USB_INT;
  1832. packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  1833. break;
  1834. case USB_ENDPOINT_XFER_BULK:
  1835. if (ep_addr & 0x80)
  1836. f->pipe = usb_rcvbulkpipe(dev,
  1837. ep->desc.bEndpointAddress);
  1838. else
  1839. f->pipe = usb_sndbulkpipe(dev,
  1840. ep->desc.bEndpointAddress);
  1841. f->usb_transfer_mode = USB_BULK;
  1842. packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  1843. break;
  1844. case USB_ENDPOINT_XFER_ISOC:
  1845. if (ep_addr & 0x80)
  1846. f->pipe = usb_rcvisocpipe(dev,
  1847. ep->desc.bEndpointAddress);
  1848. else
  1849. f->pipe = usb_sndisocpipe(dev,
  1850. ep->desc.bEndpointAddress);
  1851. f->usb_transfer_mode = USB_ISOC;
  1852. iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  1853. break;
  1854. default:
  1855. f->pipe = 0;
  1856. }
  1857. if (f->pipe) {
  1858. f->fifonum = idx & 7;
  1859. f->hw = hw;
  1860. f->usb_packet_maxlen =
  1861. le16_to_cpu(ep->desc.wMaxPacketSize);
  1862. f->intervall = ep->desc.bInterval;
  1863. }
  1864. ep++;
  1865. }
  1866. hw->dev = dev; /* save device */
  1867. hw->if_used = ifnum; /* save used interface */
  1868. hw->alt_used = alt_used; /* and alternate config */
  1869. hw->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
  1870. hw->cfg_used = vcf[16]; /* store used config */
  1871. hw->vend_idx = vend_idx; /* store found vendor */
  1872. hw->packet_size = packet_size;
  1873. hw->iso_packet_size = iso_packet_size;
  1874. /* create the control pipes needed for register access */
  1875. hw->ctrl_in_pipe = usb_rcvctrlpipe(hw->dev, 0);
  1876. hw->ctrl_out_pipe = usb_sndctrlpipe(hw->dev, 0);
  1877. hw->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  1878. driver_info =
  1879. (struct hfcsusb_vdata *)hfcsusb_idtab[vend_idx].driver_info;
  1880. printk(KERN_DEBUG "%s: %s: detected \"%s\" (%s, if=%d alt=%d)\n",
  1881. hw->name, __func__, driver_info->vend_name,
  1882. conf_str[small_match], ifnum, alt_used);
  1883. if (setup_instance(hw, dev->dev.parent))
  1884. return -EIO;
  1885. hw->intf = intf;
  1886. usb_set_intfdata(hw->intf, hw);
  1887. return 0;
  1888. }
  1889. /* function called when an active device is removed */
  1890. static void
  1891. hfcsusb_disconnect(struct usb_interface *intf)
  1892. {
  1893. struct hfcsusb *hw = usb_get_intfdata(intf);
  1894. struct hfcsusb *next;
  1895. int cnt = 0;
  1896. printk(KERN_INFO "%s: device disconnected\n", hw->name);
  1897. handle_led(hw, LED_POWER_OFF);
  1898. release_hw(hw);
  1899. list_for_each_entry_safe(hw, next, &HFClist, list)
  1900. cnt++;
  1901. if (!cnt)
  1902. hfcsusb_cnt = 0;
  1903. usb_set_intfdata(intf, NULL);
  1904. }
  1905. static struct usb_driver hfcsusb_drv = {
  1906. .name = DRIVER_NAME,
  1907. .id_table = hfcsusb_idtab,
  1908. .probe = hfcsusb_probe,
  1909. .disconnect = hfcsusb_disconnect,
  1910. };
  1911. static int __init
  1912. hfcsusb_init(void)
  1913. {
  1914. printk(KERN_INFO DRIVER_NAME " driver Rev. %s debug(0x%x) poll(%i)\n",
  1915. hfcsusb_rev, debug, poll);
  1916. if (usb_register(&hfcsusb_drv)) {
  1917. printk(KERN_INFO DRIVER_NAME
  1918. ": Unable to register hfcsusb module at usb stack\n");
  1919. return -ENODEV;
  1920. }
  1921. return 0;
  1922. }
  1923. static void __exit
  1924. hfcsusb_cleanup(void)
  1925. {
  1926. if (debug & DBG_HFC_CALL_TRACE)
  1927. printk(KERN_INFO DRIVER_NAME ": %s\n", __func__);
  1928. /* unregister Hardware */
  1929. usb_deregister(&hfcsusb_drv); /* release our driver */
  1930. }
  1931. module_init(hfcsusb_init);
  1932. module_exit(hfcsusb_cleanup);