isocdata.c 36 KB

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