isocdata.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Common data handling layer for bas_gigaset
  3. *
  4. * Copyright (c) 2005 by Tilman Schmidt <tilman@imap.cc>,
  5. * Hansjoerg Lipp <hjlipp@web.de>.
  6. *
  7. * =====================================================================
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. * =====================================================================
  13. */
  14. #include "gigaset.h"
  15. #include <linux/crc-ccitt.h>
  16. #include <linux/bitrev.h>
  17. /* access methods for isowbuf_t */
  18. /* ============================ */
  19. /* initialize buffer structure
  20. */
  21. void gigaset_isowbuf_init(struct isowbuf_t *iwb, unsigned char idle)
  22. {
  23. iwb->read = 0;
  24. iwb->nextread = 0;
  25. iwb->write = 0;
  26. atomic_set(&iwb->writesem, 1);
  27. iwb->wbits = 0;
  28. iwb->idle = idle;
  29. memset(iwb->data + BAS_OUTBUFSIZE, idle, BAS_OUTBUFPAD);
  30. }
  31. /* compute number of bytes which can be appended to buffer
  32. * so that there is still room to append a maximum frame of flags
  33. */
  34. static inline int isowbuf_freebytes(struct isowbuf_t *iwb)
  35. {
  36. int read, write, freebytes;
  37. read = iwb->read;
  38. write = iwb->write;
  39. freebytes = read - write;
  40. if (freebytes > 0) {
  41. /* no wraparound: need padding space within regular area */
  42. return freebytes - BAS_OUTBUFPAD;
  43. } else if (read < BAS_OUTBUFPAD) {
  44. /* wraparound: can use space up to end of regular area */
  45. return BAS_OUTBUFSIZE - write;
  46. } else {
  47. /* following the wraparound yields more space */
  48. return freebytes + BAS_OUTBUFSIZE - BAS_OUTBUFPAD;
  49. }
  50. }
  51. /* start writing
  52. * acquire the write semaphore
  53. * return 0 if acquired, <0 if busy
  54. */
  55. static inline int isowbuf_startwrite(struct isowbuf_t *iwb)
  56. {
  57. if (!atomic_dec_and_test(&iwb->writesem)) {
  58. atomic_inc(&iwb->writesem);
  59. gig_dbg(DEBUG_ISO, "%s: couldn't acquire iso write semaphore",
  60. __func__);
  61. return -EBUSY;
  62. }
  63. gig_dbg(DEBUG_ISO,
  64. "%s: acquired iso write semaphore, data[write]=%02x, nbits=%d",
  65. __func__, iwb->data[iwb->write], iwb->wbits);
  66. return 0;
  67. }
  68. /* finish writing
  69. * release the write semaphore
  70. * returns the current write position
  71. */
  72. static inline int isowbuf_donewrite(struct isowbuf_t *iwb)
  73. {
  74. int write = iwb->write;
  75. atomic_inc(&iwb->writesem);
  76. return write;
  77. }
  78. /* append bits to buffer without any checks
  79. * - data contains bits to append, starting at LSB
  80. * - nbits is number of bits to append (0..24)
  81. * must be called with the write semaphore held
  82. * If more than nbits bits are set in data, the extraneous bits are set in the
  83. * buffer too, but the write position is only advanced by nbits.
  84. */
  85. static inline void isowbuf_putbits(struct isowbuf_t *iwb, u32 data, int nbits)
  86. {
  87. int write = iwb->write;
  88. data <<= iwb->wbits;
  89. data |= iwb->data[write];
  90. nbits += iwb->wbits;
  91. while (nbits >= 8) {
  92. iwb->data[write++] = data & 0xff;
  93. write %= BAS_OUTBUFSIZE;
  94. data >>= 8;
  95. nbits -= 8;
  96. }
  97. iwb->wbits = nbits;
  98. iwb->data[write] = data & 0xff;
  99. iwb->write = write;
  100. }
  101. /* put final flag on HDLC bitstream
  102. * also sets the idle fill byte to the correspondingly shifted flag pattern
  103. * must be called with the write semaphore held
  104. */
  105. static inline void isowbuf_putflag(struct isowbuf_t *iwb)
  106. {
  107. int write;
  108. /* add two flags, thus reliably covering one byte */
  109. isowbuf_putbits(iwb, 0x7e7e, 8);
  110. /* recover the idle flag byte */
  111. write = iwb->write;
  112. iwb->idle = iwb->data[write];
  113. gig_dbg(DEBUG_ISO, "idle fill byte %02x", iwb->idle);
  114. /* mask extraneous bits in buffer */
  115. iwb->data[write] &= (1 << iwb->wbits) - 1;
  116. }
  117. /* retrieve a block of bytes for sending
  118. * The requested number of bytes is provided as a contiguous block.
  119. * If necessary, the frame is filled to the requested number of bytes
  120. * with the idle value.
  121. * returns offset to frame, < 0 on busy or error
  122. */
  123. int gigaset_isowbuf_getbytes(struct isowbuf_t *iwb, int size)
  124. {
  125. int read, write, limit, src, dst;
  126. unsigned char pbyte;
  127. read = iwb->nextread;
  128. write = iwb->write;
  129. if (likely(read == write)) {
  130. /* return idle frame */
  131. return read < BAS_OUTBUFPAD ?
  132. BAS_OUTBUFSIZE : read - BAS_OUTBUFPAD;
  133. }
  134. limit = read + size;
  135. gig_dbg(DEBUG_STREAM, "%s: read=%d write=%d limit=%d",
  136. __func__, read, write, limit);
  137. #ifdef CONFIG_GIGASET_DEBUG
  138. if (unlikely(size < 0 || size > BAS_OUTBUFPAD)) {
  139. pr_err("invalid size %d\n", size);
  140. return -EINVAL;
  141. }
  142. #endif
  143. if (read < write) {
  144. /* no wraparound in valid data */
  145. if (limit >= write) {
  146. /* append idle frame */
  147. if (isowbuf_startwrite(iwb) < 0)
  148. return -EBUSY;
  149. /* write position could have changed */
  150. write = iwb->write;
  151. if (limit >= write) {
  152. pbyte = iwb->data[write]; /* save
  153. partial byte */
  154. limit = write + BAS_OUTBUFPAD;
  155. gig_dbg(DEBUG_STREAM,
  156. "%s: filling %d->%d with %02x",
  157. __func__, write, limit, iwb->idle);
  158. if (write + BAS_OUTBUFPAD < BAS_OUTBUFSIZE)
  159. memset(iwb->data + write, iwb->idle,
  160. BAS_OUTBUFPAD);
  161. else {
  162. /* wraparound, fill entire pad area */
  163. memset(iwb->data + write, iwb->idle,
  164. BAS_OUTBUFSIZE + BAS_OUTBUFPAD
  165. - write);
  166. limit = 0;
  167. }
  168. gig_dbg(DEBUG_STREAM,
  169. "%s: restoring %02x at %d",
  170. __func__, pbyte, limit);
  171. iwb->data[limit] = pbyte; /* restore
  172. partial byte */
  173. iwb->write = limit;
  174. }
  175. isowbuf_donewrite(iwb);
  176. }
  177. } else {
  178. /* valid data wraparound */
  179. if (limit >= BAS_OUTBUFSIZE) {
  180. /* copy wrapped part into pad area */
  181. src = 0;
  182. dst = BAS_OUTBUFSIZE;
  183. while (dst < limit && src < write)
  184. iwb->data[dst++] = iwb->data[src++];
  185. if (dst <= limit) {
  186. /* fill pad area with idle byte */
  187. memset(iwb->data + dst, iwb->idle,
  188. BAS_OUTBUFSIZE + BAS_OUTBUFPAD - dst);
  189. }
  190. limit = src;
  191. }
  192. }
  193. iwb->nextread = limit;
  194. return read;
  195. }
  196. /* dump_bytes
  197. * write hex bytes to syslog for debugging
  198. */
  199. static inline void dump_bytes(enum debuglevel level, const char *tag,
  200. unsigned char *bytes, int count)
  201. {
  202. #ifdef CONFIG_GIGASET_DEBUG
  203. unsigned char c;
  204. static char dbgline[3 * 32 + 1];
  205. int i = 0;
  206. if (!(gigaset_debuglevel & level))
  207. return;
  208. while (count-- > 0) {
  209. if (i > sizeof(dbgline) - 4) {
  210. dbgline[i] = '\0';
  211. gig_dbg(level, "%s:%s", tag, dbgline);
  212. i = 0;
  213. }
  214. c = *bytes++;
  215. dbgline[i] = (i && !(i % 12)) ? '-' : ' ';
  216. i++;
  217. dbgline[i++] = hex_asc_hi(c);
  218. dbgline[i++] = hex_asc_lo(c);
  219. }
  220. dbgline[i] = '\0';
  221. gig_dbg(level, "%s:%s", tag, dbgline);
  222. #endif
  223. }
  224. /*============================================================================*/
  225. /* bytewise HDLC bitstuffing via table lookup
  226. * lookup table: 5 subtables for 0..4 preceding consecutive '1' bits
  227. * index: 256*(number of preceding '1' bits) + (next byte to stuff)
  228. * value: bit 9.. 0 = result bits
  229. * bit 12..10 = number of trailing '1' bits in result
  230. * bit 14..13 = number of bits added by stuffing
  231. */
  232. static const u16 stufftab[5 * 256] = {
  233. /* previous 1s = 0: */
  234. 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
  235. 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x201f,
  236. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
  237. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x203e, 0x205f,
  238. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
  239. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x209f,
  240. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
  241. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x207c, 0x207d, 0x20be, 0x20df,
  242. 0x0480, 0x0481, 0x0482, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487, 0x0488, 0x0489, 0x048a, 0x048b, 0x048c, 0x048d, 0x048e, 0x048f,
  243. 0x0490, 0x0491, 0x0492, 0x0493, 0x0494, 0x0495, 0x0496, 0x0497, 0x0498, 0x0499, 0x049a, 0x049b, 0x049c, 0x049d, 0x049e, 0x251f,
  244. 0x04a0, 0x04a1, 0x04a2, 0x04a3, 0x04a4, 0x04a5, 0x04a6, 0x04a7, 0x04a8, 0x04a9, 0x04aa, 0x04ab, 0x04ac, 0x04ad, 0x04ae, 0x04af,
  245. 0x04b0, 0x04b1, 0x04b2, 0x04b3, 0x04b4, 0x04b5, 0x04b6, 0x04b7, 0x04b8, 0x04b9, 0x04ba, 0x04bb, 0x04bc, 0x04bd, 0x253e, 0x255f,
  246. 0x08c0, 0x08c1, 0x08c2, 0x08c3, 0x08c4, 0x08c5, 0x08c6, 0x08c7, 0x08c8, 0x08c9, 0x08ca, 0x08cb, 0x08cc, 0x08cd, 0x08ce, 0x08cf,
  247. 0x08d0, 0x08d1, 0x08d2, 0x08d3, 0x08d4, 0x08d5, 0x08d6, 0x08d7, 0x08d8, 0x08d9, 0x08da, 0x08db, 0x08dc, 0x08dd, 0x08de, 0x299f,
  248. 0x0ce0, 0x0ce1, 0x0ce2, 0x0ce3, 0x0ce4, 0x0ce5, 0x0ce6, 0x0ce7, 0x0ce8, 0x0ce9, 0x0cea, 0x0ceb, 0x0cec, 0x0ced, 0x0cee, 0x0cef,
  249. 0x10f0, 0x10f1, 0x10f2, 0x10f3, 0x10f4, 0x10f5, 0x10f6, 0x10f7, 0x20f8, 0x20f9, 0x20fa, 0x20fb, 0x257c, 0x257d, 0x29be, 0x2ddf,
  250. /* previous 1s = 1: */
  251. 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x200f,
  252. 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x202f,
  253. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x204f,
  254. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x203e, 0x206f,
  255. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x208f,
  256. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x20af,
  257. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x20cf,
  258. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x207c, 0x207d, 0x20be, 0x20ef,
  259. 0x0480, 0x0481, 0x0482, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487, 0x0488, 0x0489, 0x048a, 0x048b, 0x048c, 0x048d, 0x048e, 0x250f,
  260. 0x0490, 0x0491, 0x0492, 0x0493, 0x0494, 0x0495, 0x0496, 0x0497, 0x0498, 0x0499, 0x049a, 0x049b, 0x049c, 0x049d, 0x049e, 0x252f,
  261. 0x04a0, 0x04a1, 0x04a2, 0x04a3, 0x04a4, 0x04a5, 0x04a6, 0x04a7, 0x04a8, 0x04a9, 0x04aa, 0x04ab, 0x04ac, 0x04ad, 0x04ae, 0x254f,
  262. 0x04b0, 0x04b1, 0x04b2, 0x04b3, 0x04b4, 0x04b5, 0x04b6, 0x04b7, 0x04b8, 0x04b9, 0x04ba, 0x04bb, 0x04bc, 0x04bd, 0x253e, 0x256f,
  263. 0x08c0, 0x08c1, 0x08c2, 0x08c3, 0x08c4, 0x08c5, 0x08c6, 0x08c7, 0x08c8, 0x08c9, 0x08ca, 0x08cb, 0x08cc, 0x08cd, 0x08ce, 0x298f,
  264. 0x08d0, 0x08d1, 0x08d2, 0x08d3, 0x08d4, 0x08d5, 0x08d6, 0x08d7, 0x08d8, 0x08d9, 0x08da, 0x08db, 0x08dc, 0x08dd, 0x08de, 0x29af,
  265. 0x0ce0, 0x0ce1, 0x0ce2, 0x0ce3, 0x0ce4, 0x0ce5, 0x0ce6, 0x0ce7, 0x0ce8, 0x0ce9, 0x0cea, 0x0ceb, 0x0cec, 0x0ced, 0x0cee, 0x2dcf,
  266. 0x10f0, 0x10f1, 0x10f2, 0x10f3, 0x10f4, 0x10f5, 0x10f6, 0x10f7, 0x20f8, 0x20f9, 0x20fa, 0x20fb, 0x257c, 0x257d, 0x29be, 0x31ef,
  267. /* previous 1s = 2: */
  268. 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x2007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x2017,
  269. 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x2027, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x2037,
  270. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x2047, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x2057,
  271. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x2067, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x203e, 0x2077,
  272. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x2087, 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x2097,
  273. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x20a7, 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x20b7,
  274. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x20c7, 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x20d7,
  275. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x20e7, 0x0078, 0x0079, 0x007a, 0x007b, 0x207c, 0x207d, 0x20be, 0x20f7,
  276. 0x0480, 0x0481, 0x0482, 0x0483, 0x0484, 0x0485, 0x0486, 0x2507, 0x0488, 0x0489, 0x048a, 0x048b, 0x048c, 0x048d, 0x048e, 0x2517,
  277. 0x0490, 0x0491, 0x0492, 0x0493, 0x0494, 0x0495, 0x0496, 0x2527, 0x0498, 0x0499, 0x049a, 0x049b, 0x049c, 0x049d, 0x049e, 0x2537,
  278. 0x04a0, 0x04a1, 0x04a2, 0x04a3, 0x04a4, 0x04a5, 0x04a6, 0x2547, 0x04a8, 0x04a9, 0x04aa, 0x04ab, 0x04ac, 0x04ad, 0x04ae, 0x2557,
  279. 0x04b0, 0x04b1, 0x04b2, 0x04b3, 0x04b4, 0x04b5, 0x04b6, 0x2567, 0x04b8, 0x04b9, 0x04ba, 0x04bb, 0x04bc, 0x04bd, 0x253e, 0x2577,
  280. 0x08c0, 0x08c1, 0x08c2, 0x08c3, 0x08c4, 0x08c5, 0x08c6, 0x2987, 0x08c8, 0x08c9, 0x08ca, 0x08cb, 0x08cc, 0x08cd, 0x08ce, 0x2997,
  281. 0x08d0, 0x08d1, 0x08d2, 0x08d3, 0x08d4, 0x08d5, 0x08d6, 0x29a7, 0x08d8, 0x08d9, 0x08da, 0x08db, 0x08dc, 0x08dd, 0x08de, 0x29b7,
  282. 0x0ce0, 0x0ce1, 0x0ce2, 0x0ce3, 0x0ce4, 0x0ce5, 0x0ce6, 0x2dc7, 0x0ce8, 0x0ce9, 0x0cea, 0x0ceb, 0x0cec, 0x0ced, 0x0cee, 0x2dd7,
  283. 0x10f0, 0x10f1, 0x10f2, 0x10f3, 0x10f4, 0x10f5, 0x10f6, 0x31e7, 0x20f8, 0x20f9, 0x20fa, 0x20fb, 0x257c, 0x257d, 0x29be, 0x41f7,
  284. /* previous 1s = 3: */
  285. 0x0000, 0x0001, 0x0002, 0x2003, 0x0004, 0x0005, 0x0006, 0x200b, 0x0008, 0x0009, 0x000a, 0x2013, 0x000c, 0x000d, 0x000e, 0x201b,
  286. 0x0010, 0x0011, 0x0012, 0x2023, 0x0014, 0x0015, 0x0016, 0x202b, 0x0018, 0x0019, 0x001a, 0x2033, 0x001c, 0x001d, 0x001e, 0x203b,
  287. 0x0020, 0x0021, 0x0022, 0x2043, 0x0024, 0x0025, 0x0026, 0x204b, 0x0028, 0x0029, 0x002a, 0x2053, 0x002c, 0x002d, 0x002e, 0x205b,
  288. 0x0030, 0x0031, 0x0032, 0x2063, 0x0034, 0x0035, 0x0036, 0x206b, 0x0038, 0x0039, 0x003a, 0x2073, 0x003c, 0x003d, 0x203e, 0x207b,
  289. 0x0040, 0x0041, 0x0042, 0x2083, 0x0044, 0x0045, 0x0046, 0x208b, 0x0048, 0x0049, 0x004a, 0x2093, 0x004c, 0x004d, 0x004e, 0x209b,
  290. 0x0050, 0x0051, 0x0052, 0x20a3, 0x0054, 0x0055, 0x0056, 0x20ab, 0x0058, 0x0059, 0x005a, 0x20b3, 0x005c, 0x005d, 0x005e, 0x20bb,
  291. 0x0060, 0x0061, 0x0062, 0x20c3, 0x0064, 0x0065, 0x0066, 0x20cb, 0x0068, 0x0069, 0x006a, 0x20d3, 0x006c, 0x006d, 0x006e, 0x20db,
  292. 0x0070, 0x0071, 0x0072, 0x20e3, 0x0074, 0x0075, 0x0076, 0x20eb, 0x0078, 0x0079, 0x007a, 0x20f3, 0x207c, 0x207d, 0x20be, 0x40fb,
  293. 0x0480, 0x0481, 0x0482, 0x2503, 0x0484, 0x0485, 0x0486, 0x250b, 0x0488, 0x0489, 0x048a, 0x2513, 0x048c, 0x048d, 0x048e, 0x251b,
  294. 0x0490, 0x0491, 0x0492, 0x2523, 0x0494, 0x0495, 0x0496, 0x252b, 0x0498, 0x0499, 0x049a, 0x2533, 0x049c, 0x049d, 0x049e, 0x253b,
  295. 0x04a0, 0x04a1, 0x04a2, 0x2543, 0x04a4, 0x04a5, 0x04a6, 0x254b, 0x04a8, 0x04a9, 0x04aa, 0x2553, 0x04ac, 0x04ad, 0x04ae, 0x255b,
  296. 0x04b0, 0x04b1, 0x04b2, 0x2563, 0x04b4, 0x04b5, 0x04b6, 0x256b, 0x04b8, 0x04b9, 0x04ba, 0x2573, 0x04bc, 0x04bd, 0x253e, 0x257b,
  297. 0x08c0, 0x08c1, 0x08c2, 0x2983, 0x08c4, 0x08c5, 0x08c6, 0x298b, 0x08c8, 0x08c9, 0x08ca, 0x2993, 0x08cc, 0x08cd, 0x08ce, 0x299b,
  298. 0x08d0, 0x08d1, 0x08d2, 0x29a3, 0x08d4, 0x08d5, 0x08d6, 0x29ab, 0x08d8, 0x08d9, 0x08da, 0x29b3, 0x08dc, 0x08dd, 0x08de, 0x29bb,
  299. 0x0ce0, 0x0ce1, 0x0ce2, 0x2dc3, 0x0ce4, 0x0ce5, 0x0ce6, 0x2dcb, 0x0ce8, 0x0ce9, 0x0cea, 0x2dd3, 0x0cec, 0x0ced, 0x0cee, 0x2ddb,
  300. 0x10f0, 0x10f1, 0x10f2, 0x31e3, 0x10f4, 0x10f5, 0x10f6, 0x31eb, 0x20f8, 0x20f9, 0x20fa, 0x41f3, 0x257c, 0x257d, 0x29be, 0x46fb,
  301. /* previous 1s = 4: */
  302. 0x0000, 0x2001, 0x0002, 0x2005, 0x0004, 0x2009, 0x0006, 0x200d, 0x0008, 0x2011, 0x000a, 0x2015, 0x000c, 0x2019, 0x000e, 0x201d,
  303. 0x0010, 0x2021, 0x0012, 0x2025, 0x0014, 0x2029, 0x0016, 0x202d, 0x0018, 0x2031, 0x001a, 0x2035, 0x001c, 0x2039, 0x001e, 0x203d,
  304. 0x0020, 0x2041, 0x0022, 0x2045, 0x0024, 0x2049, 0x0026, 0x204d, 0x0028, 0x2051, 0x002a, 0x2055, 0x002c, 0x2059, 0x002e, 0x205d,
  305. 0x0030, 0x2061, 0x0032, 0x2065, 0x0034, 0x2069, 0x0036, 0x206d, 0x0038, 0x2071, 0x003a, 0x2075, 0x003c, 0x2079, 0x203e, 0x407d,
  306. 0x0040, 0x2081, 0x0042, 0x2085, 0x0044, 0x2089, 0x0046, 0x208d, 0x0048, 0x2091, 0x004a, 0x2095, 0x004c, 0x2099, 0x004e, 0x209d,
  307. 0x0050, 0x20a1, 0x0052, 0x20a5, 0x0054, 0x20a9, 0x0056, 0x20ad, 0x0058, 0x20b1, 0x005a, 0x20b5, 0x005c, 0x20b9, 0x005e, 0x20bd,
  308. 0x0060, 0x20c1, 0x0062, 0x20c5, 0x0064, 0x20c9, 0x0066, 0x20cd, 0x0068, 0x20d1, 0x006a, 0x20d5, 0x006c, 0x20d9, 0x006e, 0x20dd,
  309. 0x0070, 0x20e1, 0x0072, 0x20e5, 0x0074, 0x20e9, 0x0076, 0x20ed, 0x0078, 0x20f1, 0x007a, 0x20f5, 0x207c, 0x40f9, 0x20be, 0x417d,
  310. 0x0480, 0x2501, 0x0482, 0x2505, 0x0484, 0x2509, 0x0486, 0x250d, 0x0488, 0x2511, 0x048a, 0x2515, 0x048c, 0x2519, 0x048e, 0x251d,
  311. 0x0490, 0x2521, 0x0492, 0x2525, 0x0494, 0x2529, 0x0496, 0x252d, 0x0498, 0x2531, 0x049a, 0x2535, 0x049c, 0x2539, 0x049e, 0x253d,
  312. 0x04a0, 0x2541, 0x04a2, 0x2545, 0x04a4, 0x2549, 0x04a6, 0x254d, 0x04a8, 0x2551, 0x04aa, 0x2555, 0x04ac, 0x2559, 0x04ae, 0x255d,
  313. 0x04b0, 0x2561, 0x04b2, 0x2565, 0x04b4, 0x2569, 0x04b6, 0x256d, 0x04b8, 0x2571, 0x04ba, 0x2575, 0x04bc, 0x2579, 0x253e, 0x467d,
  314. 0x08c0, 0x2981, 0x08c2, 0x2985, 0x08c4, 0x2989, 0x08c6, 0x298d, 0x08c8, 0x2991, 0x08ca, 0x2995, 0x08cc, 0x2999, 0x08ce, 0x299d,
  315. 0x08d0, 0x29a1, 0x08d2, 0x29a5, 0x08d4, 0x29a9, 0x08d6, 0x29ad, 0x08d8, 0x29b1, 0x08da, 0x29b5, 0x08dc, 0x29b9, 0x08de, 0x29bd,
  316. 0x0ce0, 0x2dc1, 0x0ce2, 0x2dc5, 0x0ce4, 0x2dc9, 0x0ce6, 0x2dcd, 0x0ce8, 0x2dd1, 0x0cea, 0x2dd5, 0x0cec, 0x2dd9, 0x0cee, 0x2ddd,
  317. 0x10f0, 0x31e1, 0x10f2, 0x31e5, 0x10f4, 0x31e9, 0x10f6, 0x31ed, 0x20f8, 0x41f1, 0x20fa, 0x41f5, 0x257c, 0x46f9, 0x29be, 0x4b7d
  318. };
  319. /* hdlc_bitstuff_byte
  320. * perform HDLC bitstuffing for one input byte (8 bits, LSB first)
  321. * parameters:
  322. * cin input byte
  323. * ones number of trailing '1' bits in result before this step
  324. * iwb pointer to output buffer structure
  325. * (write semaphore must be held)
  326. * return value:
  327. * number of trailing '1' bits in result after this step
  328. */
  329. static inline int hdlc_bitstuff_byte(struct isowbuf_t *iwb, unsigned char cin,
  330. int ones)
  331. {
  332. u16 stuff;
  333. int shiftinc, newones;
  334. /* get stuffing information for input byte
  335. * value: bit 9.. 0 = result bits
  336. * bit 12..10 = number of trailing '1' bits in result
  337. * bit 14..13 = number of bits added by stuffing
  338. */
  339. stuff = stufftab[256 * ones + cin];
  340. shiftinc = (stuff >> 13) & 3;
  341. newones = (stuff >> 10) & 7;
  342. stuff &= 0x3ff;
  343. /* append stuffed byte to output stream */
  344. isowbuf_putbits(iwb, stuff, 8 + shiftinc);
  345. return newones;
  346. }
  347. /* hdlc_buildframe
  348. * Perform HDLC framing with bitstuffing on a byte buffer
  349. * The input buffer is regarded as a sequence of bits, starting with the least
  350. * significant bit of the first byte and ending with the most significant bit
  351. * of the last byte. A 16 bit FCS is appended as defined by RFC 1662.
  352. * Whenever five consecutive '1' bits appear in the resulting bit sequence, a
  353. * '0' bit is inserted after them.
  354. * The resulting bit string and a closing flag pattern (PPP_FLAG, '01111110')
  355. * are appended to the output buffer starting at the given bit position, which
  356. * is assumed to already contain a leading flag.
  357. * The output buffer must have sufficient length; count + count/5 + 6 bytes
  358. * starting at *out are safe and are verified to be present.
  359. * parameters:
  360. * in input buffer
  361. * count number of bytes in input buffer
  362. * iwb pointer to output buffer structure
  363. * (write semaphore must be held)
  364. * return value:
  365. * position of end of packet in output buffer on success,
  366. * -EAGAIN if write semaphore busy or buffer full
  367. */
  368. static inline int hdlc_buildframe(struct isowbuf_t *iwb,
  369. unsigned char *in, int count)
  370. {
  371. int ones;
  372. u16 fcs;
  373. int end;
  374. unsigned char c;
  375. if (isowbuf_freebytes(iwb) < count + count / 5 + 6 ||
  376. isowbuf_startwrite(iwb) < 0) {
  377. gig_dbg(DEBUG_ISO, "%s: %d bytes free -> -EAGAIN",
  378. __func__, isowbuf_freebytes(iwb));
  379. return -EAGAIN;
  380. }
  381. dump_bytes(DEBUG_STREAM_DUMP, "snd data", in, count);
  382. /* bitstuff and checksum input data */
  383. fcs = PPP_INITFCS;
  384. ones = 0;
  385. while (count-- > 0) {
  386. c = *in++;
  387. ones = hdlc_bitstuff_byte(iwb, c, ones);
  388. fcs = crc_ccitt_byte(fcs, c);
  389. }
  390. /* bitstuff and append FCS
  391. * (complemented, least significant byte first) */
  392. fcs ^= 0xffff;
  393. ones = hdlc_bitstuff_byte(iwb, fcs & 0x00ff, ones);
  394. ones = hdlc_bitstuff_byte(iwb, (fcs >> 8) & 0x00ff, ones);
  395. /* put closing flag and repeat byte for flag idle */
  396. isowbuf_putflag(iwb);
  397. end = isowbuf_donewrite(iwb);
  398. return end;
  399. }
  400. /* trans_buildframe
  401. * Append a block of 'transparent' data to the output buffer,
  402. * inverting the bytes.
  403. * The output buffer must have sufficient length; count bytes
  404. * starting at *out are safe and are verified to be present.
  405. * parameters:
  406. * in input buffer
  407. * count number of bytes in input buffer
  408. * iwb pointer to output buffer structure
  409. * (write semaphore must be held)
  410. * return value:
  411. * position of end of packet in output buffer on success,
  412. * -EAGAIN if write semaphore busy or buffer full
  413. */
  414. static inline int trans_buildframe(struct isowbuf_t *iwb,
  415. unsigned char *in, int count)
  416. {
  417. int write;
  418. unsigned char c;
  419. if (unlikely(count <= 0))
  420. return iwb->write;
  421. if (isowbuf_freebytes(iwb) < count ||
  422. isowbuf_startwrite(iwb) < 0) {
  423. gig_dbg(DEBUG_ISO, "can't put %d bytes", count);
  424. return -EAGAIN;
  425. }
  426. gig_dbg(DEBUG_STREAM, "put %d bytes", count);
  427. dump_bytes(DEBUG_STREAM_DUMP, "snd data", in, count);
  428. write = iwb->write;
  429. do {
  430. c = bitrev8(*in++);
  431. iwb->data[write++] = c;
  432. write %= BAS_OUTBUFSIZE;
  433. } while (--count > 0);
  434. iwb->write = write;
  435. iwb->idle = c;
  436. return isowbuf_donewrite(iwb);
  437. }
  438. int gigaset_isoc_buildframe(struct bc_state *bcs, unsigned char *in, int len)
  439. {
  440. int result;
  441. switch (bcs->proto2) {
  442. case L2_HDLC:
  443. result = hdlc_buildframe(bcs->hw.bas->isooutbuf, in, len);
  444. gig_dbg(DEBUG_ISO, "%s: %d bytes HDLC -> %d",
  445. __func__, len, result);
  446. break;
  447. default: /* assume transparent */
  448. result = trans_buildframe(bcs->hw.bas->isooutbuf, in, len);
  449. gig_dbg(DEBUG_ISO, "%s: %d bytes trans -> %d",
  450. __func__, len, result);
  451. }
  452. return result;
  453. }
  454. /* hdlc_putbyte
  455. * append byte c to current skb of B channel structure *bcs, updating fcs
  456. */
  457. static inline void hdlc_putbyte(unsigned char c, struct bc_state *bcs)
  458. {
  459. bcs->rx_fcs = crc_ccitt_byte(bcs->rx_fcs, c);
  460. if (bcs->rx_skb == NULL)
  461. /* skipping */
  462. return;
  463. if (bcs->rx_skb->len >= bcs->rx_bufsize) {
  464. dev_warn(bcs->cs->dev, "received oversized packet discarded\n");
  465. bcs->hw.bas->giants++;
  466. dev_kfree_skb_any(bcs->rx_skb);
  467. bcs->rx_skb = NULL;
  468. return;
  469. }
  470. *__skb_put(bcs->rx_skb, 1) = c;
  471. }
  472. /* hdlc_flush
  473. * drop partial HDLC data packet
  474. */
  475. static inline void hdlc_flush(struct bc_state *bcs)
  476. {
  477. /* clear skb or allocate new if not skipping */
  478. if (bcs->rx_skb != NULL)
  479. skb_trim(bcs->rx_skb, 0);
  480. else
  481. gigaset_new_rx_skb(bcs);
  482. /* reset packet state */
  483. bcs->rx_fcs = PPP_INITFCS;
  484. }
  485. /* hdlc_done
  486. * process completed HDLC data packet
  487. */
  488. static inline void hdlc_done(struct bc_state *bcs)
  489. {
  490. struct cardstate *cs = bcs->cs;
  491. struct sk_buff *procskb;
  492. unsigned int len;
  493. if (unlikely(bcs->ignore)) {
  494. bcs->ignore--;
  495. hdlc_flush(bcs);
  496. return;
  497. }
  498. procskb = bcs->rx_skb;
  499. if (procskb == NULL) {
  500. /* previous error */
  501. gig_dbg(DEBUG_ISO, "%s: skb=NULL", __func__);
  502. gigaset_isdn_rcv_err(bcs);
  503. } else if (procskb->len < 2) {
  504. dev_notice(cs->dev, "received short frame (%d octets)\n",
  505. procskb->len);
  506. bcs->hw.bas->runts++;
  507. dev_kfree_skb_any(procskb);
  508. gigaset_isdn_rcv_err(bcs);
  509. } else if (bcs->rx_fcs != PPP_GOODFCS) {
  510. dev_notice(cs->dev, "frame check error\n");
  511. bcs->hw.bas->fcserrs++;
  512. dev_kfree_skb_any(procskb);
  513. gigaset_isdn_rcv_err(bcs);
  514. } else {
  515. len = procskb->len;
  516. __skb_trim(procskb, len -= 2); /* subtract FCS */
  517. gig_dbg(DEBUG_ISO, "%s: good frame (%d octets)", __func__, len);
  518. dump_bytes(DEBUG_STREAM_DUMP,
  519. "rcv data", procskb->data, len);
  520. bcs->hw.bas->goodbytes += len;
  521. gigaset_skb_rcvd(bcs, procskb);
  522. }
  523. gigaset_new_rx_skb(bcs);
  524. bcs->rx_fcs = PPP_INITFCS;
  525. }
  526. /* hdlc_frag
  527. * drop HDLC data packet with non-integral last byte
  528. */
  529. static inline void hdlc_frag(struct bc_state *bcs, unsigned inbits)
  530. {
  531. if (unlikely(bcs->ignore)) {
  532. bcs->ignore--;
  533. hdlc_flush(bcs);
  534. return;
  535. }
  536. dev_notice(bcs->cs->dev, "received partial byte (%d bits)\n", inbits);
  537. bcs->hw.bas->alignerrs++;
  538. gigaset_isdn_rcv_err(bcs);
  539. __skb_trim(bcs->rx_skb, 0);
  540. bcs->rx_fcs = PPP_INITFCS;
  541. }
  542. /* bit counts lookup table for HDLC bit unstuffing
  543. * index: input byte
  544. * value: bit 0..3 = number of consecutive '1' bits starting from LSB
  545. * bit 4..6 = number of consecutive '1' bits starting from MSB
  546. * (replacing 8 by 7 to make it fit; the algorithm won't care)
  547. * bit 7 set if there are 5 or more "interior" consecutive '1' bits
  548. */
  549. static const unsigned char bitcounts[256] = {
  550. 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04,
  551. 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x05,
  552. 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04,
  553. 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x80, 0x06,
  554. 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04,
  555. 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x05,
  556. 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04,
  557. 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x80, 0x81, 0x80, 0x07,
  558. 0x10, 0x11, 0x10, 0x12, 0x10, 0x11, 0x10, 0x13, 0x10, 0x11, 0x10, 0x12, 0x10, 0x11, 0x10, 0x14,
  559. 0x10, 0x11, 0x10, 0x12, 0x10, 0x11, 0x10, 0x13, 0x10, 0x11, 0x10, 0x12, 0x10, 0x11, 0x10, 0x15,
  560. 0x10, 0x11, 0x10, 0x12, 0x10, 0x11, 0x10, 0x13, 0x10, 0x11, 0x10, 0x12, 0x10, 0x11, 0x10, 0x14,
  561. 0x10, 0x11, 0x10, 0x12, 0x10, 0x11, 0x10, 0x13, 0x10, 0x11, 0x10, 0x12, 0x10, 0x11, 0x90, 0x16,
  562. 0x20, 0x21, 0x20, 0x22, 0x20, 0x21, 0x20, 0x23, 0x20, 0x21, 0x20, 0x22, 0x20, 0x21, 0x20, 0x24,
  563. 0x20, 0x21, 0x20, 0x22, 0x20, 0x21, 0x20, 0x23, 0x20, 0x21, 0x20, 0x22, 0x20, 0x21, 0x20, 0x25,
  564. 0x30, 0x31, 0x30, 0x32, 0x30, 0x31, 0x30, 0x33, 0x30, 0x31, 0x30, 0x32, 0x30, 0x31, 0x30, 0x34,
  565. 0x40, 0x41, 0x40, 0x42, 0x40, 0x41, 0x40, 0x43, 0x50, 0x51, 0x50, 0x52, 0x60, 0x61, 0x70, 0x78
  566. };
  567. /* hdlc_unpack
  568. * perform HDLC frame processing (bit unstuffing, flag detection, FCS
  569. * calculation) on a sequence of received data bytes (8 bits each, LSB first)
  570. * pass on successfully received, complete frames as SKBs via gigaset_skb_rcvd
  571. * notify of errors via gigaset_isdn_rcv_err
  572. * tally frames, errors etc. in BC structure counters
  573. * parameters:
  574. * src received data
  575. * count number of received bytes
  576. * bcs receiving B channel structure
  577. */
  578. static inline void hdlc_unpack(unsigned char *src, unsigned count,
  579. struct bc_state *bcs)
  580. {
  581. struct bas_bc_state *ubc = bcs->hw.bas;
  582. int inputstate;
  583. unsigned seqlen, inbyte, inbits;
  584. /* load previous state:
  585. * inputstate = set of flag bits:
  586. * - INS_flag_hunt: no complete opening flag received since connection
  587. * setup or last abort
  588. * - INS_have_data: at least one complete data byte received since last
  589. * flag
  590. * seqlen = number of consecutive '1' bits in last 7 input stream bits
  591. * (0..7)
  592. * inbyte = accumulated partial data byte (if !INS_flag_hunt)
  593. * inbits = number of valid bits in inbyte, starting at LSB (0..6)
  594. */
  595. inputstate = bcs->inputstate;
  596. seqlen = ubc->seqlen;
  597. inbyte = ubc->inbyte;
  598. inbits = ubc->inbits;
  599. /* bit unstuffing a byte a time
  600. * Take your time to understand this; it's straightforward but tedious.
  601. * The "bitcounts" lookup table is used to speed up the counting of
  602. * leading and trailing '1' bits.
  603. */
  604. while (count--) {
  605. unsigned char c = *src++;
  606. unsigned char tabentry = bitcounts[c];
  607. unsigned lead1 = tabentry & 0x0f;
  608. unsigned trail1 = (tabentry >> 4) & 0x0f;
  609. seqlen += lead1;
  610. if (unlikely(inputstate & INS_flag_hunt)) {
  611. if (c == PPP_FLAG) {
  612. /* flag-in-one */
  613. inputstate &= ~(INS_flag_hunt | INS_have_data);
  614. inbyte = 0;
  615. inbits = 0;
  616. } else if (seqlen == 6 && trail1 != 7) {
  617. /* flag completed & not followed by abort */
  618. inputstate &= ~(INS_flag_hunt | INS_have_data);
  619. inbyte = c >> (lead1 + 1);
  620. inbits = 7 - lead1;
  621. if (trail1 >= 8) {
  622. /* interior stuffing:
  623. * omitting the MSB handles most cases,
  624. * correct the incorrectly handled
  625. * cases individually */
  626. inbits--;
  627. switch (c) {
  628. case 0xbe:
  629. inbyte = 0x3f;
  630. break;
  631. }
  632. }
  633. }
  634. /* else: continue flag-hunting */
  635. } else if (likely(seqlen < 5 && trail1 < 7)) {
  636. /* streamlined case: 8 data bits, no stuffing */
  637. inbyte |= c << inbits;
  638. hdlc_putbyte(inbyte & 0xff, bcs);
  639. inputstate |= INS_have_data;
  640. inbyte >>= 8;
  641. /* inbits unchanged */
  642. } else if (likely(seqlen == 6 && inbits == 7 - lead1 &&
  643. trail1 + 1 == inbits &&
  644. !(inputstate & INS_have_data))) {
  645. /* streamlined case: flag idle - state unchanged */
  646. } else if (unlikely(seqlen > 6)) {
  647. /* abort sequence */
  648. ubc->aborts++;
  649. hdlc_flush(bcs);
  650. inputstate |= INS_flag_hunt;
  651. } else if (seqlen == 6) {
  652. /* closing flag, including (6 - lead1) '1's
  653. * and one '0' from inbits */
  654. if (inbits > 7 - lead1) {
  655. hdlc_frag(bcs, inbits + lead1 - 7);
  656. inputstate &= ~INS_have_data;
  657. } else {
  658. if (inbits < 7 - lead1)
  659. ubc->stolen0s++;
  660. if (inputstate & INS_have_data) {
  661. hdlc_done(bcs);
  662. inputstate &= ~INS_have_data;
  663. }
  664. }
  665. if (c == PPP_FLAG) {
  666. /* complete flag, LSB overlaps preceding flag */
  667. ubc->shared0s++;
  668. inbits = 0;
  669. inbyte = 0;
  670. } else if (trail1 != 7) {
  671. /* remaining bits */
  672. inbyte = c >> (lead1 + 1);
  673. inbits = 7 - lead1;
  674. if (trail1 >= 8) {
  675. /* interior stuffing:
  676. * omitting the MSB handles most cases,
  677. * correct the incorrectly handled
  678. * cases individually */
  679. inbits--;
  680. switch (c) {
  681. case 0xbe:
  682. inbyte = 0x3f;
  683. break;
  684. }
  685. }
  686. } else {
  687. /* abort sequence follows,
  688. * skb already empty anyway */
  689. ubc->aborts++;
  690. inputstate |= INS_flag_hunt;
  691. }
  692. } else { /* (seqlen < 6) && (seqlen == 5 || trail1 >= 7) */
  693. if (c == PPP_FLAG) {
  694. /* complete flag */
  695. if (seqlen == 5)
  696. ubc->stolen0s++;
  697. if (inbits) {
  698. hdlc_frag(bcs, inbits);
  699. inbits = 0;
  700. inbyte = 0;
  701. } else if (inputstate & INS_have_data)
  702. hdlc_done(bcs);
  703. inputstate &= ~INS_have_data;
  704. } else if (trail1 == 7) {
  705. /* abort sequence */
  706. ubc->aborts++;
  707. hdlc_flush(bcs);
  708. inputstate |= INS_flag_hunt;
  709. } else {
  710. /* stuffed data */
  711. if (trail1 < 7) { /* => seqlen == 5 */
  712. /* stuff bit at position lead1,
  713. * no interior stuffing */
  714. unsigned char mask = (1 << lead1) - 1;
  715. c = (c & mask) | ((c & ~mask) >> 1);
  716. inbyte |= c << inbits;
  717. inbits += 7;
  718. } else if (seqlen < 5) { /* trail1 >= 8 */
  719. /* interior stuffing:
  720. * omitting the MSB handles most cases,
  721. * correct the incorrectly handled
  722. * cases individually */
  723. switch (c) {
  724. case 0xbe:
  725. c = 0x7e;
  726. break;
  727. }
  728. inbyte |= c << inbits;
  729. inbits += 7;
  730. } else { /* seqlen == 5 && trail1 >= 8 */
  731. /* stuff bit at lead1 *and* interior
  732. * stuffing -- unstuff individually */
  733. switch (c) {
  734. case 0x7d:
  735. c = 0x3f;
  736. break;
  737. case 0xbe:
  738. c = 0x3f;
  739. break;
  740. case 0x3e:
  741. c = 0x1f;
  742. break;
  743. case 0x7c:
  744. c = 0x3e;
  745. break;
  746. }
  747. inbyte |= c << inbits;
  748. inbits += 6;
  749. }
  750. if (inbits >= 8) {
  751. inbits -= 8;
  752. hdlc_putbyte(inbyte & 0xff, bcs);
  753. inputstate |= INS_have_data;
  754. inbyte >>= 8;
  755. }
  756. }
  757. }
  758. seqlen = trail1 & 7;
  759. }
  760. /* save new state */
  761. bcs->inputstate = inputstate;
  762. ubc->seqlen = seqlen;
  763. ubc->inbyte = inbyte;
  764. ubc->inbits = inbits;
  765. }
  766. /* trans_receive
  767. * pass on received USB frame transparently as SKB via gigaset_skb_rcvd
  768. * invert bytes
  769. * tally frames, errors etc. in BC structure counters
  770. * parameters:
  771. * src received data
  772. * count number of received bytes
  773. * bcs receiving B channel structure
  774. */
  775. static inline void trans_receive(unsigned char *src, unsigned count,
  776. struct bc_state *bcs)
  777. {
  778. struct sk_buff *skb;
  779. int dobytes;
  780. unsigned char *dst;
  781. if (unlikely(bcs->ignore)) {
  782. bcs->ignore--;
  783. return;
  784. }
  785. skb = bcs->rx_skb;
  786. if (skb == NULL) {
  787. skb = gigaset_new_rx_skb(bcs);
  788. if (skb == NULL)
  789. return;
  790. }
  791. dobytes = bcs->rx_bufsize - skb->len;
  792. while (count > 0) {
  793. dst = skb_put(skb, count < dobytes ? count : dobytes);
  794. while (count > 0 && dobytes > 0) {
  795. *dst++ = bitrev8(*src++);
  796. count--;
  797. dobytes--;
  798. }
  799. if (dobytes == 0) {
  800. dump_bytes(DEBUG_STREAM_DUMP,
  801. "rcv data", skb->data, skb->len);
  802. bcs->hw.bas->goodbytes += skb->len;
  803. gigaset_skb_rcvd(bcs, skb);
  804. skb = gigaset_new_rx_skb(bcs);
  805. if (skb == NULL)
  806. return;
  807. dobytes = bcs->rx_bufsize;
  808. }
  809. }
  810. }
  811. void gigaset_isoc_receive(unsigned char *src, unsigned count,
  812. struct bc_state *bcs)
  813. {
  814. switch (bcs->proto2) {
  815. case L2_HDLC:
  816. hdlc_unpack(src, count, bcs);
  817. break;
  818. default: /* assume transparent */
  819. trans_receive(src, count, bcs);
  820. }
  821. }
  822. /* == data input =========================================================== */
  823. /* process a block of received bytes in command mode (mstate != MS_LOCKED)
  824. * Append received bytes to the command response buffer and forward them
  825. * line by line to the response handler.
  826. * Note: Received lines may be terminated by CR, LF, or CR LF, which will be
  827. * removed before passing the line to the response handler.
  828. */
  829. static void cmd_loop(unsigned char *src, int numbytes, struct inbuf_t *inbuf)
  830. {
  831. struct cardstate *cs = inbuf->cs;
  832. unsigned cbytes = cs->cbytes;
  833. unsigned char c;
  834. while (numbytes--) {
  835. c = *src++;
  836. switch (c) {
  837. case '\n':
  838. if (cbytes == 0 && cs->respdata[0] == '\r') {
  839. /* collapse LF with preceding CR */
  840. cs->respdata[0] = 0;
  841. break;
  842. }
  843. /* --v-- fall through --v-- */
  844. case '\r':
  845. /* end of message line, pass to response handler */
  846. if (cbytes >= MAX_RESP_SIZE) {
  847. dev_warn(cs->dev, "response too large (%d)\n",
  848. cbytes);
  849. cbytes = MAX_RESP_SIZE;
  850. }
  851. cs->cbytes = cbytes;
  852. gigaset_dbg_buffer(DEBUG_TRANSCMD, "received response",
  853. cbytes, cs->respdata);
  854. gigaset_handle_modem_response(cs);
  855. cbytes = 0;
  856. /* store EOL byte for CRLF collapsing */
  857. cs->respdata[0] = c;
  858. break;
  859. default:
  860. /* append to line buffer if possible */
  861. if (cbytes < MAX_RESP_SIZE)
  862. cs->respdata[cbytes] = c;
  863. cbytes++;
  864. }
  865. }
  866. /* save state */
  867. cs->cbytes = cbytes;
  868. }
  869. /* process a block of data received through the control channel
  870. */
  871. void gigaset_isoc_input(struct inbuf_t *inbuf)
  872. {
  873. struct cardstate *cs = inbuf->cs;
  874. unsigned tail, head, numbytes;
  875. unsigned char *src;
  876. head = inbuf->head;
  877. while (head != (tail = inbuf->tail)) {
  878. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  879. if (head > tail)
  880. tail = RBUFSIZE;
  881. src = inbuf->data + head;
  882. numbytes = tail - head;
  883. gig_dbg(DEBUG_INTR, "processing %u bytes", numbytes);
  884. if (cs->mstate == MS_LOCKED) {
  885. gigaset_dbg_buffer(DEBUG_LOCKCMD, "received response",
  886. numbytes, src);
  887. gigaset_if_receive(inbuf->cs, src, numbytes);
  888. } else {
  889. cmd_loop(src, numbytes, inbuf);
  890. }
  891. head += numbytes;
  892. if (head == RBUFSIZE)
  893. head = 0;
  894. gig_dbg(DEBUG_INTR, "setting head to %u", head);
  895. inbuf->head = head;
  896. }
  897. }
  898. /* == data output ========================================================== */
  899. /**
  900. * gigaset_isoc_send_skb() - queue an skb for sending
  901. * @bcs: B channel descriptor structure.
  902. * @skb: data to send.
  903. *
  904. * Called by LL to queue an skb for sending, and start transmission if
  905. * necessary.
  906. * Once the payload data has been transmitted completely, gigaset_skb_sent()
  907. * will be called with the skb's link layer header preserved.
  908. *
  909. * Return value:
  910. * number of bytes accepted for sending (skb->len) if ok,
  911. * error code < 0 (eg. -ENODEV) on error
  912. */
  913. int gigaset_isoc_send_skb(struct bc_state *bcs, struct sk_buff *skb)
  914. {
  915. int len = skb->len;
  916. unsigned long flags;
  917. spin_lock_irqsave(&bcs->cs->lock, flags);
  918. if (!bcs->cs->connected) {
  919. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  920. return -ENODEV;
  921. }
  922. skb_queue_tail(&bcs->squeue, skb);
  923. gig_dbg(DEBUG_ISO, "%s: skb queued, qlen=%d",
  924. __func__, skb_queue_len(&bcs->squeue));
  925. /* tasklet submits URB if necessary */
  926. tasklet_schedule(&bcs->hw.bas->sent_tasklet);
  927. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  928. return len; /* ok so far */
  929. }