st5481_d.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Driver for ST5481 USB ISDN modem
  3. *
  4. * Author Frode Isaksen
  5. * Copyright 2001 by Frode Isaksen <fisaksen@bewan.com>
  6. * 2001 by Kai Germaschewski <kai.germaschewski@gmx.de>
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/gfp.h>
  14. #include <linux/usb.h>
  15. #include <linux/netdevice.h>
  16. #include "st5481.h"
  17. static void ph_connect(struct st5481_adapter *adapter);
  18. static void ph_disconnect(struct st5481_adapter *adapter);
  19. static struct Fsm l1fsm;
  20. static char *strL1State[] =
  21. {
  22. "ST_L1_F3",
  23. "ST_L1_F4",
  24. "ST_L1_F6",
  25. "ST_L1_F7",
  26. "ST_L1_F8",
  27. };
  28. static char *strL1Event[] =
  29. {
  30. "EV_IND_DP",
  31. "EV_IND_1",
  32. "EV_IND_2",
  33. "EV_IND_3",
  34. "EV_IND_RSY",
  35. "EV_IND_5",
  36. "EV_IND_6",
  37. "EV_IND_7",
  38. "EV_IND_AP",
  39. "EV_IND_9",
  40. "EV_IND_10",
  41. "EV_IND_11",
  42. "EV_IND_AI8",
  43. "EV_IND_AI10",
  44. "EV_IND_AIL",
  45. "EV_IND_DI",
  46. "EV_PH_ACTIVATE_REQ",
  47. "EV_PH_DEACTIVATE_REQ",
  48. "EV_TIMER3",
  49. };
  50. static inline void D_L1L2(struct st5481_adapter *adapter, int pr, void *arg)
  51. {
  52. struct hisax_if *ifc = (struct hisax_if *) &adapter->hisax_d_if;
  53. ifc->l1l2(ifc, pr, arg);
  54. }
  55. static void
  56. l1_go_f3(struct FsmInst *fi, int event, void *arg)
  57. {
  58. struct st5481_adapter *adapter = fi->userdata;
  59. if (fi->state == ST_L1_F7)
  60. ph_disconnect(adapter);
  61. FsmChangeState(fi, ST_L1_F3);
  62. D_L1L2(adapter, PH_DEACTIVATE | INDICATION, NULL);
  63. }
  64. static void
  65. l1_go_f6(struct FsmInst *fi, int event, void *arg)
  66. {
  67. struct st5481_adapter *adapter = fi->userdata;
  68. if (fi->state == ST_L1_F7)
  69. ph_disconnect(adapter);
  70. FsmChangeState(fi, ST_L1_F6);
  71. }
  72. static void
  73. l1_go_f7(struct FsmInst *fi, int event, void *arg)
  74. {
  75. struct st5481_adapter *adapter = fi->userdata;
  76. FsmDelTimer(&adapter->timer, 0);
  77. ph_connect(adapter);
  78. FsmChangeState(fi, ST_L1_F7);
  79. D_L1L2(adapter, PH_ACTIVATE | INDICATION, NULL);
  80. }
  81. static void
  82. l1_go_f8(struct FsmInst *fi, int event, void *arg)
  83. {
  84. struct st5481_adapter *adapter = fi->userdata;
  85. if (fi->state == ST_L1_F7)
  86. ph_disconnect(adapter);
  87. FsmChangeState(fi, ST_L1_F8);
  88. }
  89. static void
  90. l1_timer3(struct FsmInst *fi, int event, void *arg)
  91. {
  92. struct st5481_adapter *adapter = fi->userdata;
  93. st5481_ph_command(adapter, ST5481_CMD_DR);
  94. FsmChangeState(fi, ST_L1_F3);
  95. D_L1L2(adapter, PH_DEACTIVATE | INDICATION, NULL);
  96. }
  97. static void
  98. l1_ignore(struct FsmInst *fi, int event, void *arg)
  99. {
  100. }
  101. static void
  102. l1_activate(struct FsmInst *fi, int event, void *arg)
  103. {
  104. struct st5481_adapter *adapter = fi->userdata;
  105. st5481_ph_command(adapter, ST5481_CMD_DR);
  106. st5481_ph_command(adapter, ST5481_CMD_PUP);
  107. FsmRestartTimer(&adapter->timer, TIMER3_VALUE, EV_TIMER3, NULL, 2);
  108. st5481_ph_command(adapter, ST5481_CMD_AR8);
  109. FsmChangeState(fi, ST_L1_F4);
  110. }
  111. static struct FsmNode L1FnList[] __initdata =
  112. {
  113. {ST_L1_F3, EV_IND_DP, l1_ignore},
  114. {ST_L1_F3, EV_IND_AP, l1_go_f6},
  115. {ST_L1_F3, EV_IND_AI8, l1_go_f7},
  116. {ST_L1_F3, EV_IND_AI10, l1_go_f7},
  117. {ST_L1_F3, EV_PH_ACTIVATE_REQ, l1_activate},
  118. {ST_L1_F4, EV_TIMER3, l1_timer3},
  119. {ST_L1_F4, EV_IND_DP, l1_go_f3},
  120. {ST_L1_F4, EV_IND_AP, l1_go_f6},
  121. {ST_L1_F4, EV_IND_AI8, l1_go_f7},
  122. {ST_L1_F4, EV_IND_AI10, l1_go_f7},
  123. {ST_L1_F6, EV_TIMER3, l1_timer3},
  124. {ST_L1_F6, EV_IND_DP, l1_go_f3},
  125. {ST_L1_F6, EV_IND_AP, l1_ignore},
  126. {ST_L1_F6, EV_IND_AI8, l1_go_f7},
  127. {ST_L1_F6, EV_IND_AI10, l1_go_f7},
  128. {ST_L1_F7, EV_IND_RSY, l1_go_f8},
  129. {ST_L1_F7, EV_IND_DP, l1_go_f3},
  130. {ST_L1_F7, EV_IND_AP, l1_go_f6},
  131. {ST_L1_F7, EV_IND_AI8, l1_ignore},
  132. {ST_L1_F7, EV_IND_AI10, l1_ignore},
  133. {ST_L1_F7, EV_IND_RSY, l1_go_f8},
  134. {ST_L1_F8, EV_TIMER3, l1_timer3},
  135. {ST_L1_F8, EV_IND_DP, l1_go_f3},
  136. {ST_L1_F8, EV_IND_AP, l1_go_f6},
  137. {ST_L1_F8, EV_IND_AI8, l1_go_f8},
  138. {ST_L1_F8, EV_IND_AI10, l1_go_f8},
  139. {ST_L1_F8, EV_IND_RSY, l1_ignore},
  140. };
  141. static __printf(2, 3)
  142. void l1m_debug(struct FsmInst *fi, char *fmt, ...)
  143. {
  144. va_list args;
  145. char buf[256];
  146. va_start(args, fmt);
  147. vsnprintf(buf, sizeof(buf), fmt, args);
  148. DBG(8, "%s", buf);
  149. va_end(args);
  150. }
  151. /* ======================================================================
  152. * D-Channel out
  153. */
  154. /*
  155. D OUT state machine:
  156. ====================
  157. Transmit short frame (< 16 bytes of encoded data):
  158. L1 FRAME D_OUT_STATE USB D CHANNEL
  159. -------- ----------- --- ---------
  160. FIXME
  161. -> [xx..xx] SHORT_INIT -> [7Exx..xxC1C27EFF]
  162. SHORT_WAIT_DEN <> OUT_D_COUNTER=16
  163. END_OF_SHORT <- DEN_EVENT -> 7Exx
  164. xxxx
  165. xxxx
  166. xxxx
  167. xxxx
  168. xxxx
  169. C1C1
  170. 7EFF
  171. WAIT_FOR_RESET_IDLE <- D_UNDERRUN <- (8ms)
  172. IDLE <> Reset pipe
  173. Transmit long frame (>= 16 bytes of encoded data):
  174. L1 FRAME D_OUT_STATE USB D CHANNEL
  175. -------- ----------- --- ---------
  176. -> [xx...xx] IDLE
  177. WAIT_FOR_STOP <> OUT_D_COUNTER=0
  178. WAIT_FOR_RESET <> Reset pipe
  179. STOP
  180. INIT_LONG_FRAME -> [7Exx..xx]
  181. WAIT_DEN <> OUT_D_COUNTER=16
  182. OUT_NORMAL <- DEN_EVENT -> 7Exx
  183. END_OF_FRAME_BUSY -> [xxxx] xxxx
  184. END_OF_FRAME_NOT_BUSY -> [xxxx] xxxx
  185. -> [xxxx] xxxx
  186. -> [C1C2] xxxx
  187. -> [7EFF] xxxx
  188. xxxx
  189. xxxx
  190. ....
  191. xxxx
  192. C1C2
  193. 7EFF
  194. <- D_UNDERRUN <- (> 8ms)
  195. WAIT_FOR_STOP <> OUT_D_COUNTER=0
  196. WAIT_FOR_RESET <> Reset pipe
  197. STOP
  198. */
  199. static struct Fsm dout_fsm;
  200. static char *strDoutState[] =
  201. {
  202. "ST_DOUT_NONE",
  203. "ST_DOUT_SHORT_INIT",
  204. "ST_DOUT_SHORT_WAIT_DEN",
  205. "ST_DOUT_LONG_INIT",
  206. "ST_DOUT_LONG_WAIT_DEN",
  207. "ST_DOUT_NORMAL",
  208. "ST_DOUT_WAIT_FOR_UNDERRUN",
  209. "ST_DOUT_WAIT_FOR_NOT_BUSY",
  210. "ST_DOUT_WAIT_FOR_STOP",
  211. "ST_DOUT_WAIT_FOR_RESET",
  212. };
  213. static char *strDoutEvent[] =
  214. {
  215. "EV_DOUT_START_XMIT",
  216. "EV_DOUT_COMPLETE",
  217. "EV_DOUT_DEN",
  218. "EV_DOUT_RESETED",
  219. "EV_DOUT_STOPPED",
  220. "EV_DOUT_COLL",
  221. "EV_DOUT_UNDERRUN",
  222. };
  223. static __printf(2, 3)
  224. void dout_debug(struct FsmInst *fi, char *fmt, ...)
  225. {
  226. va_list args;
  227. char buf[256];
  228. va_start(args, fmt);
  229. vsnprintf(buf, sizeof(buf), fmt, args);
  230. DBG(0x2, "%s", buf);
  231. va_end(args);
  232. }
  233. static void dout_stop_event(void *context)
  234. {
  235. struct st5481_adapter *adapter = context;
  236. FsmEvent(&adapter->d_out.fsm, EV_DOUT_STOPPED, NULL);
  237. }
  238. /*
  239. * Start the transfer of a D channel frame.
  240. */
  241. static void usb_d_out(struct st5481_adapter *adapter, int buf_nr)
  242. {
  243. struct st5481_d_out *d_out = &adapter->d_out;
  244. struct urb *urb;
  245. unsigned int num_packets, packet_offset;
  246. int len, buf_size, bytes_sent;
  247. struct sk_buff *skb;
  248. struct usb_iso_packet_descriptor *desc;
  249. if (d_out->fsm.state != ST_DOUT_NORMAL)
  250. return;
  251. if (test_and_set_bit(buf_nr, &d_out->busy)) {
  252. DBG(2, "ep %d urb %d busy %#lx", EP_D_OUT, buf_nr, d_out->busy);
  253. return;
  254. }
  255. urb = d_out->urb[buf_nr];
  256. skb = d_out->tx_skb;
  257. buf_size = NUM_ISO_PACKETS_D * SIZE_ISO_PACKETS_D_OUT;
  258. if (skb) {
  259. len = isdnhdlc_encode(&d_out->hdlc_state,
  260. skb->data, skb->len, &bytes_sent,
  261. urb->transfer_buffer, buf_size);
  262. skb_pull(skb, bytes_sent);
  263. } else {
  264. // Send flags or idle
  265. len = isdnhdlc_encode(&d_out->hdlc_state,
  266. NULL, 0, &bytes_sent,
  267. urb->transfer_buffer, buf_size);
  268. }
  269. if (len < buf_size) {
  270. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_UNDERRUN);
  271. }
  272. if (skb && !skb->len) {
  273. d_out->tx_skb = NULL;
  274. D_L1L2(adapter, PH_DATA | CONFIRM, NULL);
  275. dev_kfree_skb_any(skb);
  276. }
  277. // Prepare the URB
  278. urb->transfer_buffer_length = len;
  279. num_packets = 0;
  280. packet_offset = 0;
  281. while (packet_offset < len) {
  282. desc = &urb->iso_frame_desc[num_packets];
  283. desc->offset = packet_offset;
  284. desc->length = SIZE_ISO_PACKETS_D_OUT;
  285. if (len - packet_offset < desc->length)
  286. desc->length = len - packet_offset;
  287. num_packets++;
  288. packet_offset += desc->length;
  289. }
  290. urb->number_of_packets = num_packets;
  291. // Prepare the URB
  292. urb->dev = adapter->usb_dev;
  293. // Need to transmit the next buffer 2ms after the DEN_EVENT
  294. urb->transfer_flags = 0;
  295. urb->start_frame = usb_get_current_frame_number(adapter->usb_dev) + 2;
  296. DBG_ISO_PACKET(0x20, urb);
  297. if (usb_submit_urb(urb, GFP_KERNEL) < 0) {
  298. // There is another URB queued up
  299. urb->transfer_flags = URB_ISO_ASAP;
  300. SUBMIT_URB(urb, GFP_KERNEL);
  301. }
  302. }
  303. static void fifo_reseted(void *context)
  304. {
  305. struct st5481_adapter *adapter = context;
  306. FsmEvent(&adapter->d_out.fsm, EV_DOUT_RESETED, NULL);
  307. }
  308. static void usb_d_out_complete(struct urb *urb)
  309. {
  310. struct st5481_adapter *adapter = urb->context;
  311. struct st5481_d_out *d_out = &adapter->d_out;
  312. long buf_nr;
  313. DBG(2, "");
  314. buf_nr = get_buf_nr(d_out->urb, urb);
  315. test_and_clear_bit(buf_nr, &d_out->busy);
  316. if (unlikely(urb->status < 0)) {
  317. switch (urb->status) {
  318. case -ENOENT:
  319. case -ESHUTDOWN:
  320. case -ECONNRESET:
  321. DBG(1, "urb killed status %d", urb->status);
  322. break;
  323. default:
  324. WARNING("urb status %d", urb->status);
  325. if (d_out->busy == 0) {
  326. st5481_usb_pipe_reset(adapter, EP_D_OUT | USB_DIR_OUT, fifo_reseted, adapter);
  327. }
  328. break;
  329. }
  330. return; // Give up
  331. }
  332. FsmEvent(&adapter->d_out.fsm, EV_DOUT_COMPLETE, (void *) buf_nr);
  333. }
  334. /* ====================================================================== */
  335. static void dout_start_xmit(struct FsmInst *fsm, int event, void *arg)
  336. {
  337. // FIXME unify?
  338. struct st5481_adapter *adapter = fsm->userdata;
  339. struct st5481_d_out *d_out = &adapter->d_out;
  340. struct urb *urb;
  341. int len, bytes_sent;
  342. struct sk_buff *skb;
  343. int buf_nr = 0;
  344. skb = d_out->tx_skb;
  345. DBG(2, "len=%d", skb->len);
  346. isdnhdlc_out_init(&d_out->hdlc_state, HDLC_DCHANNEL | HDLC_BITREVERSE);
  347. if (test_and_set_bit(buf_nr, &d_out->busy)) {
  348. WARNING("ep %d urb %d busy %#lx", EP_D_OUT, buf_nr, d_out->busy);
  349. return;
  350. }
  351. urb = d_out->urb[buf_nr];
  352. DBG_SKB(0x10, skb);
  353. len = isdnhdlc_encode(&d_out->hdlc_state,
  354. skb->data, skb->len, &bytes_sent,
  355. urb->transfer_buffer, 16);
  356. skb_pull(skb, bytes_sent);
  357. if (len < 16)
  358. FsmChangeState(&d_out->fsm, ST_DOUT_SHORT_INIT);
  359. else
  360. FsmChangeState(&d_out->fsm, ST_DOUT_LONG_INIT);
  361. if (skb->len == 0) {
  362. d_out->tx_skb = NULL;
  363. D_L1L2(adapter, PH_DATA | CONFIRM, NULL);
  364. dev_kfree_skb_any(skb);
  365. }
  366. // Prepare the URB
  367. urb->transfer_buffer_length = len;
  368. urb->iso_frame_desc[0].offset = 0;
  369. urb->iso_frame_desc[0].length = len;
  370. urb->number_of_packets = 1;
  371. // Prepare the URB
  372. urb->dev = adapter->usb_dev;
  373. urb->transfer_flags = URB_ISO_ASAP;
  374. DBG_ISO_PACKET(0x20, urb);
  375. SUBMIT_URB(urb, GFP_KERNEL);
  376. }
  377. static void dout_short_fifo(struct FsmInst *fsm, int event, void *arg)
  378. {
  379. struct st5481_adapter *adapter = fsm->userdata;
  380. struct st5481_d_out *d_out = &adapter->d_out;
  381. FsmChangeState(&d_out->fsm, ST_DOUT_SHORT_WAIT_DEN);
  382. st5481_usb_device_ctrl_msg(adapter, OUT_D_COUNTER, 16, NULL, NULL);
  383. }
  384. static void dout_end_short_frame(struct FsmInst *fsm, int event, void *arg)
  385. {
  386. struct st5481_adapter *adapter = fsm->userdata;
  387. struct st5481_d_out *d_out = &adapter->d_out;
  388. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_UNDERRUN);
  389. }
  390. static void dout_long_enable_fifo(struct FsmInst *fsm, int event, void *arg)
  391. {
  392. struct st5481_adapter *adapter = fsm->userdata;
  393. struct st5481_d_out *d_out = &adapter->d_out;
  394. st5481_usb_device_ctrl_msg(adapter, OUT_D_COUNTER, 16, NULL, NULL);
  395. FsmChangeState(&d_out->fsm, ST_DOUT_LONG_WAIT_DEN);
  396. }
  397. static void dout_long_den(struct FsmInst *fsm, int event, void *arg)
  398. {
  399. struct st5481_adapter *adapter = fsm->userdata;
  400. struct st5481_d_out *d_out = &adapter->d_out;
  401. FsmChangeState(&d_out->fsm, ST_DOUT_NORMAL);
  402. usb_d_out(adapter, 0);
  403. usb_d_out(adapter, 1);
  404. }
  405. static void dout_reset(struct FsmInst *fsm, int event, void *arg)
  406. {
  407. struct st5481_adapter *adapter = fsm->userdata;
  408. struct st5481_d_out *d_out = &adapter->d_out;
  409. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_RESET);
  410. st5481_usb_pipe_reset(adapter, EP_D_OUT | USB_DIR_OUT, fifo_reseted, adapter);
  411. }
  412. static void dout_stop(struct FsmInst *fsm, int event, void *arg)
  413. {
  414. struct st5481_adapter *adapter = fsm->userdata;
  415. struct st5481_d_out *d_out = &adapter->d_out;
  416. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_STOP);
  417. st5481_usb_device_ctrl_msg(adapter, OUT_D_COUNTER, 0, dout_stop_event, adapter);
  418. }
  419. static void dout_underrun(struct FsmInst *fsm, int event, void *arg)
  420. {
  421. struct st5481_adapter *adapter = fsm->userdata;
  422. struct st5481_d_out *d_out = &adapter->d_out;
  423. if (test_bit(0, &d_out->busy) || test_bit(1, &d_out->busy)) {
  424. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_NOT_BUSY);
  425. } else {
  426. dout_stop(fsm, event, arg);
  427. }
  428. }
  429. static void dout_check_busy(struct FsmInst *fsm, int event, void *arg)
  430. {
  431. struct st5481_adapter *adapter = fsm->userdata;
  432. struct st5481_d_out *d_out = &adapter->d_out;
  433. if (!test_bit(0, &d_out->busy) && !test_bit(1, &d_out->busy))
  434. dout_stop(fsm, event, arg);
  435. }
  436. static void dout_reseted(struct FsmInst *fsm, int event, void *arg)
  437. {
  438. struct st5481_adapter *adapter = fsm->userdata;
  439. struct st5481_d_out *d_out = &adapter->d_out;
  440. FsmChangeState(&d_out->fsm, ST_DOUT_NONE);
  441. // FIXME locking
  442. if (d_out->tx_skb)
  443. FsmEvent(&d_out->fsm, EV_DOUT_START_XMIT, NULL);
  444. }
  445. static void dout_complete(struct FsmInst *fsm, int event, void *arg)
  446. {
  447. struct st5481_adapter *adapter = fsm->userdata;
  448. long buf_nr = (long) arg;
  449. usb_d_out(adapter, buf_nr);
  450. }
  451. static void dout_ignore(struct FsmInst *fsm, int event, void *arg)
  452. {
  453. }
  454. static struct FsmNode DoutFnList[] __initdata =
  455. {
  456. {ST_DOUT_NONE, EV_DOUT_START_XMIT, dout_start_xmit},
  457. {ST_DOUT_SHORT_INIT, EV_DOUT_COMPLETE, dout_short_fifo},
  458. {ST_DOUT_SHORT_WAIT_DEN, EV_DOUT_DEN, dout_end_short_frame},
  459. {ST_DOUT_SHORT_WAIT_DEN, EV_DOUT_UNDERRUN, dout_underrun},
  460. {ST_DOUT_LONG_INIT, EV_DOUT_COMPLETE, dout_long_enable_fifo},
  461. {ST_DOUT_LONG_WAIT_DEN, EV_DOUT_DEN, dout_long_den},
  462. {ST_DOUT_LONG_WAIT_DEN, EV_DOUT_UNDERRUN, dout_underrun},
  463. {ST_DOUT_NORMAL, EV_DOUT_UNDERRUN, dout_underrun},
  464. {ST_DOUT_NORMAL, EV_DOUT_COMPLETE, dout_complete},
  465. {ST_DOUT_WAIT_FOR_UNDERRUN, EV_DOUT_UNDERRUN, dout_underrun},
  466. {ST_DOUT_WAIT_FOR_UNDERRUN, EV_DOUT_COMPLETE, dout_ignore},
  467. {ST_DOUT_WAIT_FOR_NOT_BUSY, EV_DOUT_COMPLETE, dout_check_busy},
  468. {ST_DOUT_WAIT_FOR_STOP, EV_DOUT_STOPPED, dout_reset},
  469. {ST_DOUT_WAIT_FOR_RESET, EV_DOUT_RESETED, dout_reseted},
  470. };
  471. void st5481_d_l2l1(struct hisax_if *hisax_d_if, int pr, void *arg)
  472. {
  473. struct st5481_adapter *adapter = hisax_d_if->priv;
  474. struct sk_buff *skb = arg;
  475. switch (pr) {
  476. case PH_ACTIVATE | REQUEST:
  477. FsmEvent(&adapter->l1m, EV_PH_ACTIVATE_REQ, NULL);
  478. break;
  479. case PH_DEACTIVATE | REQUEST:
  480. FsmEvent(&adapter->l1m, EV_PH_DEACTIVATE_REQ, NULL);
  481. break;
  482. case PH_DATA | REQUEST:
  483. DBG(2, "PH_DATA REQUEST len %d", skb->len);
  484. BUG_ON(adapter->d_out.tx_skb);
  485. adapter->d_out.tx_skb = skb;
  486. FsmEvent(&adapter->d_out.fsm, EV_DOUT_START_XMIT, NULL);
  487. break;
  488. default:
  489. WARNING("pr %#x\n", pr);
  490. break;
  491. }
  492. }
  493. /* ======================================================================
  494. */
  495. /*
  496. * Start receiving on the D channel since entered state F7.
  497. */
  498. static void ph_connect(struct st5481_adapter *adapter)
  499. {
  500. struct st5481_d_out *d_out = &adapter->d_out;
  501. struct st5481_in *d_in = &adapter->d_in;
  502. DBG(8, "");
  503. FsmChangeState(&d_out->fsm, ST_DOUT_NONE);
  504. // st5481_usb_device_ctrl_msg(adapter, FFMSK_D, OUT_UNDERRUN, NULL, NULL);
  505. st5481_usb_device_ctrl_msg(adapter, FFMSK_D, 0xfc, NULL, NULL);
  506. st5481_in_mode(d_in, L1_MODE_HDLC);
  507. #ifdef LOOPBACK
  508. // Turn loopback on (data sent on B and D looped back)
  509. st5481_usb_device_ctrl_msg(cs, LBB, 0x04, NULL, NULL);
  510. #endif
  511. st5481_usb_pipe_reset(adapter, EP_D_OUT | USB_DIR_OUT, NULL, NULL);
  512. // Turn on the green LED to tell that we are in state F7
  513. adapter->leds |= GREEN_LED;
  514. st5481_usb_device_ctrl_msg(adapter, GPIO_OUT, adapter->leds, NULL, NULL);
  515. }
  516. /*
  517. * Stop receiving on the D channel since not in state F7.
  518. */
  519. static void ph_disconnect(struct st5481_adapter *adapter)
  520. {
  521. DBG(8, "");
  522. st5481_in_mode(&adapter->d_in, L1_MODE_NULL);
  523. // Turn off the green LED to tell that we left state F7
  524. adapter->leds &= ~GREEN_LED;
  525. st5481_usb_device_ctrl_msg(adapter, GPIO_OUT, adapter->leds, NULL, NULL);
  526. }
  527. static int st5481_setup_d_out(struct st5481_adapter *adapter)
  528. {
  529. struct usb_device *dev = adapter->usb_dev;
  530. struct usb_interface *intf;
  531. struct usb_host_interface *altsetting = NULL;
  532. struct usb_host_endpoint *endpoint;
  533. struct st5481_d_out *d_out = &adapter->d_out;
  534. DBG(2, "");
  535. intf = usb_ifnum_to_if(dev, 0);
  536. if (intf)
  537. altsetting = usb_altnum_to_altsetting(intf, 3);
  538. if (!altsetting)
  539. return -ENXIO;
  540. // Allocate URBs and buffers for the D channel out
  541. endpoint = &altsetting->endpoint[EP_D_OUT-1];
  542. DBG(2, "endpoint address=%02x,packet size=%d",
  543. endpoint->desc.bEndpointAddress, le16_to_cpu(endpoint->desc.wMaxPacketSize));
  544. return st5481_setup_isocpipes(d_out->urb, dev,
  545. usb_sndisocpipe(dev, endpoint->desc.bEndpointAddress),
  546. NUM_ISO_PACKETS_D, SIZE_ISO_PACKETS_D_OUT,
  547. NUM_ISO_PACKETS_D * SIZE_ISO_PACKETS_D_OUT,
  548. usb_d_out_complete, adapter);
  549. }
  550. static void st5481_release_d_out(struct st5481_adapter *adapter)
  551. {
  552. struct st5481_d_out *d_out = &adapter->d_out;
  553. DBG(2, "");
  554. st5481_release_isocpipes(d_out->urb);
  555. }
  556. int st5481_setup_d(struct st5481_adapter *adapter)
  557. {
  558. int retval;
  559. DBG(2, "");
  560. retval = st5481_setup_d_out(adapter);
  561. if (retval)
  562. goto err;
  563. adapter->d_in.bufsize = MAX_DFRAME_LEN_L1;
  564. adapter->d_in.num_packets = NUM_ISO_PACKETS_D;
  565. adapter->d_in.packet_size = SIZE_ISO_PACKETS_D_IN;
  566. adapter->d_in.ep = EP_D_IN | USB_DIR_IN;
  567. adapter->d_in.counter = IN_D_COUNTER;
  568. adapter->d_in.adapter = adapter;
  569. adapter->d_in.hisax_if = &adapter->hisax_d_if.ifc;
  570. retval = st5481_setup_in(&adapter->d_in);
  571. if (retval)
  572. goto err_d_out;
  573. adapter->l1m.fsm = &l1fsm;
  574. adapter->l1m.state = ST_L1_F3;
  575. adapter->l1m.debug = st5481_debug & 0x100;
  576. adapter->l1m.userdata = adapter;
  577. adapter->l1m.printdebug = l1m_debug;
  578. FsmInitTimer(&adapter->l1m, &adapter->timer);
  579. adapter->d_out.fsm.fsm = &dout_fsm;
  580. adapter->d_out.fsm.state = ST_DOUT_NONE;
  581. adapter->d_out.fsm.debug = st5481_debug & 0x100;
  582. adapter->d_out.fsm.userdata = adapter;
  583. adapter->d_out.fsm.printdebug = dout_debug;
  584. return 0;
  585. err_d_out:
  586. st5481_release_d_out(adapter);
  587. err:
  588. return retval;
  589. }
  590. void st5481_release_d(struct st5481_adapter *adapter)
  591. {
  592. DBG(2, "");
  593. st5481_release_in(&adapter->d_in);
  594. st5481_release_d_out(adapter);
  595. }
  596. /* ======================================================================
  597. * init / exit
  598. */
  599. int __init st5481_d_init(void)
  600. {
  601. int retval;
  602. l1fsm.state_count = L1_STATE_COUNT;
  603. l1fsm.event_count = L1_EVENT_COUNT;
  604. l1fsm.strEvent = strL1Event;
  605. l1fsm.strState = strL1State;
  606. retval = FsmNew(&l1fsm, L1FnList, ARRAY_SIZE(L1FnList));
  607. if (retval)
  608. goto err;
  609. dout_fsm.state_count = DOUT_STATE_COUNT;
  610. dout_fsm.event_count = DOUT_EVENT_COUNT;
  611. dout_fsm.strEvent = strDoutEvent;
  612. dout_fsm.strState = strDoutState;
  613. retval = FsmNew(&dout_fsm, DoutFnList, ARRAY_SIZE(DoutFnList));
  614. if (retval)
  615. goto err_l1;
  616. return 0;
  617. err_l1:
  618. FsmFree(&l1fsm);
  619. err:
  620. return retval;
  621. }
  622. // can't be __exit
  623. void st5481_d_exit(void)
  624. {
  625. FsmFree(&l1fsm);
  626. FsmFree(&dout_fsm);
  627. }