st5481.h 14 KB

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