hfcsusb.c 55 KB

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