hfcsusb.c 54 KB

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