st5481.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #ifndef _ST5481_H_
  13. #define _ST5481_H_
  14. #include <linux/config.h>
  15. // USB IDs, the Product Id is in the range 0x4810-0x481F
  16. #define ST_VENDOR_ID 0x0483
  17. #define ST5481_PRODUCT_ID 0x4810
  18. #define ST5481_PRODUCT_ID_MASK 0xFFF0
  19. // ST5481 endpoints when using alternative setting 3 (2B+D).
  20. // To get the endpoint address, OR with 0x80 for IN endpoints.
  21. #define EP_CTRL 0x00U /* Control endpoint */
  22. #define EP_INT 0x01U /* Interrupt endpoint */
  23. #define EP_B1_OUT 0x02U /* B1 channel out */
  24. #define EP_B1_IN 0x03U /* B1 channel in */
  25. #define EP_B2_OUT 0x04U /* B2 channel out */
  26. #define EP_B2_IN 0x05U /* B2 channel in */
  27. #define EP_D_OUT 0x06U /* D channel out */
  28. #define EP_D_IN 0x07U /* D channel in */
  29. // Number of isochronous packets. With 20 packets we get
  30. // 50 interrupts/sec for each endpoint.
  31. #define NUM_ISO_PACKETS_D 20
  32. #define NUM_ISO_PACKETS_B 20
  33. // Size of each isochronous packet.
  34. // In outgoing direction we need to match ISDN data rates:
  35. // D: 2 bytes / msec -> 16 kbit / s
  36. // B: 16 bytes / msec -> 64 kbit / s
  37. #define SIZE_ISO_PACKETS_D_IN 16
  38. #define SIZE_ISO_PACKETS_D_OUT 2
  39. #define SIZE_ISO_PACKETS_B_IN 32
  40. #define SIZE_ISO_PACKETS_B_OUT 8
  41. // If we overrun/underrun, we send one packet with +/- 2 bytes
  42. #define B_FLOW_ADJUST 2
  43. // Registers that are written using vendor specific device request
  44. // on endpoint 0.
  45. #define LBA 0x02 /* S loopback */
  46. #define SET_DEFAULT 0x06 /* Soft reset */
  47. #define LBB 0x1D /* S maintenance loopback */
  48. #define STT 0x1e /* S force transmission signals */
  49. #define SDA_MIN 0x20 /* SDA-sin minimal value */
  50. #define SDA_MAX 0x21 /* SDA-sin maximal value */
  51. #define SDELAY_VALUE 0x22 /* Delay between Tx and Rx clock */
  52. #define IN_D_COUNTER 0x36 /* D receive channel fifo counter */
  53. #define OUT_D_COUNTER 0x37 /* D transmit channel fifo counter */
  54. #define IN_B1_COUNTER 0x38 /* B1 receive channel fifo counter */
  55. #define OUT_B1_COUNTER 0x39 /* B1 transmit channel fifo counter */
  56. #define IN_B2_COUNTER 0x3a /* B2 receive channel fifo counter */
  57. #define OUT_B2_COUNTER 0x3b /* B2 transmit channel fifo counter */
  58. #define FFCTRL_IN_D 0x3C /* D receive channel fifo threshold low */
  59. #define FFCTRH_IN_D 0x3D /* D receive channel fifo threshold high */
  60. #define FFCTRL_OUT_D 0x3E /* D transmit channel fifo threshold low */
  61. #define FFCTRH_OUT_D 0x3F /* D transmit channel fifo threshold high */
  62. #define FFCTRL_IN_B1 0x40 /* B1 receive channel fifo threshold low */
  63. #define FFCTRH_IN_B1 0x41 /* B1 receive channel fifo threshold high */
  64. #define FFCTRL_OUT_B1 0x42 /* B1 transmit channel fifo threshold low */
  65. #define FFCTRH_OUT_B1 0x43 /* B1 transmit channel fifo threshold high */
  66. #define FFCTRL_IN_B2 0x44 /* B2 receive channel fifo threshold low */
  67. #define FFCTRH_IN_B2 0x45 /* B2 receive channel fifo threshold high */
  68. #define FFCTRL_OUT_B2 0x46 /* B2 transmit channel fifo threshold low */
  69. #define FFCTRH_OUT_B2 0x47 /* B2 transmit channel fifo threshold high */
  70. #define MPMSK 0x4A /* Multi purpose interrupt MASK register */
  71. #define FFMSK_D 0x4c /* D fifo interrupt MASK register */
  72. #define FFMSK_B1 0x4e /* B1 fifo interrupt MASK register */
  73. #define FFMSK_B2 0x50 /* B2 fifo interrupt MASK register */
  74. #define GPIO_DIR 0x52 /* GPIO pins direction registers */
  75. #define GPIO_OUT 0x53 /* GPIO pins output register */
  76. #define GPIO_IN 0x54 /* GPIO pins input register */
  77. #define TXCI 0x56 /* CI command to be transmitted */
  78. // Format of the interrupt packet received on endpoint 1:
  79. //
  80. // +--------+--------+--------+--------+--------+--------+
  81. // !MPINT !FFINT_D !FFINT_B1!FFINT_B2!CCIST !GPIO_INT!
  82. // +--------+--------+--------+--------+--------+--------+
  83. // Offsets in the interrupt packet
  84. #define MPINT 0
  85. #define FFINT_D 1
  86. #define FFINT_B1 2
  87. #define FFINT_B2 3
  88. #define CCIST 4
  89. #define GPIO_INT 5
  90. #define INT_PKT_SIZE 6
  91. // MPINT
  92. #define LSD_INT 0x80 /* S line activity detected */
  93. #define RXCI_INT 0x40 /* Indicate primitive arrived */
  94. #define DEN_INT 0x20 /* Signal enabling data out of D Tx fifo */
  95. #define DCOLL_INT 0x10 /* D channel collision */
  96. #define AMIVN_INT 0x04 /* AMI violation number reached 2 */
  97. #define INFOI_INT 0x04 /* INFOi changed */
  98. #define DRXON_INT 0x02 /* Reception channel active */
  99. #define GPCHG_INT 0x01 /* GPIO pin value changed */
  100. // FFINT_x
  101. #define IN_OVERRUN 0x80 /* In fifo overrun */
  102. #define OUT_UNDERRUN 0x40 /* Out fifo underrun */
  103. #define IN_UP 0x20 /* In fifo thresholdh up-crossed */
  104. #define IN_DOWN 0x10 /* In fifo thresholdl down-crossed */
  105. #define OUT_UP 0x08 /* Out fifo thresholdh up-crossed */
  106. #define OUT_DOWN 0x04 /* Out fifo thresholdl down-crossed */
  107. #define IN_COUNTER_ZEROED 0x02 /* In down-counter reached 0 */
  108. #define OUT_COUNTER_ZEROED 0x01 /* Out down-counter reached 0 */
  109. #define ANY_REC_INT (IN_OVERRUN+IN_UP+IN_DOWN+IN_COUNTER_ZEROED)
  110. #define ANY_XMIT_INT (OUT_UNDERRUN+OUT_UP+OUT_DOWN+OUT_COUNTER_ZEROED)
  111. // Level 1 commands that are sent using the TXCI device request
  112. #define ST5481_CMD_DR 0x0 /* Deactivation Request */
  113. #define ST5481_CMD_RES 0x1 /* state machine RESet */
  114. #define ST5481_CMD_TM1 0x2 /* Test Mode 1 */
  115. #define ST5481_CMD_TM2 0x3 /* Test Mode 2 */
  116. #define ST5481_CMD_PUP 0x7 /* Power UP */
  117. #define ST5481_CMD_AR8 0x8 /* Activation Request class 1 */
  118. #define ST5481_CMD_AR10 0x9 /* Activation Request class 2 */
  119. #define ST5481_CMD_ARL 0xA /* Activation Request Loopback */
  120. #define ST5481_CMD_PDN 0xF /* Power DoWn */
  121. // Turn on/off the LEDs using the GPIO device request.
  122. // To use the B LEDs, number_of_leds must be set to 4
  123. #define B1_LED 0x10U
  124. #define B2_LED 0x20U
  125. #define GREEN_LED 0x40U
  126. #define RED_LED 0x80U
  127. // D channel out states
  128. enum {
  129. ST_DOUT_NONE,
  130. ST_DOUT_SHORT_INIT,
  131. ST_DOUT_SHORT_WAIT_DEN,
  132. ST_DOUT_LONG_INIT,
  133. ST_DOUT_LONG_WAIT_DEN,
  134. ST_DOUT_NORMAL,
  135. ST_DOUT_WAIT_FOR_UNDERRUN,
  136. ST_DOUT_WAIT_FOR_NOT_BUSY,
  137. ST_DOUT_WAIT_FOR_STOP,
  138. ST_DOUT_WAIT_FOR_RESET,
  139. };
  140. #define DOUT_STATE_COUNT (ST_DOUT_WAIT_FOR_RESET + 1)
  141. // D channel out events
  142. enum {
  143. EV_DOUT_START_XMIT,
  144. EV_DOUT_COMPLETE,
  145. EV_DOUT_DEN,
  146. EV_DOUT_RESETED,
  147. EV_DOUT_STOPPED,
  148. EV_DOUT_COLL,
  149. EV_DOUT_UNDERRUN,
  150. };
  151. #define DOUT_EVENT_COUNT (EV_DOUT_UNDERRUN + 1)
  152. // ----------------------------------------------------------------------
  153. enum {
  154. ST_L1_F3,
  155. ST_L1_F4,
  156. ST_L1_F6,
  157. ST_L1_F7,
  158. ST_L1_F8,
  159. };
  160. #define L1_STATE_COUNT (ST_L1_F8+1)
  161. // The first 16 entries match the Level 1 indications that
  162. // are found at offset 4 (CCIST) in the interrupt packet
  163. enum {
  164. EV_IND_DP, // 0000 Deactivation Pending
  165. EV_IND_1, // 0001
  166. EV_IND_2, // 0010
  167. EV_IND_3, // 0011
  168. EV_IND_RSY, // 0100 ReSYnchronizing
  169. EV_IND_5, // 0101
  170. EV_IND_6, // 0110
  171. EV_IND_7, // 0111
  172. EV_IND_AP, // 1000 Activation Pending
  173. EV_IND_9, // 1001
  174. EV_IND_10, // 1010
  175. EV_IND_11, // 1011
  176. EV_IND_AI8, // 1100 Activation Indication class 8
  177. EV_IND_AI10,// 1101 Activation Indication class 10
  178. EV_IND_AIL, // 1110 Activation Indication Loopback
  179. EV_IND_DI, // 1111 Deactivation Indication
  180. EV_PH_ACTIVATE_REQ,
  181. EV_PH_DEACTIVATE_REQ,
  182. EV_TIMER3,
  183. };
  184. #define L1_EVENT_COUNT (EV_TIMER3 + 1)
  185. #define ERR(format, arg...) \
  186. printk(KERN_ERR "%s:%s: " format "\n" , __FILE__, __FUNCTION__ , ## arg)
  187. #define WARN(format, arg...) \
  188. printk(KERN_WARNING "%s:%s: " format "\n" , __FILE__, __FUNCTION__ , ## arg)
  189. #define INFO(format, arg...) \
  190. printk(KERN_INFO "%s:%s: " format "\n" , __FILE__, __FUNCTION__ , ## arg)
  191. #include "isdnhdlc.h"
  192. #include "fsm.h"
  193. #include "hisax_if.h"
  194. #include <linux/skbuff.h>
  195. /* ======================================================================
  196. * FIFO handling
  197. */
  198. /* Generic FIFO structure */
  199. struct fifo {
  200. u_char r,w,count,size;
  201. spinlock_t lock;
  202. };
  203. /*
  204. * Init an FIFO
  205. */
  206. static inline void fifo_init(struct fifo *fifo, int size)
  207. {
  208. fifo->r = fifo->w = fifo->count = 0;
  209. fifo->size = size;
  210. spin_lock_init(&fifo->lock);
  211. }
  212. /*
  213. * Add an entry to the FIFO
  214. */
  215. static inline int fifo_add(struct fifo *fifo)
  216. {
  217. unsigned long flags;
  218. int index;
  219. if (!fifo) {
  220. return -1;
  221. }
  222. spin_lock_irqsave(&fifo->lock, flags);
  223. if (fifo->count == fifo->size) {
  224. // FIFO full
  225. index = -1;
  226. } else {
  227. // Return index where to get the next data to add to the FIFO
  228. index = fifo->w++ & (fifo->size-1);
  229. fifo->count++;
  230. }
  231. spin_unlock_irqrestore(&fifo->lock, flags);
  232. return index;
  233. }
  234. /*
  235. * Remove an entry from the FIFO with the index returned.
  236. */
  237. static inline int fifo_remove(struct fifo *fifo)
  238. {
  239. unsigned long flags;
  240. int index;
  241. if (!fifo) {
  242. return -1;
  243. }
  244. spin_lock_irqsave(&fifo->lock, flags);
  245. if (!fifo->count) {
  246. // FIFO empty
  247. index = -1;
  248. } else {
  249. // Return index where to get the next data from the FIFO
  250. index = fifo->r++ & (fifo->size-1);
  251. fifo->count--;
  252. }
  253. spin_unlock_irqrestore(&fifo->lock, flags);
  254. return index;
  255. }
  256. /* ======================================================================
  257. * control pipe
  258. */
  259. typedef void (*ctrl_complete_t)(void *);
  260. typedef struct ctrl_msg {
  261. struct usb_ctrlrequest dr;
  262. ctrl_complete_t complete;
  263. void *context;
  264. } ctrl_msg;
  265. /* FIFO of ctrl messages waiting to be sent */
  266. #define MAX_EP0_MSG 16
  267. struct ctrl_msg_fifo {
  268. struct fifo f;
  269. struct ctrl_msg data[MAX_EP0_MSG];
  270. };
  271. #define MAX_DFRAME_LEN_L1 300
  272. #define HSCX_BUFMAX 4096
  273. struct st5481_ctrl {
  274. struct ctrl_msg_fifo msg_fifo;
  275. unsigned long busy;
  276. struct urb *urb;
  277. };
  278. struct st5481_intr {
  279. // struct evt_fifo evt_fifo;
  280. struct urb *urb;
  281. };
  282. struct st5481_d_out {
  283. struct isdnhdlc_vars hdlc_state;
  284. struct urb *urb[2]; /* double buffering */
  285. unsigned long busy;
  286. struct sk_buff *tx_skb;
  287. struct FsmInst fsm;
  288. };
  289. struct st5481_b_out {
  290. struct isdnhdlc_vars hdlc_state;
  291. struct urb *urb[2]; /* double buffering */
  292. u_char flow_event;
  293. u_long busy;
  294. struct sk_buff *tx_skb;
  295. };
  296. struct st5481_in {
  297. struct isdnhdlc_vars hdlc_state;
  298. struct urb *urb[2]; /* double buffering */
  299. int mode;
  300. int bufsize;
  301. unsigned int num_packets;
  302. unsigned int packet_size;
  303. unsigned char ep, counter;
  304. unsigned char *rcvbuf;
  305. struct st5481_adapter *adapter;
  306. struct hisax_if *hisax_if;
  307. };
  308. int st5481_setup_in(struct st5481_in *in);
  309. void st5481_release_in(struct st5481_in *in);
  310. void st5481_in_mode(struct st5481_in *in, int mode);
  311. struct st5481_bcs {
  312. struct hisax_b_if b_if;
  313. struct st5481_adapter *adapter;
  314. struct st5481_in b_in;
  315. struct st5481_b_out b_out;
  316. int channel;
  317. int mode;
  318. };
  319. struct st5481_adapter {
  320. struct list_head list;
  321. int number_of_leds;
  322. struct usb_device *usb_dev;
  323. struct hisax_d_if hisax_d_if;
  324. struct st5481_ctrl ctrl;
  325. struct st5481_intr intr;
  326. struct st5481_in d_in;
  327. struct st5481_d_out d_out;
  328. unsigned char leds;
  329. unsigned int led_counter;
  330. unsigned long event;
  331. struct FsmInst l1m;
  332. struct FsmTimer timer;
  333. struct st5481_bcs bcs[2];
  334. };
  335. #define TIMER3_VALUE 7000
  336. /* ======================================================================
  337. *
  338. */
  339. /*
  340. * Submit an URB with error reporting. This is a macro so
  341. * the __FUNCTION__ returns the caller function name.
  342. */
  343. #define SUBMIT_URB(urb, mem_flags) \
  344. ({ \
  345. int status; \
  346. if ((status = usb_submit_urb(urb, mem_flags)) < 0) { \
  347. WARN("usb_submit_urb failed,status=%d", status); \
  348. } \
  349. status; \
  350. })
  351. /*
  352. * USB double buffering, return the URB index (0 or 1).
  353. */
  354. static inline int get_buf_nr(struct urb *urbs[], struct urb *urb)
  355. {
  356. return (urbs[0]==urb ? 0 : 1);
  357. }
  358. /* ---------------------------------------------------------------------- */
  359. /* B Channel */
  360. int st5481_setup_b(struct st5481_bcs *bcs);
  361. void st5481_release_b(struct st5481_bcs *bcs);
  362. void st5481_d_l2l1(struct hisax_if *hisax_d_if, int pr, void *arg);
  363. /* D Channel */
  364. int st5481_setup_d(struct st5481_adapter *adapter);
  365. void st5481_release_d(struct st5481_adapter *adapter);
  366. void st5481_b_l2l1(struct hisax_if *b_if, int pr, void *arg);
  367. int st5481_d_init(void);
  368. void st5481_d_exit(void);
  369. /* USB */
  370. void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command);
  371. int st5481_setup_isocpipes(struct urb* urb[2], struct usb_device *dev,
  372. unsigned int pipe, int num_packets,
  373. int packet_size, int buf_size,
  374. usb_complete_t complete, void *context);
  375. void st5481_release_isocpipes(struct urb* urb[2]);
  376. void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
  377. u_char pipe, ctrl_complete_t complete, void *context);
  378. void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
  379. u8 request, u16 value,
  380. ctrl_complete_t complete, void *context);
  381. int st5481_setup_usb(struct st5481_adapter *adapter);
  382. void st5481_release_usb(struct st5481_adapter *adapter);
  383. void st5481_start(struct st5481_adapter *adapter);
  384. void st5481_stop(struct st5481_adapter *adapter);
  385. // ----------------------------------------------------------------------
  386. // debugging macros
  387. #define __debug_variable st5481_debug
  388. #include "hisax_debug.h"
  389. #ifdef CONFIG_HISAX_DEBUG
  390. extern int st5481_debug;
  391. #define DBG_ISO_PACKET(level,urb) \
  392. if (level & __debug_variable) dump_iso_packet(__FUNCTION__,urb)
  393. static void __attribute__((unused))
  394. dump_iso_packet(const char *name, struct urb *urb)
  395. {
  396. int i,j;
  397. int len,ofs;
  398. u_char *data;
  399. printk(KERN_DEBUG "%s: packets=%d,errors=%d\n",
  400. name,urb->number_of_packets,urb->error_count);
  401. for (i = 0; i < urb->number_of_packets; ++i) {
  402. if (urb->pipe & USB_DIR_IN) {
  403. len = urb->iso_frame_desc[i].actual_length;
  404. } else {
  405. len = urb->iso_frame_desc[i].length;
  406. }
  407. ofs = urb->iso_frame_desc[i].offset;
  408. printk(KERN_DEBUG "len=%.2d,ofs=%.3d ",len,ofs);
  409. if (len) {
  410. data = urb->transfer_buffer+ofs;
  411. for (j=0; j < len; j++) {
  412. printk ("%.2x", data[j]);
  413. }
  414. }
  415. printk("\n");
  416. }
  417. }
  418. static inline const char *ST5481_CMD_string(int evt)
  419. {
  420. static char s[16];
  421. switch (evt) {
  422. case ST5481_CMD_DR: return "DR";
  423. case ST5481_CMD_RES: return "RES";
  424. case ST5481_CMD_TM1: return "TM1";
  425. case ST5481_CMD_TM2: return "TM2";
  426. case ST5481_CMD_PUP: return "PUP";
  427. case ST5481_CMD_AR8: return "AR8";
  428. case ST5481_CMD_AR10: return "AR10";
  429. case ST5481_CMD_ARL: return "ARL";
  430. case ST5481_CMD_PDN: return "PDN";
  431. };
  432. sprintf(s,"0x%x",evt);
  433. return s;
  434. }
  435. #else
  436. #define DBG_ISO_PACKET(level,urb) do {} while (0)
  437. #endif
  438. #endif