hfcsusb.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140
  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. if (debug & DBG_HFC_CALL_TRACE)
  742. printk(KERN_DEBUG "%s: %s: fifo(%i) len(%i) "
  743. "dch(%p) bch(%p) ech(%p)\n",
  744. hw->name, __func__, fifon, len,
  745. fifo->dch, fifo->bch, fifo->ech);
  746. if (!len)
  747. return;
  748. if ((!!fifo->dch + !!fifo->bch + !!fifo->ech) != 1) {
  749. printk(KERN_DEBUG "%s: %s: undefined channel\n",
  750. hw->name, __func__);
  751. return;
  752. }
  753. spin_lock(&hw->lock);
  754. if (fifo->dch) {
  755. rx_skb = fifo->dch->rx_skb;
  756. maxlen = fifo->dch->maxlen;
  757. hdlc = 1;
  758. }
  759. if (fifo->bch) {
  760. if (test_bit(FLG_RX_OFF, &fifo->bch->Flags)) {
  761. fifo->bch->dropcnt += len;
  762. spin_unlock(&hw->lock);
  763. return;
  764. }
  765. maxlen = bchannel_get_rxbuf(fifo->bch, len);
  766. rx_skb = fifo->bch->rx_skb;
  767. if (maxlen < 0) {
  768. if (rx_skb)
  769. skb_trim(rx_skb, 0);
  770. pr_warning("%s.B%d: No bufferspace for %d bytes\n",
  771. hw->name, fifo->bch->nr, len);
  772. spin_unlock(&hw->lock);
  773. return;
  774. }
  775. maxlen = fifo->bch->maxlen;
  776. hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
  777. }
  778. if (fifo->ech) {
  779. rx_skb = fifo->ech->rx_skb;
  780. maxlen = fifo->ech->maxlen;
  781. hdlc = 1;
  782. }
  783. if (fifo->dch || fifo->ech) {
  784. if (!rx_skb) {
  785. rx_skb = mI_alloc_skb(maxlen, GFP_ATOMIC);
  786. if (rx_skb) {
  787. if (fifo->dch)
  788. fifo->dch->rx_skb = rx_skb;
  789. if (fifo->ech)
  790. fifo->ech->rx_skb = rx_skb;
  791. skb_trim(rx_skb, 0);
  792. } else {
  793. printk(KERN_DEBUG "%s: %s: No mem for rx_skb\n",
  794. hw->name, __func__);
  795. spin_unlock(&hw->lock);
  796. return;
  797. }
  798. }
  799. /* D/E-Channel SKB range check */
  800. if ((rx_skb->len + len) >= MAX_DFRAME_LEN_L1) {
  801. printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
  802. "for fifo(%d) HFCUSB_D_RX\n",
  803. hw->name, __func__, fifon);
  804. skb_trim(rx_skb, 0);
  805. spin_unlock(&hw->lock);
  806. return;
  807. }
  808. }
  809. memcpy(skb_put(rx_skb, len), data, len);
  810. if (hdlc) {
  811. /* we have a complete hdlc packet */
  812. if (finish) {
  813. if ((rx_skb->len > 3) &&
  814. (!(rx_skb->data[rx_skb->len - 1]))) {
  815. if (debug & DBG_HFC_FIFO_VERBOSE) {
  816. printk(KERN_DEBUG "%s: %s: fifon(%i)"
  817. " new RX len(%i): ",
  818. hw->name, __func__, fifon,
  819. rx_skb->len);
  820. i = 0;
  821. while (i < rx_skb->len)
  822. printk("%02x ",
  823. rx_skb->data[i++]);
  824. printk("\n");
  825. }
  826. /* remove CRC & status */
  827. skb_trim(rx_skb, rx_skb->len - 3);
  828. if (fifo->dch)
  829. recv_Dchannel(fifo->dch);
  830. if (fifo->bch)
  831. recv_Bchannel(fifo->bch, MISDN_ID_ANY,
  832. 0);
  833. if (fifo->ech)
  834. recv_Echannel(fifo->ech,
  835. &hw->dch);
  836. } else {
  837. if (debug & DBG_HFC_FIFO_VERBOSE) {
  838. printk(KERN_DEBUG
  839. "%s: CRC or minlen ERROR fifon(%i) "
  840. "RX len(%i): ",
  841. hw->name, fifon, rx_skb->len);
  842. i = 0;
  843. while (i < rx_skb->len)
  844. printk("%02x ",
  845. rx_skb->data[i++]);
  846. printk("\n");
  847. }
  848. skb_trim(rx_skb, 0);
  849. }
  850. }
  851. } else {
  852. /* deliver transparent data to layer2 */
  853. recv_Bchannel(fifo->bch, MISDN_ID_ANY, false);
  854. }
  855. spin_unlock(&hw->lock);
  856. }
  857. static void
  858. fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
  859. void *buf, int num_packets, int packet_size, int interval,
  860. usb_complete_t complete, void *context)
  861. {
  862. int k;
  863. usb_fill_bulk_urb(urb, dev, pipe, buf, packet_size * num_packets,
  864. complete, context);
  865. urb->number_of_packets = num_packets;
  866. urb->transfer_flags = URB_ISO_ASAP;
  867. urb->actual_length = 0;
  868. urb->interval = interval;
  869. for (k = 0; k < num_packets; k++) {
  870. urb->iso_frame_desc[k].offset = packet_size * k;
  871. urb->iso_frame_desc[k].length = packet_size;
  872. urb->iso_frame_desc[k].actual_length = 0;
  873. }
  874. }
  875. /* receive completion routine for all ISO tx fifos */
  876. static void
  877. rx_iso_complete(struct urb *urb)
  878. {
  879. struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
  880. struct usb_fifo *fifo = context_iso_urb->owner_fifo;
  881. struct hfcsusb *hw = fifo->hw;
  882. int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
  883. status, iso_status, i;
  884. __u8 *buf;
  885. static __u8 eof[8];
  886. __u8 s0_state;
  887. fifon = fifo->fifonum;
  888. status = urb->status;
  889. spin_lock(&hw->lock);
  890. if (fifo->stop_gracefull) {
  891. fifo->stop_gracefull = 0;
  892. fifo->active = 0;
  893. spin_unlock(&hw->lock);
  894. return;
  895. }
  896. spin_unlock(&hw->lock);
  897. /*
  898. * ISO transfer only partially completed,
  899. * look at individual frame status for details
  900. */
  901. if (status == -EXDEV) {
  902. if (debug & DEBUG_HW)
  903. printk(KERN_DEBUG "%s: %s: with -EXDEV "
  904. "urb->status %d, fifonum %d\n",
  905. hw->name, __func__, status, fifon);
  906. /* clear status, so go on with ISO transfers */
  907. status = 0;
  908. }
  909. s0_state = 0;
  910. if (fifo->active && !status) {
  911. num_isoc_packets = iso_packets[fifon];
  912. maxlen = fifo->usb_packet_maxlen;
  913. for (k = 0; k < num_isoc_packets; ++k) {
  914. len = urb->iso_frame_desc[k].actual_length;
  915. offset = urb->iso_frame_desc[k].offset;
  916. buf = context_iso_urb->buffer + offset;
  917. iso_status = urb->iso_frame_desc[k].status;
  918. if (iso_status && (debug & DBG_HFC_FIFO_VERBOSE)) {
  919. printk(KERN_DEBUG "%s: %s: "
  920. "ISO packet %i, status: %i\n",
  921. hw->name, __func__, k, iso_status);
  922. }
  923. /* USB data log for every D ISO in */
  924. if ((fifon == HFCUSB_D_RX) &&
  925. (debug & DBG_HFC_USB_VERBOSE)) {
  926. printk(KERN_DEBUG
  927. "%s: %s: %d (%d/%d) len(%d) ",
  928. hw->name, __func__, urb->start_frame,
  929. k, num_isoc_packets - 1,
  930. len);
  931. for (i = 0; i < len; i++)
  932. printk("%x ", buf[i]);
  933. printk("\n");
  934. }
  935. if (!iso_status) {
  936. if (fifo->last_urblen != maxlen) {
  937. /*
  938. * save fifo fill-level threshold bits
  939. * to use them later in TX ISO URB
  940. * completions
  941. */
  942. hw->threshold_mask = buf[1];
  943. if (fifon == HFCUSB_D_RX)
  944. s0_state = (buf[0] >> 4);
  945. eof[fifon] = buf[0] & 1;
  946. if (len > 2)
  947. hfcsusb_rx_frame(fifo, buf + 2,
  948. len - 2, (len < maxlen)
  949. ? eof[fifon] : 0);
  950. } else
  951. hfcsusb_rx_frame(fifo, buf, len,
  952. (len < maxlen) ?
  953. eof[fifon] : 0);
  954. fifo->last_urblen = len;
  955. }
  956. }
  957. /* signal S0 layer1 state change */
  958. if ((s0_state) && (hw->initdone) &&
  959. (s0_state != hw->dch.state)) {
  960. hw->dch.state = s0_state;
  961. schedule_event(&hw->dch, FLG_PHCHANGE);
  962. }
  963. fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
  964. context_iso_urb->buffer, num_isoc_packets,
  965. fifo->usb_packet_maxlen, fifo->intervall,
  966. (usb_complete_t)rx_iso_complete, urb->context);
  967. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  968. if (errcode < 0) {
  969. if (debug & DEBUG_HW)
  970. printk(KERN_DEBUG "%s: %s: error submitting "
  971. "ISO URB: %d\n",
  972. hw->name, __func__, errcode);
  973. }
  974. } else {
  975. if (status && (debug & DBG_HFC_URB_INFO))
  976. printk(KERN_DEBUG "%s: %s: rx_iso_complete : "
  977. "urb->status %d, fifonum %d\n",
  978. hw->name, __func__, status, fifon);
  979. }
  980. }
  981. /* receive completion routine for all interrupt rx fifos */
  982. static void
  983. rx_int_complete(struct urb *urb)
  984. {
  985. int len, status, i;
  986. __u8 *buf, maxlen, fifon;
  987. struct usb_fifo *fifo = (struct usb_fifo *) urb->context;
  988. struct hfcsusb *hw = fifo->hw;
  989. static __u8 eof[8];
  990. spin_lock(&hw->lock);
  991. if (fifo->stop_gracefull) {
  992. fifo->stop_gracefull = 0;
  993. fifo->active = 0;
  994. spin_unlock(&hw->lock);
  995. return;
  996. }
  997. spin_unlock(&hw->lock);
  998. fifon = fifo->fifonum;
  999. if ((!fifo->active) || (urb->status)) {
  1000. if (debug & DBG_HFC_URB_ERROR)
  1001. printk(KERN_DEBUG
  1002. "%s: %s: RX-Fifo %i is going down (%i)\n",
  1003. hw->name, __func__, fifon, urb->status);
  1004. fifo->urb->interval = 0; /* cancel automatic rescheduling */
  1005. return;
  1006. }
  1007. len = urb->actual_length;
  1008. buf = fifo->buffer;
  1009. maxlen = fifo->usb_packet_maxlen;
  1010. /* USB data log for every D INT in */
  1011. if ((fifon == HFCUSB_D_RX) && (debug & DBG_HFC_USB_VERBOSE)) {
  1012. printk(KERN_DEBUG "%s: %s: D RX INT len(%d) ",
  1013. hw->name, __func__, len);
  1014. for (i = 0; i < len; i++)
  1015. printk("%02x ", buf[i]);
  1016. printk("\n");
  1017. }
  1018. if (fifo->last_urblen != fifo->usb_packet_maxlen) {
  1019. /* the threshold mask is in the 2nd status byte */
  1020. hw->threshold_mask = buf[1];
  1021. /* signal S0 layer1 state change */
  1022. if (hw->initdone && ((buf[0] >> 4) != hw->dch.state)) {
  1023. hw->dch.state = (buf[0] >> 4);
  1024. schedule_event(&hw->dch, FLG_PHCHANGE);
  1025. }
  1026. eof[fifon] = buf[0] & 1;
  1027. /* if we have more than the 2 status bytes -> collect data */
  1028. if (len > 2)
  1029. hfcsusb_rx_frame(fifo, buf + 2,
  1030. urb->actual_length - 2,
  1031. (len < maxlen) ? eof[fifon] : 0);
  1032. } else {
  1033. hfcsusb_rx_frame(fifo, buf, urb->actual_length,
  1034. (len < maxlen) ? eof[fifon] : 0);
  1035. }
  1036. fifo->last_urblen = urb->actual_length;
  1037. status = usb_submit_urb(urb, GFP_ATOMIC);
  1038. if (status) {
  1039. if (debug & DEBUG_HW)
  1040. printk(KERN_DEBUG "%s: %s: error resubmitting USB\n",
  1041. hw->name, __func__);
  1042. }
  1043. }
  1044. /* transmit completion routine for all ISO tx fifos */
  1045. static void
  1046. tx_iso_complete(struct urb *urb)
  1047. {
  1048. struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
  1049. struct usb_fifo *fifo = context_iso_urb->owner_fifo;
  1050. struct hfcsusb *hw = fifo->hw;
  1051. struct sk_buff *tx_skb;
  1052. int k, tx_offset, num_isoc_packets, sink, remain, current_len,
  1053. errcode, hdlc, i;
  1054. int *tx_idx;
  1055. int frame_complete, fifon, status, fillempty = 0;
  1056. __u8 threshbit, *p;
  1057. spin_lock(&hw->lock);
  1058. if (fifo->stop_gracefull) {
  1059. fifo->stop_gracefull = 0;
  1060. fifo->active = 0;
  1061. spin_unlock(&hw->lock);
  1062. return;
  1063. }
  1064. if (fifo->dch) {
  1065. tx_skb = fifo->dch->tx_skb;
  1066. tx_idx = &fifo->dch->tx_idx;
  1067. hdlc = 1;
  1068. } else if (fifo->bch) {
  1069. tx_skb = fifo->bch->tx_skb;
  1070. tx_idx = &fifo->bch->tx_idx;
  1071. hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
  1072. if (!tx_skb && !hdlc &&
  1073. test_bit(FLG_FILLEMPTY, &fifo->bch->Flags))
  1074. fillempty = 1;
  1075. } else {
  1076. printk(KERN_DEBUG "%s: %s: neither BCH nor DCH\n",
  1077. hw->name, __func__);
  1078. spin_unlock(&hw->lock);
  1079. return;
  1080. }
  1081. fifon = fifo->fifonum;
  1082. status = urb->status;
  1083. tx_offset = 0;
  1084. /*
  1085. * ISO transfer only partially completed,
  1086. * look at individual frame status for details
  1087. */
  1088. if (status == -EXDEV) {
  1089. if (debug & DBG_HFC_URB_ERROR)
  1090. printk(KERN_DEBUG "%s: %s: "
  1091. "-EXDEV (%i) fifon (%d)\n",
  1092. hw->name, __func__, status, fifon);
  1093. /* clear status, so go on with ISO transfers */
  1094. status = 0;
  1095. }
  1096. if (fifo->active && !status) {
  1097. /* is FifoFull-threshold set for our channel? */
  1098. threshbit = (hw->threshold_mask & (1 << fifon));
  1099. num_isoc_packets = iso_packets[fifon];
  1100. /* predict dataflow to avoid fifo overflow */
  1101. if (fifon >= HFCUSB_D_TX)
  1102. sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
  1103. else
  1104. sink = (threshbit) ? SINK_MIN : SINK_MAX;
  1105. fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
  1106. context_iso_urb->buffer, num_isoc_packets,
  1107. fifo->usb_packet_maxlen, fifo->intervall,
  1108. (usb_complete_t)tx_iso_complete, urb->context);
  1109. memset(context_iso_urb->buffer, 0,
  1110. sizeof(context_iso_urb->buffer));
  1111. frame_complete = 0;
  1112. for (k = 0; k < num_isoc_packets; ++k) {
  1113. /* analyze tx success of previous ISO packets */
  1114. if (debug & DBG_HFC_URB_ERROR) {
  1115. errcode = urb->iso_frame_desc[k].status;
  1116. if (errcode) {
  1117. printk(KERN_DEBUG "%s: %s: "
  1118. "ISO packet %i, status: %i\n",
  1119. hw->name, __func__, k, errcode);
  1120. }
  1121. }
  1122. /* Generate next ISO Packets */
  1123. if (tx_skb)
  1124. remain = tx_skb->len - *tx_idx;
  1125. else if (fillempty)
  1126. remain = 15; /* > not complete */
  1127. else
  1128. remain = 0;
  1129. if (remain > 0) {
  1130. fifo->bit_line -= sink;
  1131. current_len = (0 - fifo->bit_line) / 8;
  1132. if (current_len > 14)
  1133. current_len = 14;
  1134. if (current_len < 0)
  1135. current_len = 0;
  1136. if (remain < current_len)
  1137. current_len = remain;
  1138. /* how much bit do we put on the line? */
  1139. fifo->bit_line += current_len * 8;
  1140. context_iso_urb->buffer[tx_offset] = 0;
  1141. if (current_len == remain) {
  1142. if (hdlc) {
  1143. /* signal frame completion */
  1144. context_iso_urb->
  1145. buffer[tx_offset] = 1;
  1146. /* add 2 byte flags and 16bit
  1147. * CRC at end of ISDN frame */
  1148. fifo->bit_line += 32;
  1149. }
  1150. frame_complete = 1;
  1151. }
  1152. /* copy tx data to iso-urb buffer */
  1153. p = context_iso_urb->buffer + tx_offset + 1;
  1154. if (fillempty) {
  1155. memset(p, fifo->bch->fill[0],
  1156. current_len);
  1157. } else {
  1158. memcpy(p, (tx_skb->data + *tx_idx),
  1159. current_len);
  1160. *tx_idx += current_len;
  1161. }
  1162. urb->iso_frame_desc[k].offset = tx_offset;
  1163. urb->iso_frame_desc[k].length = current_len + 1;
  1164. /* USB data log for every D ISO out */
  1165. if ((fifon == HFCUSB_D_RX) && !fillempty &&
  1166. (debug & DBG_HFC_USB_VERBOSE)) {
  1167. printk(KERN_DEBUG
  1168. "%s: %s (%d/%d) offs(%d) len(%d) ",
  1169. hw->name, __func__,
  1170. k, num_isoc_packets - 1,
  1171. urb->iso_frame_desc[k].offset,
  1172. urb->iso_frame_desc[k].length);
  1173. for (i = urb->iso_frame_desc[k].offset;
  1174. i < (urb->iso_frame_desc[k].offset
  1175. + urb->iso_frame_desc[k].length);
  1176. i++)
  1177. printk("%x ",
  1178. context_iso_urb->buffer[i]);
  1179. printk(" skb->len(%i) tx-idx(%d)\n",
  1180. tx_skb->len, *tx_idx);
  1181. }
  1182. tx_offset += (current_len + 1);
  1183. } else {
  1184. urb->iso_frame_desc[k].offset = tx_offset++;
  1185. urb->iso_frame_desc[k].length = 1;
  1186. /* we lower data margin every msec */
  1187. fifo->bit_line -= sink;
  1188. if (fifo->bit_line < BITLINE_INF)
  1189. fifo->bit_line = BITLINE_INF;
  1190. }
  1191. if (frame_complete) {
  1192. frame_complete = 0;
  1193. if (debug & DBG_HFC_FIFO_VERBOSE) {
  1194. printk(KERN_DEBUG "%s: %s: "
  1195. "fifon(%i) new TX len(%i): ",
  1196. hw->name, __func__,
  1197. fifon, tx_skb->len);
  1198. i = 0;
  1199. while (i < tx_skb->len)
  1200. printk("%02x ",
  1201. tx_skb->data[i++]);
  1202. printk("\n");
  1203. }
  1204. dev_kfree_skb(tx_skb);
  1205. tx_skb = NULL;
  1206. if (fifo->dch && get_next_dframe(fifo->dch))
  1207. tx_skb = fifo->dch->tx_skb;
  1208. else if (fifo->bch &&
  1209. get_next_bframe(fifo->bch))
  1210. tx_skb = fifo->bch->tx_skb;
  1211. }
  1212. }
  1213. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  1214. if (errcode < 0) {
  1215. if (debug & DEBUG_HW)
  1216. printk(KERN_DEBUG
  1217. "%s: %s: error submitting ISO URB: %d \n",
  1218. hw->name, __func__, errcode);
  1219. }
  1220. /*
  1221. * abuse DChannel tx iso completion to trigger NT mode state
  1222. * changes tx_iso_complete is assumed to be called every
  1223. * fifo->intervall (ms)
  1224. */
  1225. if ((fifon == HFCUSB_D_TX) && (hw->protocol == ISDN_P_NT_S0)
  1226. && (hw->timers & NT_ACTIVATION_TIMER)) {
  1227. if ((--hw->nt_timer) < 0)
  1228. schedule_event(&hw->dch, FLG_PHCHANGE);
  1229. }
  1230. } else {
  1231. if (status && (debug & DBG_HFC_URB_ERROR))
  1232. printk(KERN_DEBUG "%s: %s: urb->status %s (%i)"
  1233. "fifonum=%d\n",
  1234. hw->name, __func__,
  1235. symbolic(urb_errlist, status), status, fifon);
  1236. }
  1237. spin_unlock(&hw->lock);
  1238. }
  1239. /*
  1240. * allocs urbs and start isoc transfer with two pending urbs to avoid
  1241. * gaps in the transfer chain
  1242. */
  1243. static int
  1244. start_isoc_chain(struct usb_fifo *fifo, int num_packets_per_urb,
  1245. usb_complete_t complete, int packet_size)
  1246. {
  1247. struct hfcsusb *hw = fifo->hw;
  1248. int i, k, errcode;
  1249. if (debug)
  1250. printk(KERN_DEBUG "%s: %s: fifo %i\n",
  1251. hw->name, __func__, fifo->fifonum);
  1252. /* allocate Memory for Iso out Urbs */
  1253. for (i = 0; i < 2; i++) {
  1254. if (!(fifo->iso[i].urb)) {
  1255. fifo->iso[i].urb =
  1256. usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
  1257. if (!(fifo->iso[i].urb)) {
  1258. printk(KERN_DEBUG
  1259. "%s: %s: alloc urb for fifo %i failed",
  1260. hw->name, __func__, fifo->fifonum);
  1261. }
  1262. fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
  1263. fifo->iso[i].indx = i;
  1264. /* Init the first iso */
  1265. if (ISO_BUFFER_SIZE >=
  1266. (fifo->usb_packet_maxlen *
  1267. num_packets_per_urb)) {
  1268. fill_isoc_urb(fifo->iso[i].urb,
  1269. fifo->hw->dev, fifo->pipe,
  1270. fifo->iso[i].buffer,
  1271. num_packets_per_urb,
  1272. fifo->usb_packet_maxlen,
  1273. fifo->intervall, complete,
  1274. &fifo->iso[i]);
  1275. memset(fifo->iso[i].buffer, 0,
  1276. sizeof(fifo->iso[i].buffer));
  1277. for (k = 0; k < num_packets_per_urb; k++) {
  1278. fifo->iso[i].urb->
  1279. iso_frame_desc[k].offset =
  1280. k * packet_size;
  1281. fifo->iso[i].urb->
  1282. iso_frame_desc[k].length =
  1283. packet_size;
  1284. }
  1285. } else {
  1286. printk(KERN_DEBUG
  1287. "%s: %s: ISO Buffer size to small!\n",
  1288. hw->name, __func__);
  1289. }
  1290. }
  1291. fifo->bit_line = BITLINE_INF;
  1292. errcode = usb_submit_urb(fifo->iso[i].urb, GFP_KERNEL);
  1293. fifo->active = (errcode >= 0) ? 1 : 0;
  1294. fifo->stop_gracefull = 0;
  1295. if (errcode < 0) {
  1296. printk(KERN_DEBUG "%s: %s: %s URB nr:%d\n",
  1297. hw->name, __func__,
  1298. symbolic(urb_errlist, errcode), i);
  1299. }
  1300. }
  1301. return fifo->active;
  1302. }
  1303. static void
  1304. stop_iso_gracefull(struct usb_fifo *fifo)
  1305. {
  1306. struct hfcsusb *hw = fifo->hw;
  1307. int i, timeout;
  1308. u_long flags;
  1309. for (i = 0; i < 2; i++) {
  1310. spin_lock_irqsave(&hw->lock, flags);
  1311. if (debug)
  1312. printk(KERN_DEBUG "%s: %s for fifo %i.%i\n",
  1313. hw->name, __func__, fifo->fifonum, i);
  1314. fifo->stop_gracefull = 1;
  1315. spin_unlock_irqrestore(&hw->lock, flags);
  1316. }
  1317. for (i = 0; i < 2; i++) {
  1318. timeout = 3;
  1319. while (fifo->stop_gracefull && timeout--)
  1320. schedule_timeout_interruptible((HZ / 1000) * 16);
  1321. if (debug && fifo->stop_gracefull)
  1322. printk(KERN_DEBUG "%s: ERROR %s for fifo %i.%i\n",
  1323. hw->name, __func__, fifo->fifonum, i);
  1324. }
  1325. }
  1326. static void
  1327. stop_int_gracefull(struct usb_fifo *fifo)
  1328. {
  1329. struct hfcsusb *hw = fifo->hw;
  1330. int timeout;
  1331. u_long flags;
  1332. spin_lock_irqsave(&hw->lock, flags);
  1333. if (debug)
  1334. printk(KERN_DEBUG "%s: %s for fifo %i\n",
  1335. hw->name, __func__, fifo->fifonum);
  1336. fifo->stop_gracefull = 1;
  1337. spin_unlock_irqrestore(&hw->lock, flags);
  1338. timeout = 3;
  1339. while (fifo->stop_gracefull && timeout--)
  1340. schedule_timeout_interruptible((HZ / 1000) * 3);
  1341. if (debug && fifo->stop_gracefull)
  1342. printk(KERN_DEBUG "%s: ERROR %s for fifo %i\n",
  1343. hw->name, __func__, fifo->fifonum);
  1344. }
  1345. /* start the interrupt transfer for the given fifo */
  1346. static void
  1347. start_int_fifo(struct usb_fifo *fifo)
  1348. {
  1349. struct hfcsusb *hw = fifo->hw;
  1350. int errcode;
  1351. if (debug)
  1352. printk(KERN_DEBUG "%s: %s: INT IN fifo:%d\n",
  1353. hw->name, __func__, fifo->fifonum);
  1354. if (!fifo->urb) {
  1355. fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
  1356. if (!fifo->urb)
  1357. return;
  1358. }
  1359. usb_fill_int_urb(fifo->urb, fifo->hw->dev, fifo->pipe,
  1360. fifo->buffer, fifo->usb_packet_maxlen,
  1361. (usb_complete_t)rx_int_complete, fifo, fifo->intervall);
  1362. fifo->active = 1;
  1363. fifo->stop_gracefull = 0;
  1364. errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
  1365. if (errcode) {
  1366. printk(KERN_DEBUG "%s: %s: submit URB: status:%i\n",
  1367. hw->name, __func__, errcode);
  1368. fifo->active = 0;
  1369. }
  1370. }
  1371. static void
  1372. setPortMode(struct hfcsusb *hw)
  1373. {
  1374. if (debug & DEBUG_HW)
  1375. printk(KERN_DEBUG "%s: %s %s\n", hw->name, __func__,
  1376. (hw->protocol == ISDN_P_TE_S0) ? "TE" : "NT");
  1377. if (hw->protocol == ISDN_P_TE_S0) {
  1378. write_reg(hw, HFCUSB_SCTRL, 0x40);
  1379. write_reg(hw, HFCUSB_SCTRL_E, 0x00);
  1380. write_reg(hw, HFCUSB_CLKDEL, CLKDEL_TE);
  1381. write_reg(hw, HFCUSB_STATES, 3 | 0x10);
  1382. write_reg(hw, HFCUSB_STATES, 3);
  1383. } else {
  1384. write_reg(hw, HFCUSB_SCTRL, 0x44);
  1385. write_reg(hw, HFCUSB_SCTRL_E, 0x09);
  1386. write_reg(hw, HFCUSB_CLKDEL, CLKDEL_NT);
  1387. write_reg(hw, HFCUSB_STATES, 1 | 0x10);
  1388. write_reg(hw, HFCUSB_STATES, 1);
  1389. }
  1390. }
  1391. static void
  1392. reset_hfcsusb(struct hfcsusb *hw)
  1393. {
  1394. struct usb_fifo *fifo;
  1395. int i;
  1396. if (debug & DEBUG_HW)
  1397. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  1398. /* do Chip reset */
  1399. write_reg(hw, HFCUSB_CIRM, 8);
  1400. /* aux = output, reset off */
  1401. write_reg(hw, HFCUSB_CIRM, 0x10);
  1402. /* set USB_SIZE to match the wMaxPacketSize for INT or BULK transfers */
  1403. write_reg(hw, HFCUSB_USB_SIZE, (hw->packet_size / 8) |
  1404. ((hw->packet_size / 8) << 4));
  1405. /* set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers */
  1406. write_reg(hw, HFCUSB_USB_SIZE_I, hw->iso_packet_size);
  1407. /* enable PCM/GCI master mode */
  1408. write_reg(hw, HFCUSB_MST_MODE1, 0); /* set default values */
  1409. write_reg(hw, HFCUSB_MST_MODE0, 1); /* enable master mode */
  1410. /* init the fifos */
  1411. write_reg(hw, HFCUSB_F_THRES,
  1412. (HFCUSB_TX_THRESHOLD / 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
  1413. fifo = hw->fifos;
  1414. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1415. write_reg(hw, HFCUSB_FIFO, i); /* select the desired fifo */
  1416. fifo[i].max_size =
  1417. (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
  1418. fifo[i].last_urblen = 0;
  1419. /* set 2 bit for D- & E-channel */
  1420. write_reg(hw, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));
  1421. /* enable all fifos */
  1422. if (i == HFCUSB_D_TX)
  1423. write_reg(hw, HFCUSB_CON_HDLC,
  1424. (hw->protocol == ISDN_P_NT_S0) ? 0x08 : 0x09);
  1425. else
  1426. write_reg(hw, HFCUSB_CON_HDLC, 0x08);
  1427. write_reg(hw, HFCUSB_INC_RES_F, 2); /* reset the fifo */
  1428. }
  1429. write_reg(hw, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
  1430. handle_led(hw, LED_POWER_ON);
  1431. }
  1432. /* start USB data pipes dependand on device's endpoint configuration */
  1433. static void
  1434. hfcsusb_start_endpoint(struct hfcsusb *hw, int channel)
  1435. {
  1436. /* quick check if endpoint already running */
  1437. if ((channel == HFC_CHAN_D) && (hw->fifos[HFCUSB_D_RX].active))
  1438. return;
  1439. if ((channel == HFC_CHAN_B1) && (hw->fifos[HFCUSB_B1_RX].active))
  1440. return;
  1441. if ((channel == HFC_CHAN_B2) && (hw->fifos[HFCUSB_B2_RX].active))
  1442. return;
  1443. if ((channel == HFC_CHAN_E) && (hw->fifos[HFCUSB_PCM_RX].active))
  1444. return;
  1445. /* start rx endpoints using USB INT IN method */
  1446. if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
  1447. start_int_fifo(hw->fifos + channel * 2 + 1);
  1448. /* start rx endpoints using USB ISO IN method */
  1449. if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO) {
  1450. switch (channel) {
  1451. case HFC_CHAN_D:
  1452. start_isoc_chain(hw->fifos + HFCUSB_D_RX,
  1453. ISOC_PACKETS_D,
  1454. (usb_complete_t)rx_iso_complete,
  1455. 16);
  1456. break;
  1457. case HFC_CHAN_E:
  1458. start_isoc_chain(hw->fifos + HFCUSB_PCM_RX,
  1459. ISOC_PACKETS_D,
  1460. (usb_complete_t)rx_iso_complete,
  1461. 16);
  1462. break;
  1463. case HFC_CHAN_B1:
  1464. start_isoc_chain(hw->fifos + HFCUSB_B1_RX,
  1465. ISOC_PACKETS_B,
  1466. (usb_complete_t)rx_iso_complete,
  1467. 16);
  1468. break;
  1469. case HFC_CHAN_B2:
  1470. start_isoc_chain(hw->fifos + HFCUSB_B2_RX,
  1471. ISOC_PACKETS_B,
  1472. (usb_complete_t)rx_iso_complete,
  1473. 16);
  1474. break;
  1475. }
  1476. }
  1477. /* start tx endpoints using USB ISO OUT method */
  1478. switch (channel) {
  1479. case HFC_CHAN_D:
  1480. start_isoc_chain(hw->fifos + HFCUSB_D_TX,
  1481. ISOC_PACKETS_B,
  1482. (usb_complete_t)tx_iso_complete, 1);
  1483. break;
  1484. case HFC_CHAN_B1:
  1485. start_isoc_chain(hw->fifos + HFCUSB_B1_TX,
  1486. ISOC_PACKETS_D,
  1487. (usb_complete_t)tx_iso_complete, 1);
  1488. break;
  1489. case HFC_CHAN_B2:
  1490. start_isoc_chain(hw->fifos + HFCUSB_B2_TX,
  1491. ISOC_PACKETS_B,
  1492. (usb_complete_t)tx_iso_complete, 1);
  1493. break;
  1494. }
  1495. }
  1496. /* stop USB data pipes dependand on device's endpoint configuration */
  1497. static void
  1498. hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel)
  1499. {
  1500. /* quick check if endpoint currently running */
  1501. if ((channel == HFC_CHAN_D) && (!hw->fifos[HFCUSB_D_RX].active))
  1502. return;
  1503. if ((channel == HFC_CHAN_B1) && (!hw->fifos[HFCUSB_B1_RX].active))
  1504. return;
  1505. if ((channel == HFC_CHAN_B2) && (!hw->fifos[HFCUSB_B2_RX].active))
  1506. return;
  1507. if ((channel == HFC_CHAN_E) && (!hw->fifos[HFCUSB_PCM_RX].active))
  1508. return;
  1509. /* rx endpoints using USB INT IN method */
  1510. if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
  1511. stop_int_gracefull(hw->fifos + channel * 2 + 1);
  1512. /* rx endpoints using USB ISO IN method */
  1513. if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO)
  1514. stop_iso_gracefull(hw->fifos + channel * 2 + 1);
  1515. /* tx endpoints using USB ISO OUT method */
  1516. if (channel != HFC_CHAN_E)
  1517. stop_iso_gracefull(hw->fifos + channel * 2);
  1518. }
  1519. /* Hardware Initialization */
  1520. static int
  1521. setup_hfcsusb(struct hfcsusb *hw)
  1522. {
  1523. u_char b;
  1524. if (debug & DBG_HFC_CALL_TRACE)
  1525. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  1526. /* check the chip id */
  1527. if (read_reg_atomic(hw, HFCUSB_CHIP_ID, &b) != 1) {
  1528. printk(KERN_DEBUG "%s: %s: cannot read chip id\n",
  1529. hw->name, __func__);
  1530. return 1;
  1531. }
  1532. if (b != HFCUSB_CHIPID) {
  1533. printk(KERN_DEBUG "%s: %s: Invalid chip id 0x%02x\n",
  1534. hw->name, __func__, b);
  1535. return 1;
  1536. }
  1537. /* first set the needed config, interface and alternate */
  1538. (void) usb_set_interface(hw->dev, hw->if_used, hw->alt_used);
  1539. hw->led_state = 0;
  1540. /* init the background machinery for control requests */
  1541. hw->ctrl_read.bRequestType = 0xc0;
  1542. hw->ctrl_read.bRequest = 1;
  1543. hw->ctrl_read.wLength = cpu_to_le16(1);
  1544. hw->ctrl_write.bRequestType = 0x40;
  1545. hw->ctrl_write.bRequest = 0;
  1546. hw->ctrl_write.wLength = 0;
  1547. usb_fill_control_urb(hw->ctrl_urb, hw->dev, hw->ctrl_out_pipe,
  1548. (u_char *)&hw->ctrl_write, NULL, 0,
  1549. (usb_complete_t)ctrl_complete, hw);
  1550. reset_hfcsusb(hw);
  1551. return 0;
  1552. }
  1553. static void
  1554. release_hw(struct hfcsusb *hw)
  1555. {
  1556. if (debug & DBG_HFC_CALL_TRACE)
  1557. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  1558. /*
  1559. * stop all endpoints gracefully
  1560. * TODO: mISDN_core should generate CLOSE_CHANNEL
  1561. * signals after calling mISDN_unregister_device()
  1562. */
  1563. hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
  1564. hfcsusb_stop_endpoint(hw, HFC_CHAN_B1);
  1565. hfcsusb_stop_endpoint(hw, HFC_CHAN_B2);
  1566. if (hw->fifos[HFCUSB_PCM_RX].pipe)
  1567. hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
  1568. if (hw->protocol == ISDN_P_TE_S0)
  1569. l1_event(hw->dch.l1, CLOSE_CHANNEL);
  1570. mISDN_unregister_device(&hw->dch.dev);
  1571. mISDN_freebchannel(&hw->bch[1]);
  1572. mISDN_freebchannel(&hw->bch[0]);
  1573. mISDN_freedchannel(&hw->dch);
  1574. if (hw->ctrl_urb) {
  1575. usb_kill_urb(hw->ctrl_urb);
  1576. usb_free_urb(hw->ctrl_urb);
  1577. hw->ctrl_urb = NULL;
  1578. }
  1579. if (hw->intf)
  1580. usb_set_intfdata(hw->intf, NULL);
  1581. list_del(&hw->list);
  1582. kfree(hw);
  1583. hw = NULL;
  1584. }
  1585. static void
  1586. deactivate_bchannel(struct bchannel *bch)
  1587. {
  1588. struct hfcsusb *hw = bch->hw;
  1589. u_long flags;
  1590. if (bch->debug & DEBUG_HW)
  1591. printk(KERN_DEBUG "%s: %s: bch->nr(%i)\n",
  1592. hw->name, __func__, bch->nr);
  1593. spin_lock_irqsave(&hw->lock, flags);
  1594. mISDN_clear_bchannel(bch);
  1595. spin_unlock_irqrestore(&hw->lock, flags);
  1596. hfcsusb_setup_bch(bch, ISDN_P_NONE);
  1597. hfcsusb_stop_endpoint(hw, bch->nr - 1);
  1598. }
  1599. /*
  1600. * Layer 1 B-channel hardware access
  1601. */
  1602. static int
  1603. hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1604. {
  1605. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  1606. int ret = -EINVAL;
  1607. if (bch->debug & DEBUG_HW)
  1608. printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
  1609. switch (cmd) {
  1610. case HW_TESTRX_RAW:
  1611. case HW_TESTRX_HDLC:
  1612. case HW_TESTRX_OFF:
  1613. ret = -EINVAL;
  1614. break;
  1615. case CLOSE_CHANNEL:
  1616. test_and_clear_bit(FLG_OPEN, &bch->Flags);
  1617. deactivate_bchannel(bch);
  1618. ch->protocol = ISDN_P_NONE;
  1619. ch->peer = NULL;
  1620. module_put(THIS_MODULE);
  1621. ret = 0;
  1622. break;
  1623. case CONTROL_CHANNEL:
  1624. ret = channel_bctrl(bch, arg);
  1625. break;
  1626. default:
  1627. printk(KERN_WARNING "%s: unknown prim(%x)\n",
  1628. __func__, cmd);
  1629. }
  1630. return ret;
  1631. }
  1632. static int
  1633. setup_instance(struct hfcsusb *hw, struct device *parent)
  1634. {
  1635. u_long flags;
  1636. int err, i;
  1637. if (debug & DBG_HFC_CALL_TRACE)
  1638. printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
  1639. spin_lock_init(&hw->ctrl_lock);
  1640. spin_lock_init(&hw->lock);
  1641. mISDN_initdchannel(&hw->dch, MAX_DFRAME_LEN_L1, ph_state);
  1642. hw->dch.debug = debug & 0xFFFF;
  1643. hw->dch.hw = hw;
  1644. hw->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
  1645. hw->dch.dev.D.send = hfcusb_l2l1D;
  1646. hw->dch.dev.D.ctrl = hfc_dctrl;
  1647. /* enable E-Channel logging */
  1648. if (hw->fifos[HFCUSB_PCM_RX].pipe)
  1649. mISDN_initdchannel(&hw->ech, MAX_DFRAME_LEN_L1, NULL);
  1650. hw->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
  1651. (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
  1652. hw->dch.dev.nrbchan = 2;
  1653. for (i = 0; i < 2; i++) {
  1654. hw->bch[i].nr = i + 1;
  1655. set_channelmap(i + 1, hw->dch.dev.channelmap);
  1656. hw->bch[i].debug = debug;
  1657. mISDN_initbchannel(&hw->bch[i], MAX_DATA_MEM, poll >> 1);
  1658. hw->bch[i].hw = hw;
  1659. hw->bch[i].ch.send = hfcusb_l2l1B;
  1660. hw->bch[i].ch.ctrl = hfc_bctrl;
  1661. hw->bch[i].ch.nr = i + 1;
  1662. list_add(&hw->bch[i].ch.list, &hw->dch.dev.bchannels);
  1663. }
  1664. hw->fifos[HFCUSB_B1_TX].bch = &hw->bch[0];
  1665. hw->fifos[HFCUSB_B1_RX].bch = &hw->bch[0];
  1666. hw->fifos[HFCUSB_B2_TX].bch = &hw->bch[1];
  1667. hw->fifos[HFCUSB_B2_RX].bch = &hw->bch[1];
  1668. hw->fifos[HFCUSB_D_TX].dch = &hw->dch;
  1669. hw->fifos[HFCUSB_D_RX].dch = &hw->dch;
  1670. hw->fifos[HFCUSB_PCM_RX].ech = &hw->ech;
  1671. hw->fifos[HFCUSB_PCM_TX].ech = &hw->ech;
  1672. err = setup_hfcsusb(hw);
  1673. if (err)
  1674. goto out;
  1675. snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s.%d", DRIVER_NAME,
  1676. hfcsusb_cnt + 1);
  1677. printk(KERN_INFO "%s: registered as '%s'\n",
  1678. DRIVER_NAME, hw->name);
  1679. err = mISDN_register_device(&hw->dch.dev, parent, hw->name);
  1680. if (err)
  1681. goto out;
  1682. hfcsusb_cnt++;
  1683. write_lock_irqsave(&HFClock, flags);
  1684. list_add_tail(&hw->list, &HFClist);
  1685. write_unlock_irqrestore(&HFClock, flags);
  1686. return 0;
  1687. out:
  1688. mISDN_freebchannel(&hw->bch[1]);
  1689. mISDN_freebchannel(&hw->bch[0]);
  1690. mISDN_freedchannel(&hw->dch);
  1691. kfree(hw);
  1692. return err;
  1693. }
  1694. static int
  1695. hfcsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1696. {
  1697. struct hfcsusb *hw;
  1698. struct usb_device *dev = interface_to_usbdev(intf);
  1699. struct usb_host_interface *iface = intf->cur_altsetting;
  1700. struct usb_host_interface *iface_used = NULL;
  1701. struct usb_host_endpoint *ep;
  1702. struct hfcsusb_vdata *driver_info;
  1703. int ifnum = iface->desc.bInterfaceNumber, i, idx, alt_idx,
  1704. probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found,
  1705. ep_addr, cmptbl[16], small_match, iso_packet_size, packet_size,
  1706. alt_used = 0;
  1707. vend_idx = 0xffff;
  1708. for (i = 0; hfcsusb_idtab[i].idVendor; i++) {
  1709. if ((le16_to_cpu(dev->descriptor.idVendor)
  1710. == hfcsusb_idtab[i].idVendor) &&
  1711. (le16_to_cpu(dev->descriptor.idProduct)
  1712. == hfcsusb_idtab[i].idProduct)) {
  1713. vend_idx = i;
  1714. continue;
  1715. }
  1716. }
  1717. printk(KERN_DEBUG
  1718. "%s: interface(%d) actalt(%d) minor(%d) vend_idx(%d)\n",
  1719. __func__, ifnum, iface->desc.bAlternateSetting,
  1720. intf->minor, vend_idx);
  1721. if (vend_idx == 0xffff) {
  1722. printk(KERN_WARNING
  1723. "%s: no valid vendor found in USB descriptor\n",
  1724. __func__);
  1725. return -EIO;
  1726. }
  1727. /* if vendor and product ID is OK, start probing alternate settings */
  1728. alt_idx = 0;
  1729. small_match = -1;
  1730. /* default settings */
  1731. iso_packet_size = 16;
  1732. packet_size = 64;
  1733. while (alt_idx < intf->num_altsetting) {
  1734. iface = intf->altsetting + alt_idx;
  1735. probe_alt_setting = iface->desc.bAlternateSetting;
  1736. cfg_used = 0;
  1737. while (validconf[cfg_used][0]) {
  1738. cfg_found = 1;
  1739. vcf = validconf[cfg_used];
  1740. ep = iface->endpoint;
  1741. memcpy(cmptbl, vcf, 16 * sizeof(int));
  1742. /* check for all endpoints in this alternate setting */
  1743. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1744. ep_addr = ep->desc.bEndpointAddress;
  1745. /* get endpoint base */
  1746. idx = ((ep_addr & 0x7f) - 1) * 2;
  1747. if (ep_addr & 0x80)
  1748. idx++;
  1749. attr = ep->desc.bmAttributes;
  1750. if (cmptbl[idx] != EP_NOP) {
  1751. if (cmptbl[idx] == EP_NUL)
  1752. cfg_found = 0;
  1753. if (attr == USB_ENDPOINT_XFER_INT
  1754. && cmptbl[idx] == EP_INT)
  1755. cmptbl[idx] = EP_NUL;
  1756. if (attr == USB_ENDPOINT_XFER_BULK
  1757. && cmptbl[idx] == EP_BLK)
  1758. cmptbl[idx] = EP_NUL;
  1759. if (attr == USB_ENDPOINT_XFER_ISOC
  1760. && cmptbl[idx] == EP_ISO)
  1761. cmptbl[idx] = EP_NUL;
  1762. if (attr == USB_ENDPOINT_XFER_INT &&
  1763. ep->desc.bInterval < vcf[17]) {
  1764. cfg_found = 0;
  1765. }
  1766. }
  1767. ep++;
  1768. }
  1769. for (i = 0; i < 16; i++)
  1770. if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
  1771. cfg_found = 0;
  1772. if (cfg_found) {
  1773. if (small_match < cfg_used) {
  1774. small_match = cfg_used;
  1775. alt_used = probe_alt_setting;
  1776. iface_used = iface;
  1777. }
  1778. }
  1779. cfg_used++;
  1780. }
  1781. alt_idx++;
  1782. } /* (alt_idx < intf->num_altsetting) */
  1783. /* not found a valid USB Ta Endpoint config */
  1784. if (small_match == -1)
  1785. return -EIO;
  1786. iface = iface_used;
  1787. hw = kzalloc(sizeof(struct hfcsusb), GFP_KERNEL);
  1788. if (!hw)
  1789. return -ENOMEM; /* got no mem */
  1790. snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s", DRIVER_NAME);
  1791. ep = iface->endpoint;
  1792. vcf = validconf[small_match];
  1793. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1794. struct usb_fifo *f;
  1795. ep_addr = ep->desc.bEndpointAddress;
  1796. /* get endpoint base */
  1797. idx = ((ep_addr & 0x7f) - 1) * 2;
  1798. if (ep_addr & 0x80)
  1799. idx++;
  1800. f = &hw->fifos[idx & 7];
  1801. /* init Endpoints */
  1802. if (vcf[idx] == EP_NOP || vcf[idx] == EP_NUL) {
  1803. ep++;
  1804. continue;
  1805. }
  1806. switch (ep->desc.bmAttributes) {
  1807. case USB_ENDPOINT_XFER_INT:
  1808. f->pipe = usb_rcvintpipe(dev,
  1809. ep->desc.bEndpointAddress);
  1810. f->usb_transfer_mode = USB_INT;
  1811. packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  1812. break;
  1813. case USB_ENDPOINT_XFER_BULK:
  1814. if (ep_addr & 0x80)
  1815. f->pipe = usb_rcvbulkpipe(dev,
  1816. ep->desc.bEndpointAddress);
  1817. else
  1818. f->pipe = usb_sndbulkpipe(dev,
  1819. ep->desc.bEndpointAddress);
  1820. f->usb_transfer_mode = USB_BULK;
  1821. packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  1822. break;
  1823. case USB_ENDPOINT_XFER_ISOC:
  1824. if (ep_addr & 0x80)
  1825. f->pipe = usb_rcvisocpipe(dev,
  1826. ep->desc.bEndpointAddress);
  1827. else
  1828. f->pipe = usb_sndisocpipe(dev,
  1829. ep->desc.bEndpointAddress);
  1830. f->usb_transfer_mode = USB_ISOC;
  1831. iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  1832. break;
  1833. default:
  1834. f->pipe = 0;
  1835. }
  1836. if (f->pipe) {
  1837. f->fifonum = idx & 7;
  1838. f->hw = hw;
  1839. f->usb_packet_maxlen =
  1840. le16_to_cpu(ep->desc.wMaxPacketSize);
  1841. f->intervall = ep->desc.bInterval;
  1842. }
  1843. ep++;
  1844. }
  1845. hw->dev = dev; /* save device */
  1846. hw->if_used = ifnum; /* save used interface */
  1847. hw->alt_used = alt_used; /* and alternate config */
  1848. hw->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
  1849. hw->cfg_used = vcf[16]; /* store used config */
  1850. hw->vend_idx = vend_idx; /* store found vendor */
  1851. hw->packet_size = packet_size;
  1852. hw->iso_packet_size = iso_packet_size;
  1853. /* create the control pipes needed for register access */
  1854. hw->ctrl_in_pipe = usb_rcvctrlpipe(hw->dev, 0);
  1855. hw->ctrl_out_pipe = usb_sndctrlpipe(hw->dev, 0);
  1856. driver_info = (struct hfcsusb_vdata *)
  1857. hfcsusb_idtab[vend_idx].driver_info;
  1858. hw->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  1859. if (!hw->ctrl_urb) {
  1860. pr_warn("%s: No memory for control urb\n",
  1861. driver_info->vend_name);
  1862. kfree(hw);
  1863. return -ENOMEM;
  1864. }
  1865. pr_info("%s: %s: detected \"%s\" (%s, if=%d alt=%d)\n",
  1866. hw->name, __func__, driver_info->vend_name,
  1867. conf_str[small_match], ifnum, alt_used);
  1868. if (setup_instance(hw, dev->dev.parent))
  1869. return -EIO;
  1870. hw->intf = intf;
  1871. usb_set_intfdata(hw->intf, hw);
  1872. return 0;
  1873. }
  1874. /* function called when an active device is removed */
  1875. static void
  1876. hfcsusb_disconnect(struct usb_interface *intf)
  1877. {
  1878. struct hfcsusb *hw = usb_get_intfdata(intf);
  1879. struct hfcsusb *next;
  1880. int cnt = 0;
  1881. printk(KERN_INFO "%s: device disconnected\n", hw->name);
  1882. handle_led(hw, LED_POWER_OFF);
  1883. release_hw(hw);
  1884. list_for_each_entry_safe(hw, next, &HFClist, list)
  1885. cnt++;
  1886. if (!cnt)
  1887. hfcsusb_cnt = 0;
  1888. usb_set_intfdata(intf, NULL);
  1889. }
  1890. static struct usb_driver hfcsusb_drv = {
  1891. .name = DRIVER_NAME,
  1892. .id_table = hfcsusb_idtab,
  1893. .probe = hfcsusb_probe,
  1894. .disconnect = hfcsusb_disconnect,
  1895. .disable_hub_initiated_lpm = 1,
  1896. };
  1897. module_usb_driver(hfcsusb_drv);