hdlcdrv.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * hdlcdrv.h -- HDLC packet radio network driver.
  3. * The Linux soundcard driver for 1200 baud and 9600 baud packet radio
  4. * (C) 1996-1998 by Thomas Sailer, HB9JNX/AE4WA
  5. */
  6. #ifndef _HDLCDRV_H
  7. #define _HDLCDRV_H
  8. /* -------------------------------------------------------------------- */
  9. /*
  10. * structs for the IOCTL commands
  11. */
  12. struct hdlcdrv_params {
  13. int iobase;
  14. int irq;
  15. int dma;
  16. int dma2;
  17. int seriobase;
  18. int pariobase;
  19. int midiiobase;
  20. };
  21. struct hdlcdrv_channel_params {
  22. int tx_delay; /* the transmitter keyup delay in 10ms units */
  23. int tx_tail; /* the transmitter keyoff delay in 10ms units */
  24. int slottime; /* the slottime in 10ms; usually 10 = 100ms */
  25. int ppersist; /* the p-persistence 0..255 */
  26. int fulldup; /* some driver do not support full duplex, setting */
  27. /* this just makes them send even if DCD is on */
  28. };
  29. struct hdlcdrv_old_channel_state {
  30. int ptt;
  31. int dcd;
  32. int ptt_keyed;
  33. };
  34. struct hdlcdrv_channel_state {
  35. int ptt;
  36. int dcd;
  37. int ptt_keyed;
  38. unsigned long tx_packets;
  39. unsigned long tx_errors;
  40. unsigned long rx_packets;
  41. unsigned long rx_errors;
  42. };
  43. struct hdlcdrv_ioctl {
  44. int cmd;
  45. union {
  46. struct hdlcdrv_params mp;
  47. struct hdlcdrv_channel_params cp;
  48. struct hdlcdrv_channel_state cs;
  49. struct hdlcdrv_old_channel_state ocs;
  50. unsigned int calibrate;
  51. unsigned char bits;
  52. char modename[128];
  53. char drivername[32];
  54. } data;
  55. };
  56. /* -------------------------------------------------------------------- */
  57. /*
  58. * ioctl values
  59. */
  60. #define HDLCDRVCTL_GETMODEMPAR 0
  61. #define HDLCDRVCTL_SETMODEMPAR 1
  62. #define HDLCDRVCTL_MODEMPARMASK 2 /* not handled by hdlcdrv */
  63. #define HDLCDRVCTL_GETCHANNELPAR 10
  64. #define HDLCDRVCTL_SETCHANNELPAR 11
  65. #define HDLCDRVCTL_OLDGETSTAT 20
  66. #define HDLCDRVCTL_CALIBRATE 21
  67. #define HDLCDRVCTL_GETSTAT 22
  68. /*
  69. * these are mainly for debugging purposes
  70. */
  71. #define HDLCDRVCTL_GETSAMPLES 30
  72. #define HDLCDRVCTL_GETBITS 31
  73. /*
  74. * not handled by hdlcdrv, but by its depending drivers
  75. */
  76. #define HDLCDRVCTL_GETMODE 40
  77. #define HDLCDRVCTL_SETMODE 41
  78. #define HDLCDRVCTL_MODELIST 42
  79. #define HDLCDRVCTL_DRIVERNAME 43
  80. /*
  81. * mask of needed modem parameters, returned by HDLCDRVCTL_MODEMPARMASK
  82. */
  83. #define HDLCDRV_PARMASK_IOBASE (1<<0)
  84. #define HDLCDRV_PARMASK_IRQ (1<<1)
  85. #define HDLCDRV_PARMASK_DMA (1<<2)
  86. #define HDLCDRV_PARMASK_DMA2 (1<<3)
  87. #define HDLCDRV_PARMASK_SERIOBASE (1<<4)
  88. #define HDLCDRV_PARMASK_PARIOBASE (1<<5)
  89. #define HDLCDRV_PARMASK_MIDIIOBASE (1<<6)
  90. /* -------------------------------------------------------------------- */
  91. #ifdef __KERNEL__
  92. #include <linux/netdevice.h>
  93. #include <linux/if.h>
  94. #include <linux/spinlock.h>
  95. #define HDLCDRV_MAGIC 0x5ac6e778
  96. #define HDLCDRV_HDLCBUFFER 32 /* should be a power of 2 for speed reasons */
  97. #define HDLCDRV_BITBUFFER 256 /* should be a power of 2 for speed reasons */
  98. #undef HDLCDRV_LOOPBACK /* define for HDLC debugging purposes */
  99. #define HDLCDRV_DEBUG
  100. /* maximum packet length, excluding CRC */
  101. #define HDLCDRV_MAXFLEN 400
  102. struct hdlcdrv_hdlcbuffer {
  103. spinlock_t lock;
  104. unsigned rd, wr;
  105. unsigned short buf[HDLCDRV_HDLCBUFFER];
  106. };
  107. #ifdef HDLCDRV_DEBUG
  108. struct hdlcdrv_bitbuffer {
  109. unsigned int rd;
  110. unsigned int wr;
  111. unsigned int shreg;
  112. unsigned char buffer[HDLCDRV_BITBUFFER];
  113. };
  114. static inline void hdlcdrv_add_bitbuffer(struct hdlcdrv_bitbuffer *buf,
  115. unsigned int bit)
  116. {
  117. unsigned char new;
  118. new = buf->shreg & 1;
  119. buf->shreg >>= 1;
  120. buf->shreg |= (!!bit) << 7;
  121. if (new) {
  122. buf->buffer[buf->wr] = buf->shreg;
  123. buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  124. buf->shreg = 0x80;
  125. }
  126. }
  127. static inline void hdlcdrv_add_bitbuffer_word(struct hdlcdrv_bitbuffer *buf,
  128. unsigned int bits)
  129. {
  130. buf->buffer[buf->wr] = bits & 0xff;
  131. buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  132. buf->buffer[buf->wr] = (bits >> 8) & 0xff;
  133. buf->wr = (buf->wr+1) % sizeof(buf->buffer);
  134. }
  135. #endif /* HDLCDRV_DEBUG */
  136. /* -------------------------------------------------------------------- */
  137. /*
  138. * Information that need to be kept for each driver.
  139. */
  140. struct hdlcdrv_ops {
  141. /*
  142. * first some informations needed by the hdlcdrv routines
  143. */
  144. const char *drvname;
  145. const char *drvinfo;
  146. /*
  147. * the routines called by the hdlcdrv routines
  148. */
  149. int (*open)(struct net_device *);
  150. int (*close)(struct net_device *);
  151. int (*ioctl)(struct net_device *, struct ifreq *,
  152. struct hdlcdrv_ioctl *, int);
  153. };
  154. struct hdlcdrv_state {
  155. int magic;
  156. int opened;
  157. const struct hdlcdrv_ops *ops;
  158. struct {
  159. int bitrate;
  160. } par;
  161. struct hdlcdrv_pttoutput {
  162. int dma2;
  163. int seriobase;
  164. int pariobase;
  165. int midiiobase;
  166. unsigned int flags;
  167. } ptt_out;
  168. struct hdlcdrv_channel_params ch_params;
  169. struct hdlcdrv_hdlcrx {
  170. struct hdlcdrv_hdlcbuffer hbuf;
  171. long in_hdlc_rx;
  172. /* 0 = sync hunt, != 0 receiving */
  173. int rx_state;
  174. unsigned int bitstream;
  175. unsigned int bitbuf;
  176. int numbits;
  177. unsigned char dcd;
  178. int len;
  179. unsigned char *bp;
  180. unsigned char buffer[HDLCDRV_MAXFLEN+2];
  181. } hdlcrx;
  182. struct hdlcdrv_hdlctx {
  183. struct hdlcdrv_hdlcbuffer hbuf;
  184. long in_hdlc_tx;
  185. /*
  186. * 0 = send flags
  187. * 1 = send txtail (flags)
  188. * 2 = send packet
  189. */
  190. int tx_state;
  191. int numflags;
  192. unsigned int bitstream;
  193. unsigned char ptt;
  194. int calibrate;
  195. int slotcnt;
  196. unsigned int bitbuf;
  197. int numbits;
  198. int len;
  199. unsigned char *bp;
  200. unsigned char buffer[HDLCDRV_MAXFLEN+2];
  201. } hdlctx;
  202. #ifdef HDLCDRV_DEBUG
  203. struct hdlcdrv_bitbuffer bitbuf_channel;
  204. struct hdlcdrv_bitbuffer bitbuf_hdlc;
  205. #endif /* HDLCDRV_DEBUG */
  206. struct net_device_stats stats;
  207. int ptt_keyed;
  208. /* queued skb for transmission */
  209. struct sk_buff *skb;
  210. };
  211. /* -------------------------------------------------------------------- */
  212. static inline int hdlcdrv_hbuf_full(struct hdlcdrv_hdlcbuffer *hb)
  213. {
  214. unsigned long flags;
  215. int ret;
  216. spin_lock_irqsave(&hb->lock, flags);
  217. ret = !((HDLCDRV_HDLCBUFFER - 1 + hb->rd - hb->wr) % HDLCDRV_HDLCBUFFER);
  218. spin_unlock_irqrestore(&hb->lock, flags);
  219. return ret;
  220. }
  221. /* -------------------------------------------------------------------- */
  222. static inline int hdlcdrv_hbuf_empty(struct hdlcdrv_hdlcbuffer *hb)
  223. {
  224. unsigned long flags;
  225. int ret;
  226. spin_lock_irqsave(&hb->lock, flags);
  227. ret = (hb->rd == hb->wr);
  228. spin_unlock_irqrestore(&hb->lock, flags);
  229. return ret;
  230. }
  231. /* -------------------------------------------------------------------- */
  232. static inline unsigned short hdlcdrv_hbuf_get(struct hdlcdrv_hdlcbuffer *hb)
  233. {
  234. unsigned long flags;
  235. unsigned short val;
  236. unsigned newr;
  237. spin_lock_irqsave(&hb->lock, flags);
  238. if (hb->rd == hb->wr)
  239. val = 0;
  240. else {
  241. newr = (hb->rd+1) % HDLCDRV_HDLCBUFFER;
  242. val = hb->buf[hb->rd];
  243. hb->rd = newr;
  244. }
  245. spin_unlock_irqrestore(&hb->lock, flags);
  246. return val;
  247. }
  248. /* -------------------------------------------------------------------- */
  249. static inline void hdlcdrv_hbuf_put(struct hdlcdrv_hdlcbuffer *hb,
  250. unsigned short val)
  251. {
  252. unsigned newp;
  253. unsigned long flags;
  254. spin_lock_irqsave(&hb->lock, flags);
  255. newp = (hb->wr+1) % HDLCDRV_HDLCBUFFER;
  256. if (newp != hb->rd) {
  257. hb->buf[hb->wr] = val & 0xffff;
  258. hb->wr = newp;
  259. }
  260. spin_unlock_irqrestore(&hb->lock, flags);
  261. }
  262. /* -------------------------------------------------------------------- */
  263. static inline void hdlcdrv_putbits(struct hdlcdrv_state *s, unsigned int bits)
  264. {
  265. hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, bits);
  266. }
  267. static inline unsigned int hdlcdrv_getbits(struct hdlcdrv_state *s)
  268. {
  269. unsigned int ret;
  270. if (hdlcdrv_hbuf_empty(&s->hdlctx.hbuf)) {
  271. if (s->hdlctx.calibrate > 0)
  272. s->hdlctx.calibrate--;
  273. else
  274. s->hdlctx.ptt = 0;
  275. ret = 0;
  276. } else
  277. ret = hdlcdrv_hbuf_get(&s->hdlctx.hbuf);
  278. #ifdef HDLCDRV_LOOPBACK
  279. hdlcdrv_hbuf_put(&s->hdlcrx.hbuf, ret);
  280. #endif /* HDLCDRV_LOOPBACK */
  281. return ret;
  282. }
  283. static inline void hdlcdrv_channelbit(struct hdlcdrv_state *s, unsigned int bit)
  284. {
  285. #ifdef HDLCDRV_DEBUG
  286. hdlcdrv_add_bitbuffer(&s->bitbuf_channel, bit);
  287. #endif /* HDLCDRV_DEBUG */
  288. }
  289. static inline void hdlcdrv_setdcd(struct hdlcdrv_state *s, int dcd)
  290. {
  291. s->hdlcrx.dcd = !!dcd;
  292. }
  293. static inline int hdlcdrv_ptt(struct hdlcdrv_state *s)
  294. {
  295. return s->hdlctx.ptt || (s->hdlctx.calibrate > 0);
  296. }
  297. /* -------------------------------------------------------------------- */
  298. void hdlcdrv_receiver(struct net_device *, struct hdlcdrv_state *);
  299. void hdlcdrv_transmitter(struct net_device *, struct hdlcdrv_state *);
  300. void hdlcdrv_arbitrate(struct net_device *, struct hdlcdrv_state *);
  301. struct net_device *hdlcdrv_register(const struct hdlcdrv_ops *ops,
  302. unsigned int privsize, const char *ifname,
  303. unsigned int baseaddr, unsigned int irq,
  304. unsigned int dma);
  305. void hdlcdrv_unregister(struct net_device *dev);
  306. /* -------------------------------------------------------------------- */
  307. #endif /* __KERNEL__ */
  308. /* -------------------------------------------------------------------- */
  309. #endif /* _HDLCDRV_H */
  310. /* -------------------------------------------------------------------- */