1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "gigaset.h"
17#include <linux/crc-ccitt.h>
18#include <linux/bitrev.h>
19#include <linux/export.h>
20
21
22
23
24
25
26static inline int muststuff(unsigned char c)
27{
28 if (c < PPP_TRANS) return 1;
29 if (c == PPP_FLAG) return 1;
30 if (c == PPP_ESCAPE) return 1;
31
32
33
34 return 0;
35}
36
37
38
39
40
41
42
43
44
45
46
47
48
49static unsigned cmd_loop(unsigned numbytes, struct inbuf_t *inbuf)
50{
51 unsigned char *src = inbuf->data + inbuf->head;
52 struct cardstate *cs = inbuf->cs;
53 unsigned cbytes = cs->cbytes;
54 unsigned procbytes = 0;
55 unsigned char c;
56
57 while (procbytes < numbytes) {
58 c = *src++;
59 procbytes++;
60
61 switch (c) {
62 case '\n':
63 if (cbytes == 0 && cs->respdata[0] == '\r') {
64
65 cs->respdata[0] = 0;
66 break;
67 }
68
69 case '\r':
70
71 if (cbytes >= MAX_RESP_SIZE) {
72 dev_warn(cs->dev, "response too large (%d)\n",
73 cbytes);
74 cbytes = MAX_RESP_SIZE;
75 }
76 cs->cbytes = cbytes;
77 gigaset_dbg_buffer(DEBUG_TRANSCMD, "received response",
78 cbytes, cs->respdata);
79 gigaset_handle_modem_response(cs);
80 cbytes = 0;
81
82
83 cs->respdata[0] = c;
84
85
86 if (cs->dle && !(inbuf->inputstate & INS_DLE_command))
87 inbuf->inputstate &= ~INS_command;
88
89
90 goto exit;
91
92 case DLE_FLAG:
93 if (inbuf->inputstate & INS_DLE_char) {
94
95 inbuf->inputstate &= ~INS_DLE_char;
96 } else if (cs->dle ||
97 (inbuf->inputstate & INS_DLE_command)) {
98
99 inbuf->inputstate |= INS_DLE_char;
100 goto exit;
101 }
102
103
104 default:
105
106 if (cbytes < MAX_RESP_SIZE)
107 cs->respdata[cbytes] = c;
108 cbytes++;
109 }
110 }
111exit:
112 cs->cbytes = cbytes;
113 return procbytes;
114}
115
116
117
118
119
120
121static unsigned lock_loop(unsigned numbytes, struct inbuf_t *inbuf)
122{
123 unsigned char *src = inbuf->data + inbuf->head;
124
125 gigaset_dbg_buffer(DEBUG_LOCKCMD, "received response", numbytes, src);
126 gigaset_if_receive(inbuf->cs, src, numbytes);
127 return numbytes;
128}
129
130
131
132
133
134
135
136
137
138static unsigned hdlc_loop(unsigned numbytes, struct inbuf_t *inbuf)
139{
140 struct cardstate *cs = inbuf->cs;
141 struct bc_state *bcs = cs->bcs;
142 int inputstate = bcs->inputstate;
143 __u16 fcs = bcs->rx_fcs;
144 struct sk_buff *skb = bcs->rx_skb;
145 unsigned char *src = inbuf->data + inbuf->head;
146 unsigned procbytes = 0;
147 unsigned char c;
148
149 if (inputstate & INS_byte_stuff) {
150 if (!numbytes)
151 return 0;
152 inputstate &= ~INS_byte_stuff;
153 goto byte_stuff;
154 }
155
156 while (procbytes < numbytes) {
157 c = *src++;
158 procbytes++;
159 if (c == DLE_FLAG) {
160 if (inputstate & INS_DLE_char) {
161
162 inputstate &= ~INS_DLE_char;
163 } else if (cs->dle || (inputstate & INS_DLE_command)) {
164
165 inputstate |= INS_DLE_char;
166 break;
167 }
168 }
169
170 if (c == PPP_ESCAPE) {
171
172 if (procbytes >= numbytes) {
173
174 inputstate |= INS_byte_stuff;
175 break;
176 }
177byte_stuff:
178 c = *src++;
179 procbytes++;
180 if (c == DLE_FLAG) {
181 if (inputstate & INS_DLE_char) {
182
183 inputstate &= ~INS_DLE_char;
184 } else if (cs->dle ||
185 (inputstate & INS_DLE_command)) {
186
187 inputstate |=
188 INS_DLE_char | INS_byte_stuff;
189 break;
190 }
191 }
192 c ^= PPP_TRANS;
193#ifdef CONFIG_GIGASET_DEBUG
194 if (!muststuff(c))
195 gig_dbg(DEBUG_HDLC, "byte stuffed: 0x%02x", c);
196#endif
197 } else if (c == PPP_FLAG) {
198
199 if (inputstate & INS_have_data) {
200 gig_dbg(DEBUG_HDLC,
201 "7e----------------------------");
202
203
204 if (!skb) {
205
206 gigaset_isdn_rcv_err(bcs);
207 } else if (skb->len < 2) {
208
209 dev_warn(cs->dev,
210 "short frame (%d)\n",
211 skb->len);
212 gigaset_isdn_rcv_err(bcs);
213 dev_kfree_skb_any(skb);
214 } else if (fcs != PPP_GOODFCS) {
215
216 dev_err(cs->dev,
217 "Checksum failed, %u bytes corrupted!\n",
218 skb->len);
219 gigaset_isdn_rcv_err(bcs);
220 dev_kfree_skb_any(skb);
221 } else {
222
223 __skb_trim(skb, skb->len - 2);
224 gigaset_skb_rcvd(bcs, skb);
225 }
226
227
228 inputstate &= ~INS_have_data;
229 skb = gigaset_new_rx_skb(bcs);
230 } else {
231
232#ifdef CONFIG_GIGASET_DEBUG
233 ++bcs->emptycount;
234#endif
235 if (!skb) {
236
237 gigaset_isdn_rcv_err(bcs);
238 skb = gigaset_new_rx_skb(bcs);
239 }
240 }
241
242 fcs = PPP_INITFCS;
243 continue;
244#ifdef CONFIG_GIGASET_DEBUG
245 } else if (muststuff(c)) {
246
247 gig_dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
248#endif
249 }
250
251
252#ifdef CONFIG_GIGASET_DEBUG
253 if (!(inputstate & INS_have_data)) {
254 gig_dbg(DEBUG_HDLC, "7e (%d x) ================",
255 bcs->emptycount);
256 bcs->emptycount = 0;
257 }
258#endif
259 inputstate |= INS_have_data;
260 if (skb) {
261 if (skb->len >= bcs->rx_bufsize) {
262 dev_warn(cs->dev, "received packet too long\n");
263 dev_kfree_skb_any(skb);
264
265 bcs->rx_skb = skb = NULL;
266 } else {
267 *__skb_put(skb, 1) = c;
268 fcs = crc_ccitt_byte(fcs, c);
269 }
270 }
271 }
272
273 bcs->inputstate = inputstate;
274 bcs->rx_fcs = fcs;
275 return procbytes;
276}
277
278
279
280
281
282
283
284
285static unsigned iraw_loop(unsigned numbytes, struct inbuf_t *inbuf)
286{
287 struct cardstate *cs = inbuf->cs;
288 struct bc_state *bcs = cs->bcs;
289 int inputstate = bcs->inputstate;
290 struct sk_buff *skb = bcs->rx_skb;
291 unsigned char *src = inbuf->data + inbuf->head;
292 unsigned procbytes = 0;
293 unsigned char c;
294
295 if (!skb) {
296
297 gigaset_new_rx_skb(bcs);
298 return numbytes;
299 }
300
301 while (procbytes < numbytes && skb->len < bcs->rx_bufsize) {
302 c = *src++;
303 procbytes++;
304
305 if (c == DLE_FLAG) {
306 if (inputstate & INS_DLE_char) {
307
308 inputstate &= ~INS_DLE_char;
309 } else if (cs->dle || (inputstate & INS_DLE_command)) {
310
311 inputstate |= INS_DLE_char;
312 break;
313 }
314 }
315
316
317 inputstate |= INS_have_data;
318 *__skb_put(skb, 1) = bitrev8(c);
319 }
320
321
322 if (inputstate & INS_have_data) {
323 gigaset_skb_rcvd(bcs, skb);
324 inputstate &= ~INS_have_data;
325 gigaset_new_rx_skb(bcs);
326 }
327
328 bcs->inputstate = inputstate;
329 return procbytes;
330}
331
332
333
334
335
336
337
338static void handle_dle(struct inbuf_t *inbuf)
339{
340 struct cardstate *cs = inbuf->cs;
341
342 if (cs->mstate == MS_LOCKED)
343 return;
344
345 if (!(inbuf->inputstate & INS_DLE_char)) {
346
347 if (inbuf->data[inbuf->head] == DLE_FLAG &&
348 (cs->dle || inbuf->inputstate & INS_DLE_command)) {
349
350 inbuf->head++;
351 if (inbuf->head == inbuf->tail ||
352 inbuf->head == RBUFSIZE) {
353
354 inbuf->inputstate |= INS_DLE_char;
355 return;
356 }
357 } else {
358
359 return;
360 }
361 }
362
363
364 inbuf->inputstate &= ~INS_DLE_char;
365
366 switch (inbuf->data[inbuf->head]) {
367 case 'X':
368 if (inbuf->inputstate & INS_command)
369 dev_notice(cs->dev,
370 "received <DLE>X in command mode\n");
371 inbuf->inputstate |= INS_command | INS_DLE_command;
372 inbuf->head++;
373 break;
374 case '.':
375 if (!(inbuf->inputstate & INS_DLE_command))
376 dev_notice(cs->dev,
377 "received <DLE>. without <DLE>X\n");
378 inbuf->inputstate &= ~INS_DLE_command;
379
380 if (cs->dle)
381 inbuf->inputstate &= ~INS_command;
382 inbuf->head++;
383 break;
384 case DLE_FLAG:
385
386 inbuf->inputstate |= INS_DLE_char;
387 if (!(cs->dle || inbuf->inputstate & INS_DLE_command))
388 dev_notice(cs->dev,
389 "received <DLE><DLE> not in DLE mode\n");
390 break;
391 default:
392 dev_notice(cs->dev, "received <DLE><%02x>\n",
393 inbuf->data[inbuf->head]);
394
395 }
396}
397
398
399
400
401
402
403
404
405
406
407void gigaset_m10x_input(struct inbuf_t *inbuf)
408{
409 struct cardstate *cs = inbuf->cs;
410 unsigned numbytes, procbytes;
411
412 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", inbuf->head, inbuf->tail);
413
414 while (inbuf->head != inbuf->tail) {
415
416 handle_dle(inbuf);
417
418
419 numbytes = (inbuf->head > inbuf->tail ?
420 RBUFSIZE : inbuf->tail) - inbuf->head;
421 gig_dbg(DEBUG_INTR, "processing %u bytes", numbytes);
422
423
424
425
426
427 if (cs->mstate == MS_LOCKED)
428 procbytes = lock_loop(numbytes, inbuf);
429 else if (inbuf->inputstate & INS_command)
430 procbytes = cmd_loop(numbytes, inbuf);
431 else if (cs->bcs->proto2 == L2_HDLC)
432 procbytes = hdlc_loop(numbytes, inbuf);
433 else
434 procbytes = iraw_loop(numbytes, inbuf);
435 inbuf->head += procbytes;
436
437
438 if (inbuf->head >= RBUFSIZE)
439 inbuf->head = 0;
440
441 gig_dbg(DEBUG_INTR, "head set to %u", inbuf->head);
442 }
443}
444EXPORT_SYMBOL_GPL(gigaset_m10x_input);
445
446
447
448
449
450
451
452
453
454
455
456
457
458static struct sk_buff *HDLC_Encode(struct sk_buff *skb)
459{
460 struct sk_buff *hdlc_skb;
461 __u16 fcs;
462 unsigned char c;
463 unsigned char *cp;
464 int len;
465 unsigned int stuf_cnt;
466
467 stuf_cnt = 0;
468 fcs = PPP_INITFCS;
469 cp = skb->data;
470 len = skb->len;
471 while (len--) {
472 if (muststuff(*cp))
473 stuf_cnt++;
474 fcs = crc_ccitt_byte(fcs, *cp++);
475 }
476 fcs ^= 0xffff;
477
478
479
480
481
482 hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + skb->mac_len);
483 if (!hdlc_skb) {
484 dev_kfree_skb_any(skb);
485 return NULL;
486 }
487
488
489 skb_reset_mac_header(hdlc_skb);
490 skb_reserve(hdlc_skb, skb->mac_len);
491 memcpy(skb_mac_header(hdlc_skb), skb_mac_header(skb), skb->mac_len);
492 hdlc_skb->mac_len = skb->mac_len;
493
494
495 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
496
497
498 while (skb->len--) {
499 if (muststuff(*skb->data)) {
500 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
501 *(skb_put(hdlc_skb, 1)) = (*skb->data++) ^ PPP_TRANS;
502 } else
503 *(skb_put(hdlc_skb, 1)) = *skb->data++;
504 }
505
506
507 c = (fcs & 0x00ff);
508 if (muststuff(c)) {
509 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
510 c ^= PPP_TRANS;
511 }
512 *(skb_put(hdlc_skb, 1)) = c;
513
514 c = ((fcs >> 8) & 0x00ff);
515 if (muststuff(c)) {
516 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
517 c ^= PPP_TRANS;
518 }
519 *(skb_put(hdlc_skb, 1)) = c;
520
521 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
522
523 dev_kfree_skb_any(skb);
524 return hdlc_skb;
525}
526
527
528
529
530
531
532
533
534
535
536static struct sk_buff *iraw_encode(struct sk_buff *skb)
537{
538 struct sk_buff *iraw_skb;
539 unsigned char c;
540 unsigned char *cp;
541 int len;
542
543
544
545
546 iraw_skb = dev_alloc_skb(2 * skb->len + skb->mac_len);
547 if (!iraw_skb) {
548 dev_kfree_skb_any(skb);
549 return NULL;
550 }
551
552
553 skb_reset_mac_header(iraw_skb);
554 skb_reserve(iraw_skb, skb->mac_len);
555 memcpy(skb_mac_header(iraw_skb), skb_mac_header(skb), skb->mac_len);
556 iraw_skb->mac_len = skb->mac_len;
557
558
559 cp = skb->data;
560 len = skb->len;
561 while (len--) {
562 c = bitrev8(*cp++);
563 if (c == DLE_FLAG)
564 *(skb_put(iraw_skb, 1)) = c;
565 *(skb_put(iraw_skb, 1)) = c;
566 }
567 dev_kfree_skb_any(skb);
568 return iraw_skb;
569}
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
586{
587 struct cardstate *cs = bcs->cs;
588 unsigned len = skb->len;
589 unsigned long flags;
590
591 if (bcs->proto2 == L2_HDLC)
592 skb = HDLC_Encode(skb);
593 else
594 skb = iraw_encode(skb);
595 if (!skb) {
596 dev_err(cs->dev,
597 "unable to allocate memory for encoding!\n");
598 return -ENOMEM;
599 }
600
601 skb_queue_tail(&bcs->squeue, skb);
602 spin_lock_irqsave(&cs->lock, flags);
603 if (cs->connected)
604 tasklet_schedule(&cs->write_tasklet);
605 spin_unlock_irqrestore(&cs->lock, flags);
606
607 return len;
608}
609EXPORT_SYMBOL_GPL(gigaset_m10x_send_skb);
610