st5481_d.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:19k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  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/usb.h>
  14. #include <linux/slab.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 void l1m_debug(struct FsmInst *fi, char *fmt, ...)
  142. {
  143. va_list args;
  144. char buf[256];
  145. va_start(args, fmt);
  146. vsprintf(buf, fmt, args);
  147. DBG(8, "%s", buf);
  148. va_end(args);
  149. }
  150. /* ======================================================================
  151.  * D-Channel out
  152.  */
  153. /*
  154.   D OUT state machine:
  155.   ====================
  156.   Transmit short frame (< 16 bytes of encoded data):
  157.   L1 FRAME    D_OUT_STATE           USB                  D CHANNEL
  158.   --------    -----------           ---                  ---------
  159.  
  160.               FIXME
  161.  -> [xx..xx]  SHORT_INIT            -> [7Exx..xxC1C27EFF]
  162.               SHORT_WAIT_DEN        <> OUT_D_COUNTER=16 
  163.                                                  
  164.               END_OF_SHORT          <- DEN_EVENT         -> 7Exx
  165.                                                           xxxx 
  166.                                                           xxxx
  167.   xxxx 
  168.   xxxx
  169.   xxxx
  170.   C1C1 
  171.   7EFF 
  172.               WAIT_FOR_RESET_IDLE   <- D_UNDERRUN        <- (8ms)                        
  173.               IDLE                  <> Reset pipe
  174.               
  175.   Transmit long frame (>= 16 bytes of encoded data):
  176.   L1 FRAME    D_OUT_STATE           USB                  D CHANNEL
  177.   --------    -----------           ---                  ---------
  178.  -> [xx...xx] IDLE
  179.               WAIT_FOR_STOP         <> OUT_D_COUNTER=0
  180.               WAIT_FOR_RESET        <> Reset pipe
  181.       STOP
  182.       INIT_LONG_FRAME       -> [7Exx..xx]
  183.               WAIT_DEN              <> OUT_D_COUNTER=16 
  184.               OUT_NORMAL            <- DEN_EVENT       -> 7Exx
  185.               END_OF_FRAME_BUSY     -> [xxxx]             xxxx 
  186.               END_OF_FRAME_NOT_BUSY -> [xxxx]             xxxx
  187.     -> [xxxx]   xxxx 
  188.     -> [C1C2]   xxxx
  189.     -> [7EFF]   xxxx
  190.   xxxx 
  191.   xxxx 
  192.                                                           ....
  193.   xxxx
  194.   C1C2
  195.   7EFF
  196.                       <- D_UNDERRUN      <- (> 8ms)                        
  197.               WAIT_FOR_STOP         <> OUT_D_COUNTER=0
  198.               WAIT_FOR_RESET        <> Reset pipe
  199.       STOP
  200. */          
  201. static struct Fsm dout_fsm;
  202. static char *strDoutState[] =
  203. {
  204. "ST_DOUT_NONE",
  205. "ST_DOUT_SHORT_INIT",
  206. "ST_DOUT_SHORT_WAIT_DEN",
  207. "ST_DOUT_LONG_INIT",
  208. "ST_DOUT_LONG_WAIT_DEN",
  209. "ST_DOUT_NORMAL",
  210. "ST_DOUT_WAIT_FOR_UNDERRUN",
  211.         "ST_DOUT_WAIT_FOR_NOT_BUSY",
  212. "ST_DOUT_WAIT_FOR_STOP",
  213. "ST_DOUT_WAIT_FOR_RESET",
  214. };
  215. static char *strDoutEvent[] =
  216. {
  217. "EV_DOUT_START_XMIT",
  218. "EV_DOUT_COMPLETE",
  219. "EV_DOUT_DEN",
  220. "EV_DOUT_RESETED",
  221. "EV_DOUT_STOPPED",
  222. "EV_DOUT_COLL",
  223. "EV_DOUT_UNDERRUN",
  224. };
  225. static void dout_debug(struct FsmInst *fi, char *fmt, ...)
  226. {
  227. va_list args;
  228. char buf[256];
  229. va_start(args, fmt);
  230. vsprintf(buf, fmt, args);
  231. DBG(0x2, "%s", buf);
  232. va_end(args);
  233. }
  234. static void dout_stop_event(void *context)
  235. {
  236. struct st5481_adapter *adapter = context;
  237. FsmEvent(&adapter->d_out.fsm, EV_DOUT_STOPPED, NULL);
  238. }
  239. /*
  240.  * Start the transfer of a D channel frame.
  241.  */
  242. static void usb_d_out(struct st5481_adapter *adapter, int buf_nr)
  243. {
  244. struct st5481_d_out *d_out = &adapter->d_out;
  245. struct urb *urb;
  246. unsigned int num_packets, packet_offset;
  247. int len, buf_size, bytes_sent;
  248. struct sk_buff *skb;
  249. iso_packet_descriptor_t *desc;
  250. if (d_out->fsm.state != ST_DOUT_NORMAL)
  251. return;
  252. if (test_and_set_bit(buf_nr, &d_out->busy)) {
  253. DBG(2, "ep %d urb %d busy %#lx", EP_D_OUT, buf_nr, d_out->busy);
  254. return;
  255. }
  256. urb = d_out->urb[buf_nr];
  257. skb = d_out->tx_skb;
  258. buf_size = NUM_ISO_PACKETS_D * SIZE_ISO_PACKETS_D_OUT;
  259. if (skb) {
  260. len = hdlc_encode(&d_out->hdlc_state, 
  261.   skb->data, skb->len, &bytes_sent,
  262.   urb->transfer_buffer, buf_size);
  263. skb_pull(skb,bytes_sent);
  264. } else {
  265. // Send flags or idle
  266. len = hdlc_encode(&d_out->hdlc_state, 
  267.   NULL, 0, &bytes_sent,
  268.   urb->transfer_buffer, buf_size);
  269. }
  270. if (len < buf_size) {
  271. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_UNDERRUN);
  272. }
  273. if (skb && !skb->len) {
  274. d_out->tx_skb = NULL;
  275. D_L1L2(adapter, PH_DATA | CONFIRM, NULL);
  276. dev_kfree_skb_any(skb);
  277. }
  278. // Prepare the URB
  279. urb->transfer_buffer_length = len;
  280. num_packets = 0;
  281. packet_offset = 0;
  282. while (packet_offset < len) {
  283. desc = &urb->iso_frame_desc[num_packets];
  284. desc->offset = packet_offset;
  285. desc->length = SIZE_ISO_PACKETS_D_OUT;
  286. if (len - packet_offset < desc->length)
  287. desc->length = len - packet_offset;
  288. num_packets++;
  289. packet_offset += desc->length;
  290. }
  291. urb->number_of_packets = num_packets;
  292. // Prepare the URB
  293. urb->dev = adapter->usb_dev;
  294. // Need to transmit the next buffer 2ms after the DEN_EVENT
  295. urb->transfer_flags = 0;
  296. urb->start_frame = usb_get_current_frame_number(adapter->usb_dev)+2;
  297. DBG_ISO_PACKET(0x20,urb);
  298. if (usb_submit_urb(urb) < 0) {
  299. // There is another URB queued up
  300. urb->transfer_flags = USB_ISO_ASAP;
  301. SUBMIT_URB(urb);
  302. }
  303. }
  304. static void fifo_reseted(void *context)
  305. {
  306. struct st5481_adapter *adapter = context;
  307. FsmEvent(&adapter->d_out.fsm, EV_DOUT_RESETED, NULL);
  308. }
  309. static void usb_d_out_complete(struct urb *urb)
  310. {
  311. struct st5481_adapter *adapter = urb->context;
  312. struct st5481_d_out *d_out = &adapter->d_out;
  313. int buf_nr;
  314. DBG(2, "");
  315. buf_nr = get_buf_nr(d_out->urb, urb);
  316. test_and_clear_bit(buf_nr, &d_out->busy);
  317. if (urb->status < 0) {
  318. if (urb->status != USB_ST_URB_KILLED) {
  319. WARN("urb status %d",urb->status);
  320. if (d_out->busy == 0) {
  321. st5481_usb_pipe_reset(adapter, EP_D_OUT | USB_DIR_OUT, fifo_reseted, adapter);
  322. }
  323. return;
  324. } else {
  325. DBG(1,"urb killed"); 
  326. return; // Give up
  327. }
  328. }
  329. FsmEvent(&adapter->d_out.fsm, EV_DOUT_COMPLETE, (void *) buf_nr);
  330. }
  331. /* ====================================================================== */
  332. static void dout_start_xmit(struct FsmInst *fsm, int event, void *arg)
  333. {
  334. // FIXME unify?
  335. struct st5481_adapter *adapter = fsm->userdata;
  336. struct st5481_d_out *d_out = &adapter->d_out;
  337. struct urb *urb;
  338. int len, bytes_sent;
  339. struct sk_buff *skb;
  340. int buf_nr = 0;
  341. skb = d_out->tx_skb;
  342. DBG(2,"len=%d",skb->len);
  343. hdlc_out_init(&d_out->hdlc_state, 1, 0);
  344. if (test_and_set_bit(buf_nr, &d_out->busy)) {
  345. WARN("ep %d urb %d busy %#lx", EP_D_OUT, buf_nr, d_out->busy);
  346. return;
  347. }
  348. urb = d_out->urb[buf_nr];
  349. DBG_SKB(0x10, skb);
  350. len = hdlc_encode(&d_out->hdlc_state, 
  351.   skb->data, skb->len, &bytes_sent,
  352.   urb->transfer_buffer, 16);
  353. skb_pull(skb, bytes_sent);
  354. if(len < 16)
  355. FsmChangeState(&d_out->fsm, ST_DOUT_SHORT_INIT);
  356. else
  357. FsmChangeState(&d_out->fsm, ST_DOUT_LONG_INIT);
  358. if (skb->len == 0) {
  359. d_out->tx_skb = NULL;
  360. D_L1L2(adapter, PH_DATA | CONFIRM, NULL);
  361. dev_kfree_skb_any(skb);
  362. }
  363. // Prepare the URB
  364. urb->transfer_buffer_length = len;
  365. urb->iso_frame_desc[0].offset = 0;
  366. urb->iso_frame_desc[0].length = len;
  367. urb->number_of_packets = 1;
  368. // Prepare the URB
  369. urb->dev = adapter->usb_dev;
  370. urb->transfer_flags = USB_ISO_ASAP;
  371. DBG_ISO_PACKET(0x20,urb);
  372. SUBMIT_URB(urb);
  373. }
  374. static void dout_short_fifo(struct FsmInst *fsm, int event, void *arg)
  375. {
  376. struct st5481_adapter *adapter = fsm->userdata;
  377. struct st5481_d_out *d_out = &adapter->d_out;
  378. FsmChangeState(&d_out->fsm, ST_DOUT_SHORT_WAIT_DEN);
  379. st5481_usb_device_ctrl_msg(adapter, OUT_D_COUNTER, 16, NULL, NULL);
  380. }
  381. static void dout_end_short_frame(struct FsmInst *fsm, int event, void *arg)
  382. {
  383. struct st5481_adapter *adapter = fsm->userdata;
  384. struct st5481_d_out *d_out = &adapter->d_out;
  385. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_UNDERRUN);
  386. }
  387. static void dout_long_enable_fifo(struct FsmInst *fsm, int event, void *arg)
  388. {
  389. struct st5481_adapter *adapter = fsm->userdata;
  390. struct st5481_d_out *d_out = &adapter->d_out;
  391.     
  392. st5481_usb_device_ctrl_msg(adapter, OUT_D_COUNTER, 16, NULL, NULL);
  393. FsmChangeState(&d_out->fsm, ST_DOUT_LONG_WAIT_DEN);
  394. }
  395. static void dout_long_den(struct FsmInst *fsm, int event, void *arg)
  396. {
  397. struct st5481_adapter *adapter = fsm->userdata;
  398. struct st5481_d_out *d_out = &adapter->d_out;
  399. FsmChangeState(&d_out->fsm, ST_DOUT_NORMAL);
  400. usb_d_out(adapter, 0);
  401. usb_d_out(adapter, 1);
  402. }
  403. static void dout_reset(struct FsmInst *fsm, int event, void *arg)
  404. {
  405. struct st5481_adapter *adapter = fsm->userdata;
  406. struct st5481_d_out *d_out = &adapter->d_out;
  407. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_RESET);
  408. st5481_usb_pipe_reset(adapter, EP_D_OUT | USB_DIR_OUT, fifo_reseted, adapter);
  409. }
  410. static void dout_stop(struct FsmInst *fsm, int event, void *arg)
  411. {
  412. struct st5481_adapter *adapter = fsm->userdata;
  413. struct st5481_d_out *d_out = &adapter->d_out;
  414. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_STOP);
  415. st5481_usb_device_ctrl_msg(adapter, OUT_D_COUNTER, 0, dout_stop_event, adapter);
  416. }
  417. static void dout_underrun(struct FsmInst *fsm, int event, void *arg)
  418. {
  419. struct st5481_adapter *adapter = fsm->userdata;
  420. struct st5481_d_out *d_out = &adapter->d_out;
  421. if (test_bit(0, &d_out->busy) || test_bit(1, &d_out->busy)) {
  422. FsmChangeState(&d_out->fsm, ST_DOUT_WAIT_FOR_NOT_BUSY);
  423. }  else {
  424. dout_stop(fsm, event, arg);
  425. }
  426. }
  427. static void dout_check_busy(struct FsmInst *fsm, int event, void *arg)
  428. {
  429. struct st5481_adapter *adapter = fsm->userdata;
  430. struct st5481_d_out *d_out = &adapter->d_out;
  431. if (!test_bit(0, &d_out->busy) && !test_bit(1, &d_out->busy))
  432. dout_stop(fsm, event, arg);
  433. }
  434. static void dout_reseted(struct FsmInst *fsm, int event, void *arg)
  435. {
  436. struct st5481_adapter *adapter = fsm->userdata;
  437. struct st5481_d_out *d_out = &adapter->d_out;
  438. FsmChangeState(&d_out->fsm, ST_DOUT_NONE);
  439. // FIXME locking
  440. if (d_out->tx_skb)
  441. FsmEvent(&d_out->fsm, EV_DOUT_START_XMIT, NULL);
  442. }
  443. static void dout_complete(struct FsmInst *fsm, int event, void *arg)
  444. {
  445. struct st5481_adapter *adapter = fsm->userdata;
  446. int buf_nr = (int) arg;
  447. usb_d_out(adapter, buf_nr);
  448. }
  449. static void dout_ignore(struct FsmInst *fsm, int event, void *arg)
  450. {
  451. }
  452. static struct FsmNode DoutFnList[] __initdata =
  453. {
  454. {ST_DOUT_NONE,                   EV_DOUT_START_XMIT,   dout_start_xmit},
  455. {ST_DOUT_SHORT_INIT,             EV_DOUT_COMPLETE,     dout_short_fifo},
  456. {ST_DOUT_SHORT_WAIT_DEN,         EV_DOUT_DEN,          dout_end_short_frame},
  457. {ST_DOUT_SHORT_WAIT_DEN,         EV_DOUT_UNDERRUN,     dout_underrun},
  458. {ST_DOUT_LONG_INIT,              EV_DOUT_COMPLETE,     dout_long_enable_fifo},
  459. {ST_DOUT_LONG_WAIT_DEN,          EV_DOUT_DEN,          dout_long_den},
  460. {ST_DOUT_LONG_WAIT_DEN,          EV_DOUT_UNDERRUN,     dout_underrun},
  461. {ST_DOUT_NORMAL,                 EV_DOUT_UNDERRUN,     dout_underrun},
  462. {ST_DOUT_NORMAL,                 EV_DOUT_COMPLETE,     dout_complete},
  463. {ST_DOUT_WAIT_FOR_UNDERRUN,      EV_DOUT_UNDERRUN,     dout_underrun},
  464. {ST_DOUT_WAIT_FOR_UNDERRUN,      EV_DOUT_COMPLETE,     dout_ignore},
  465. {ST_DOUT_WAIT_FOR_NOT_BUSY,      EV_DOUT_COMPLETE,     dout_check_busy},
  466. {ST_DOUT_WAIT_FOR_STOP,          EV_DOUT_STOPPED,      dout_reset},
  467. {ST_DOUT_WAIT_FOR_RESET,         EV_DOUT_RESETED,      dout_reseted},
  468. };
  469. void st5481_d_l2l1(struct hisax_if *hisax_d_if, int pr, void *arg)
  470. {
  471. struct st5481_adapter *adapter = hisax_d_if->priv;
  472. struct sk_buff *skb = arg;
  473. switch (pr) {
  474. case PH_ACTIVATE | REQUEST:
  475. FsmEvent(&adapter->l1m, EV_PH_ACTIVATE_REQ, NULL);
  476. break;
  477. case PH_DEACTIVATE | REQUEST:
  478. FsmEvent(&adapter->l1m, EV_PH_DEACTIVATE_REQ, NULL);
  479. break;
  480. case PH_DATA | REQUEST:
  481. DBG(2, "PH_DATA REQUEST len %d", skb->len);
  482. if (adapter->d_out.tx_skb)
  483. BUG();
  484. adapter->d_out.tx_skb = skb;
  485. FsmEvent(&adapter->d_out.fsm, EV_DOUT_START_XMIT, NULL);
  486. break;
  487. default:
  488. WARN("pr %#xn", pr);
  489. break;
  490. }
  491. }
  492. /* ======================================================================
  493.  */
  494. /*
  495.  * Start receiving on the D channel since entered state F7.
  496.  */
  497. static void ph_connect(struct st5481_adapter *adapter)
  498. {
  499. struct st5481_d_out *d_out = &adapter->d_out;
  500. struct st5481_in *d_in = &adapter->d_in;
  501. DBG(8,"");
  502. FsmChangeState(&d_out->fsm, ST_DOUT_NONE);
  503. // st5481_usb_device_ctrl_msg(adapter, FFMSK_D, OUT_UNDERRUN, NULL, NULL);
  504. st5481_usb_device_ctrl_msg(adapter, FFMSK_D, 0xfc, NULL, NULL);
  505. st5481_in_mode(d_in, L1_MODE_HDLC);
  506. #if LOOPBACK
  507. // Turn loopback on (data sent on B and D looped back)
  508. st5481_usb_device_ctrl_msg(cs, LBB, 0x04, NULL, NULL);
  509. #endif
  510. st5481_usb_pipe_reset(adapter, EP_D_OUT | USB_DIR_OUT, NULL, NULL);
  511. // Turn on the green LED to tell that we are in state F7
  512. adapter->leds |= GREEN_LED;
  513. st5481_usb_device_ctrl_msg(adapter, GPIO_OUT, adapter->leds, NULL, NULL);
  514. }
  515. /*
  516.  * Stop receiving on the D channel since not in state F7.
  517.  */
  518. static void ph_disconnect(struct st5481_adapter *adapter)
  519. {
  520. DBG(8,"");
  521. st5481_in_mode(&adapter->d_in, L1_MODE_NULL);
  522. // Turn off the green LED to tell that we left state F7
  523. adapter->leds &= ~GREEN_LED;
  524. st5481_usb_device_ctrl_msg(adapter, GPIO_OUT, adapter->leds, NULL, NULL);
  525. }
  526. static int __devinit st5481_setup_d_out(struct st5481_adapter *adapter)
  527. {
  528. struct usb_device *dev = adapter->usb_dev;
  529. struct usb_interface_descriptor *altsetting;
  530. struct usb_endpoint_descriptor *endpoint;
  531. struct st5481_d_out *d_out = &adapter->d_out;
  532. DBG(2,"");
  533. altsetting = &(dev->config->interface[0].altsetting[3]);
  534. // Allocate URBs and buffers for the D channel out
  535. endpoint = &altsetting->endpoint[EP_D_OUT-1];
  536. DBG(2,"endpoint address=%02x,packet size=%d",
  537.     endpoint->bEndpointAddress,endpoint->wMaxPacketSize);
  538. return st5481_setup_isocpipes(d_out->urb, dev, 
  539.       usb_sndisocpipe(dev, endpoint->bEndpointAddress),
  540.       NUM_ISO_PACKETS_D, SIZE_ISO_PACKETS_D_OUT,
  541.       NUM_ISO_PACKETS_D * SIZE_ISO_PACKETS_D_OUT,
  542.       usb_d_out_complete, adapter);
  543. }
  544. static void st5481_release_d_out(struct st5481_adapter *adapter)
  545. {
  546. struct st5481_d_out *d_out = &adapter->d_out;
  547. DBG(2,"");
  548. st5481_release_isocpipes(d_out->urb);
  549. }
  550. int __devinit st5481_setup_d(struct st5481_adapter *adapter)
  551. {
  552. int retval;
  553. DBG(2,"");
  554. retval = st5481_setup_d_out(adapter);
  555. if (retval)
  556. goto err;
  557. adapter->d_in.bufsize = MAX_DFRAME_LEN_L1;
  558. adapter->d_in.num_packets = NUM_ISO_PACKETS_D;
  559. adapter->d_in.packet_size = SIZE_ISO_PACKETS_D_IN;
  560. adapter->d_in.ep = EP_D_IN | USB_DIR_IN;
  561. adapter->d_in.counter = IN_D_COUNTER;
  562. adapter->d_in.adapter = adapter;
  563. adapter->d_in.hisax_if = &adapter->hisax_d_if.ifc;
  564. retval = st5481_setup_in(&adapter->d_in);
  565. if (retval)
  566. goto err_d_out;
  567. adapter->l1m.fsm = &l1fsm;
  568. adapter->l1m.state = ST_L1_F3;
  569. adapter->l1m.debug = 1;
  570. adapter->l1m.userdata = adapter;
  571. adapter->l1m.printdebug = l1m_debug;
  572. FsmInitTimer(&adapter->l1m, &adapter->timer);
  573. adapter->d_out.fsm.fsm = &dout_fsm;
  574. adapter->d_out.fsm.state = ST_DOUT_NONE;
  575. adapter->d_out.fsm.debug = 1;
  576. adapter->d_out.fsm.userdata = adapter;
  577. adapter->d_out.fsm.printdebug = dout_debug;
  578. return 0;
  579.  err_d_out:
  580. st5481_release_d_out(adapter);
  581.  err:
  582. return retval;
  583. }
  584. void st5481_release_d(struct st5481_adapter *adapter)
  585. {
  586. DBG(2,"");
  587. st5481_release_in(&adapter->d_in);
  588. st5481_release_d_out(adapter);
  589. }
  590. /* ======================================================================
  591.  * init / exit
  592.  */
  593. int __init st5481_d_init(void)
  594. {
  595. int retval;
  596. l1fsm.state_count = L1_STATE_COUNT;
  597. l1fsm.event_count = L1_EVENT_COUNT;
  598. l1fsm.strEvent = strL1Event;
  599. l1fsm.strState = strL1State;
  600. retval = FsmNew(&l1fsm, L1FnList, ARRAY_SIZE(L1FnList));
  601. if (retval)
  602. goto err;
  603. dout_fsm.state_count = DOUT_STATE_COUNT;
  604. dout_fsm.event_count = DOUT_EVENT_COUNT;
  605. dout_fsm.strEvent = strDoutEvent;
  606. dout_fsm.strState = strDoutState;
  607. retval = FsmNew(&dout_fsm, DoutFnList, ARRAY_SIZE(DoutFnList));
  608. if (retval)
  609. goto err_l1;
  610. return 0;
  611.  err_l1:
  612. FsmFree(&l1fsm);
  613.  err:
  614. return retval;
  615. }
  616. // can't be __exit
  617. void st5481_d_exit(void)
  618. {
  619. FsmFree(&l1fsm);
  620. FsmFree(&dout_fsm);
  621. }