aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHansjoerg Lipp <hjlipp@web.de>2006-03-26 04:38:37 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-03-26 11:57:06 -0500
commit07dc1f9f2f80f67823dc9ab4ebe3b1b3b071b911 (patch)
tree53751a6ea45cc3545e45b5690e643965c3c597be
parent76bb4685bff8781b5dbcd7080171d1e1f8c82f5b (diff)
[PATCH] isdn4linux: Siemens Gigaset drivers - M105 USB DECT adapter
And: Tilman Schmidt <tilman@imap.cc> This patch adds the connection-specific module "usb_gigaset", the hardware driver for Gigaset base stations connected via the M105 USB DECT adapter. It contains the code for handling probe/disconnect, AT command/response transmission, and call setup and termination, as well as handling asynchronous data transfers, PPP framing, byte stuffing, and flow control. Signed-off-by: Hansjoerg Lipp <hjlipp@web.de> Signed-off-by: Tilman Schmidt <tilman@imap.cc> Cc: Karsten Keil <kkeil@suse.de> Cc: Greg KH <greg@kroah.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r--drivers/isdn/gigaset/asyncdata.c597
-rw-r--r--drivers/isdn/gigaset/usb-gigaset.c1008
2 files changed, 1605 insertions, 0 deletions
diff --git a/drivers/isdn/gigaset/asyncdata.c b/drivers/isdn/gigaset/asyncdata.c
new file mode 100644
index 000000000000..171f8b703d61
--- /dev/null
+++ b/drivers/isdn/gigaset/asyncdata.c
@@ -0,0 +1,597 @@
1/*
2 * Common data handling layer for ser_gigaset and usb_gigaset
3 *
4 * Copyright (c) 2005 by Tilman Schmidt <tilman@imap.cc>,
5 * Hansjoerg Lipp <hjlipp@web.de>,
6 * Stefan Eilers <Eilers.Stefan@epost.de>.
7 *
8 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
14 * ToDo: ...
15 * =====================================================================
16 * Version: $Id: asyncdata.c,v 1.2.2.7 2005/11/13 23:05:18 hjlipp Exp $
17 * =====================================================================
18 */
19
20#include "gigaset.h"
21#include <linux/crc-ccitt.h>
22
23//#define GIG_M10x_STUFF_VOICE_DATA
24
25/* check if byte must be stuffed/escaped
26 * I'm not sure which data should be encoded.
27 * Therefore I will go the hard way and decode every value
28 * less than 0x20, the flag sequence and the control escape char.
29 */
30static inline int muststuff(unsigned char c)
31{
32 if (c < PPP_TRANS) return 1;
33 if (c == PPP_FLAG) return 1;
34 if (c == PPP_ESCAPE) return 1;
35 /* other possible candidates: */
36 /* 0x91: XON with parity set */
37 /* 0x93: XOFF with parity set */
38 return 0;
39}
40
41/* == data input =========================================================== */
42
43/* process a block of received bytes in command mode (modem response)
44 * Return value:
45 * number of processed bytes
46 */
47static inline int cmd_loop(unsigned char c, unsigned char *src, int numbytes,
48 struct inbuf_t *inbuf)
49{
50 struct cardstate *cs = inbuf->cs;
51 unsigned cbytes = cs->cbytes;
52 int inputstate = inbuf->inputstate;
53 int startbytes = numbytes;
54
55 for (;;) {
56 cs->respdata[cbytes] = c;
57 if (c == 10 || c == 13) {
58 dbg(DEBUG_TRANSCMD, "%s: End of Command (%d Bytes)",
59 __func__, cbytes);
60 cs->cbytes = cbytes;
61 gigaset_handle_modem_response(cs); /* can change cs->dle */
62 cbytes = 0;
63
64 if (cs->dle &&
65 !(inputstate & INS_DLE_command)) {
66 inputstate &= ~INS_command;
67 break;
68 }
69 } else {
70 /* advance in line buffer, checking for overflow */
71 if (cbytes < MAX_RESP_SIZE - 1)
72 cbytes++;
73 else
74 warn("response too large");
75 }
76
77 if (!numbytes)
78 break;
79 c = *src++;
80 --numbytes;
81 if (c == DLE_FLAG &&
82 (cs->dle || inputstate & INS_DLE_command)) {
83 inputstate |= INS_DLE_char;
84 break;
85 }
86 }
87
88 cs->cbytes = cbytes;
89 inbuf->inputstate = inputstate;
90
91 return startbytes - numbytes;
92}
93
94/* process a block of received bytes in lock mode (tty i/f)
95 * Return value:
96 * number of processed bytes
97 */
98static inline int lock_loop(unsigned char *src, int numbytes,
99 struct inbuf_t *inbuf)
100{
101 struct cardstate *cs = inbuf->cs;
102
103 gigaset_dbg_buffer(DEBUG_LOCKCMD, "received response", numbytes, src, 0);
104 gigaset_if_receive(cs, src, numbytes);
105
106 return numbytes;
107}
108
109/* process a block of received bytes in HDLC data mode
110 * Collect HDLC frames, undoing byte stuffing and watching for DLE escapes.
111 * When a frame is complete, check the FCS and pass valid frames to the LL.
112 * If DLE is encountered, return immediately to let the caller handle it.
113 * Return value:
114 * number of processed bytes
115 * numbytes (all bytes processed) on error --FIXME
116 */
117static inline int hdlc_loop(unsigned char c, unsigned char *src, int numbytes,
118 struct inbuf_t *inbuf)
119{
120 struct cardstate *cs = inbuf->cs;
121 struct bc_state *bcs = inbuf->bcs;
122 int inputstate;
123 __u16 fcs;
124 struct sk_buff *skb;
125 unsigned char error;
126 struct sk_buff *compskb;
127 int startbytes = numbytes;
128 int l;
129
130 IFNULLRETVAL(bcs, numbytes);
131 inputstate = bcs->inputstate;
132 fcs = bcs->fcs;
133 skb = bcs->skb;
134 IFNULLRETVAL(skb, numbytes);
135
136 if (unlikely(inputstate & INS_byte_stuff)) {
137 inputstate &= ~INS_byte_stuff;
138 goto byte_stuff;
139 }
140 for (;;) {
141 if (unlikely(c == PPP_ESCAPE)) {
142 if (unlikely(!numbytes)) {
143 inputstate |= INS_byte_stuff;
144 break;
145 }
146 c = *src++;
147 --numbytes;
148 if (unlikely(c == DLE_FLAG &&
149 (cs->dle ||
150 inbuf->inputstate & INS_DLE_command))) {
151 inbuf->inputstate |= INS_DLE_char;
152 inputstate |= INS_byte_stuff;
153 break;
154 }
155byte_stuff:
156 c ^= PPP_TRANS;
157#ifdef CONFIG_GIGASET_DEBUG
158 if (unlikely(!muststuff(c)))
159 dbg(DEBUG_HDLC,
160 "byte stuffed: 0x%02x", c);
161#endif
162 } else if (unlikely(c == PPP_FLAG)) {
163 if (unlikely(inputstate & INS_skip_frame)) {
164 if (!(inputstate & INS_have_data)) { /* 7E 7E */
165 //dbg(DEBUG_HDLC, "(7e)7e------------------------");
166#ifdef CONFIG_GIGASET_DEBUG
167 ++bcs->emptycount;
168#endif
169 } else
170 dbg(DEBUG_HDLC,
171 "7e----------------------------");
172
173 /* end of frame */
174 error = 1;
175 gigaset_rcv_error(NULL, cs, bcs);
176 } else if (!(inputstate & INS_have_data)) { /* 7E 7E */
177 //dbg(DEBUG_HDLC, "(7e)7e------------------------");
178#ifdef CONFIG_GIGASET_DEBUG
179 ++bcs->emptycount;
180#endif
181 break;
182 } else {
183 dbg(DEBUG_HDLC,
184 "7e----------------------------");
185
186 /* end of frame */
187 error = 0;
188
189 if (unlikely(fcs != PPP_GOODFCS)) {
190 err("Packet checksum at %lu failed, "
191 "packet is corrupted (%u bytes)!",
192 bcs->rcvbytes, skb->len);
193 compskb = NULL;
194 gigaset_rcv_error(compskb, cs, bcs);
195 error = 1;
196 } else {
197 if (likely((l = skb->len) > 2)) {
198 skb->tail -= 2;
199 skb->len -= 2;
200 } else {
201 dev_kfree_skb(skb);
202 skb = NULL;
203 inputstate |= INS_skip_frame;
204 if (l == 1) {
205 err("invalid packet size (1)!");
206 error = 1;
207 gigaset_rcv_error(NULL, cs, bcs);
208 }
209 }
210 if (likely(!(error ||
211 (inputstate &
212 INS_skip_frame)))) {
213 gigaset_rcv_skb(skb, cs, bcs);
214 }
215 }
216 }
217
218 if (unlikely(error))
219 if (skb)
220 dev_kfree_skb(skb);
221
222 fcs = PPP_INITFCS;
223 inputstate &= ~(INS_have_data | INS_skip_frame);
224 if (unlikely(bcs->ignore)) {
225 inputstate |= INS_skip_frame;
226 skb = NULL;
227 } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)) {
228 skb_reserve(skb, HW_HDR_LEN);
229 } else {
230 warn("could not allocate new skb");
231 inputstate |= INS_skip_frame;
232 }
233
234 break;
235#ifdef CONFIG_GIGASET_DEBUG
236 } else if (unlikely(muststuff(c))) {
237 /* Should not happen. Possible after ZDLE=1<CR><LF>. */
238 dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
239#endif
240 }
241
242 /* add character */
243
244#ifdef CONFIG_GIGASET_DEBUG
245 if (unlikely(!(inputstate & INS_have_data))) {
246 dbg(DEBUG_HDLC,
247 "7e (%d x) ================", bcs->emptycount);
248 bcs->emptycount = 0;
249 }
250#endif
251
252 inputstate |= INS_have_data;
253
254 if (likely(!(inputstate & INS_skip_frame))) {
255 if (unlikely(skb->len == SBUFSIZE)) {
256 warn("received packet too long");
257 dev_kfree_skb_any(skb);
258 skb = NULL;
259 inputstate |= INS_skip_frame;
260 break;
261 }
262 *gigaset_skb_put_quick(skb, 1) = c;
263 /* *__skb_put (skb, 1) = c; */
264 fcs = crc_ccitt_byte(fcs, c);
265 }
266
267 if (unlikely(!numbytes))
268 break;
269 c = *src++;
270 --numbytes;
271 if (unlikely(c == DLE_FLAG &&
272 (cs->dle ||
273 inbuf->inputstate & INS_DLE_command))) {
274 inbuf->inputstate |= INS_DLE_char;
275 break;
276 }
277 }
278 bcs->inputstate = inputstate;
279 bcs->fcs = fcs;
280 bcs->skb = skb;
281 return startbytes - numbytes;
282}
283
284/* process a block of received bytes in transparent data mode
285 * Invert bytes, undoing byte stuffing and watching for DLE escapes.
286 * If DLE is encountered, return immediately to let the caller handle it.
287 * Return value:
288 * number of processed bytes
289 * numbytes (all bytes processed) on error --FIXME
290 */
291static inline int iraw_loop(unsigned char c, unsigned char *src, int numbytes,
292 struct inbuf_t *inbuf)
293{
294 struct cardstate *cs = inbuf->cs;
295 struct bc_state *bcs = inbuf->bcs;
296 int inputstate;
297 struct sk_buff *skb;
298 int startbytes = numbytes;
299
300 IFNULLRETVAL(bcs, numbytes);
301 inputstate = bcs->inputstate;
302 skb = bcs->skb;
303 IFNULLRETVAL(skb, numbytes);
304
305 for (;;) {
306 /* add character */
307 inputstate |= INS_have_data;
308
309 if (likely(!(inputstate & INS_skip_frame))) {
310 if (unlikely(skb->len == SBUFSIZE)) {
311 //FIXME just pass skb up and allocate a new one
312 warn("received packet too long");
313 dev_kfree_skb_any(skb);
314 skb = NULL;
315 inputstate |= INS_skip_frame;
316 break;
317 }
318 *gigaset_skb_put_quick(skb, 1) = gigaset_invtab[c];
319 }
320
321 if (unlikely(!numbytes))
322 break;
323 c = *src++;
324 --numbytes;
325 if (unlikely(c == DLE_FLAG &&
326 (cs->dle ||
327 inbuf->inputstate & INS_DLE_command))) {
328 inbuf->inputstate |= INS_DLE_char;
329 break;
330 }
331 }
332
333 /* pass data up */
334 if (likely(inputstate & INS_have_data)) {
335 if (likely(!(inputstate & INS_skip_frame))) {
336 gigaset_rcv_skb(skb, cs, bcs);
337 }
338 inputstate &= ~(INS_have_data | INS_skip_frame);
339 if (unlikely(bcs->ignore)) {
340 inputstate |= INS_skip_frame;
341 skb = NULL;
342 } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN))
343 != NULL)) {
344 skb_reserve(skb, HW_HDR_LEN);
345 } else {
346 warn("could not allocate new skb");
347 inputstate |= INS_skip_frame;
348 }
349 }
350
351 bcs->inputstate = inputstate;
352 bcs->skb = skb;
353 return startbytes - numbytes;
354}
355
356/* process a block of data received from the device
357 */
358void gigaset_m10x_input(struct inbuf_t *inbuf)
359{
360 struct cardstate *cs;
361 unsigned tail, head, numbytes;
362 unsigned char *src, c;
363 int procbytes;
364
365 head = atomic_read(&inbuf->head);
366 tail = atomic_read(&inbuf->tail);
367 dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
368
369 if (head != tail) {
370 cs = inbuf->cs;
371 src = inbuf->data + head;
372 numbytes = (head > tail ? RBUFSIZE : tail) - head;
373 dbg(DEBUG_INTR, "processing %u bytes", numbytes);
374
375 while (numbytes) {
376 if (atomic_read(&cs->mstate) == MS_LOCKED) {
377 procbytes = lock_loop(src, numbytes, inbuf);
378 src += procbytes;
379 numbytes -= procbytes;
380 } else {
381 c = *src++;
382 --numbytes;
383 if (c == DLE_FLAG && (cs->dle ||
384 inbuf->inputstate & INS_DLE_command)) {
385 if (!(inbuf->inputstate & INS_DLE_char)) {
386 inbuf->inputstate |= INS_DLE_char;
387 goto nextbyte;
388 }
389 /* <DLE> <DLE> => <DLE> in data stream */
390 inbuf->inputstate &= ~INS_DLE_char;
391 }
392
393 if (!(inbuf->inputstate & INS_DLE_char)) {
394
395 /* FIXME Einfach je nach Modus Funktionszeiger in cs setzen [hier+hdlc_loop]? */
396 /* FIXME Spart folgendes "if" und ermoeglicht andere Protokolle */
397 if (inbuf->inputstate & INS_command)
398 procbytes = cmd_loop(c, src, numbytes, inbuf);
399 else if (inbuf->bcs->proto2 == ISDN_PROTO_L2_HDLC)
400 procbytes = hdlc_loop(c, src, numbytes, inbuf);
401 else
402 procbytes = iraw_loop(c, src, numbytes, inbuf);
403
404 src += procbytes;
405 numbytes -= procbytes;
406 } else { /* DLE-char */
407 inbuf->inputstate &= ~INS_DLE_char;
408 switch (c) {
409 case 'X': /*begin of command*/
410#ifdef CONFIG_GIGASET_DEBUG
411 if (inbuf->inputstate & INS_command)
412 err("received <DLE> 'X' in command mode");
413#endif
414 inbuf->inputstate |=
415 INS_command | INS_DLE_command;
416 break;
417 case '.': /*end of command*/
418#ifdef CONFIG_GIGASET_DEBUG
419 if (!(inbuf->inputstate & INS_command))
420 err("received <DLE> '.' in hdlc mode");
421#endif
422 inbuf->inputstate &= cs->dle ?
423 ~(INS_DLE_command|INS_command)
424 : ~INS_DLE_command;
425 break;
426 //case DLE_FLAG: /*DLE_FLAG in data stream*/ /* schon oben behandelt! */
427 default:
428 err("received 0x10 0x%02x!", (int) c);
429 /* FIXME: reset driver?? */
430 }
431 }
432 }
433nextbyte:
434 if (!numbytes) {
435 /* end of buffer, check for wrap */
436 if (head > tail) {
437 head = 0;
438 src = inbuf->data;
439 numbytes = tail;
440 } else {
441 head = tail;
442 break;
443 }
444 }
445 }
446
447 dbg(DEBUG_INTR, "setting head to %u", head);
448 atomic_set(&inbuf->head, head);
449 }
450}
451
452
453/* == data output ========================================================== */
454
455/* Encoding of a PPP packet into an octet stuffed HDLC frame
456 * with FCS, opening and closing flags.
457 * parameters:
458 * skb skb containing original packet (freed upon return)
459 * head number of headroom bytes to allocate in result skb
460 * tail number of tailroom bytes to allocate in result skb
461 * Return value:
462 * pointer to newly allocated skb containing the result frame
463 */
464static struct sk_buff *HDLC_Encode(struct sk_buff *skb, int head, int tail)
465{
466 struct sk_buff *hdlc_skb;
467 __u16 fcs;
468 unsigned char c;
469 unsigned char *cp;
470 int len;
471 unsigned int stuf_cnt;
472
473 stuf_cnt = 0;
474 fcs = PPP_INITFCS;
475 cp = skb->data;
476 len = skb->len;
477 while (len--) {
478 if (muststuff(*cp))
479 stuf_cnt++;
480 fcs = crc_ccitt_byte(fcs, *cp++);
481 }
482 fcs ^= 0xffff; /* complement */
483
484 /* size of new buffer: original size + number of stuffing bytes
485 * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
486 */
487 hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + tail + head);
488 if (!hdlc_skb) {
489 err("unable to allocate memory for HDLC encoding!");
490 dev_kfree_skb(skb);
491 return NULL;
492 }
493 skb_reserve(hdlc_skb, head);
494
495 /* Copy acknowledge request into new skb */
496 memcpy(hdlc_skb->head, skb->head, 2);
497
498 /* Add flag sequence in front of everything.. */
499 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
500
501 /* Perform byte stuffing while copying data. */
502 while (skb->len--) {
503 if (muststuff(*skb->data)) {
504 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
505 *(skb_put(hdlc_skb, 1)) = (*skb->data++) ^ PPP_TRANS;
506 } else
507 *(skb_put(hdlc_skb, 1)) = *skb->data++;
508 }
509
510 /* Finally add FCS (byte stuffed) and flag sequence */
511 c = (fcs & 0x00ff); /* least significant byte first */
512 if (muststuff(c)) {
513 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
514 c ^= PPP_TRANS;
515 }
516 *(skb_put(hdlc_skb, 1)) = c;
517
518 c = ((fcs >> 8) & 0x00ff);
519 if (muststuff(c)) {
520 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
521 c ^= PPP_TRANS;
522 }
523 *(skb_put(hdlc_skb, 1)) = c;
524
525 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
526
527 dev_kfree_skb(skb);
528 return hdlc_skb;
529}
530
531/* Encoding of a raw packet into an octet stuffed bit inverted frame
532 * parameters:
533 * skb skb containing original packet (freed upon return)
534 * head number of headroom bytes to allocate in result skb
535 * tail number of tailroom bytes to allocate in result skb
536 * Return value:
537 * pointer to newly allocated skb containing the result frame
538 */
539static struct sk_buff *iraw_encode(struct sk_buff *skb, int head, int tail)
540{
541 struct sk_buff *iraw_skb;
542 unsigned char c;
543 unsigned char *cp;
544 int len;
545
546 /* worst case: every byte must be stuffed */
547 iraw_skb = dev_alloc_skb(2*skb->len + tail + head);
548 if (!iraw_skb) {
549 err("unable to allocate memory for HDLC encoding!");
550 dev_kfree_skb(skb);
551 return NULL;
552 }
553 skb_reserve(iraw_skb, head);
554
555 cp = skb->data;
556 len = skb->len;
557 while (len--) {
558 c = gigaset_invtab[*cp++];
559 if (c == DLE_FLAG)
560 *(skb_put(iraw_skb, 1)) = c;
561 *(skb_put(iraw_skb, 1)) = c;
562 }
563 dev_kfree_skb(skb);
564 return iraw_skb;
565}
566
567/* gigaset_send_skb
568 * called by common.c to queue an skb for sending
569 * and start transmission if necessary
570 * parameters:
571 * B Channel control structure
572 * skb
573 * Return value:
574 * number of bytes accepted for sending
575 * (skb->len if ok, 0 if out of buffer space)
576 * or error code (< 0, eg. -EINVAL)
577 */
578int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
579{
580 unsigned len;
581
582 IFNULLRETVAL(bcs, -EFAULT);
583 IFNULLRETVAL(skb, -EFAULT);
584 len = skb->len;
585
586 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
587 skb = HDLC_Encode(skb, HW_HDR_LEN, 0);
588 else
589 skb = iraw_encode(skb, HW_HDR_LEN, 0);
590 if (!skb)
591 return -ENOMEM;
592
593 skb_queue_tail(&bcs->squeue, skb);
594 tasklet_schedule(&bcs->cs->write_tasklet);
595
596 return len; /* ok so far */
597}
diff --git a/drivers/isdn/gigaset/usb-gigaset.c b/drivers/isdn/gigaset/usb-gigaset.c
new file mode 100644
index 000000000000..323fc7349dec
--- /dev/null
+++ b/drivers/isdn/gigaset/usb-gigaset.c
@@ -0,0 +1,1008 @@
1/*
2 * USB driver for Gigaset 307x directly or using M105 Data.
3 *
4 * Copyright (c) 2001 by Stefan Eilers <Eilers.Stefan@epost.de>
5 * and Hansjoerg Lipp <hjlipp@web.de>.
6 *
7 * This driver was derived from the USB skeleton driver by
8 * Greg Kroah-Hartman <greg@kroah.com>
9 *
10 * =====================================================================
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 * =====================================================================
16 * ToDo: ...
17 * =====================================================================
18 * Version: $Id: usb-gigaset.c,v 1.85.4.18 2006/02/04 18:28:16 hjlipp Exp $
19 * =====================================================================
20 */
21
22#include "gigaset.h"
23
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/usb.h>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30
31/* Version Information */
32#define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers <Eilers.Stefan@epost.de>"
33#define DRIVER_DESC "USB Driver for Gigaset 307x using M105"
34
35/* Module parameters */
36
37static int startmode = SM_ISDN;
38static int cidmode = 1;
39
40module_param(startmode, int, S_IRUGO);
41module_param(cidmode, int, S_IRUGO);
42MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
43MODULE_PARM_DESC(cidmode, "Call-ID mode");
44
45#define GIGASET_MINORS 1
46#define GIGASET_MINOR 8
47#define GIGASET_MODULENAME "usb_gigaset"
48#define GIGASET_DEVFSNAME "gig/usb/"
49#define GIGASET_DEVNAME "ttyGU"
50
51#define IF_WRITEBUF 2000 //FIXME // WAKEUP_CHARS: 256
52
53/* Values for the Gigaset M105 Data */
54#define USB_M105_VENDOR_ID 0x0681
55#define USB_M105_PRODUCT_ID 0x0009
56
57/* table of devices that work with this driver */
58static struct usb_device_id gigaset_table [] = {
59 { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
60 { } /* Terminating entry */
61};
62
63MODULE_DEVICE_TABLE(usb, gigaset_table);
64
65/* Get a minor range for your devices from the usb maintainer */
66#define USB_SKEL_MINOR_BASE 200
67
68
69/*
70 * Control requests (empty fields: 00)
71 *
72 * RT|RQ|VALUE|INDEX|LEN |DATA
73 * In:
74 * C1 08 01
75 * Get flags (1 byte). Bits: 0=dtr,1=rts,3-7:?
76 * C1 0F ll ll
77 * Get device information/status (llll: 0x200 and 0x40 seen).
78 * Real size: I only saw MIN(llll,0x64).
79 * Contents: seems to be always the same...
80 * offset 0x00: Length of this structure (0x64) (len: 1,2,3 bytes)
81 * offset 0x3c: String (16 bit chars): "MCCI USB Serial V2.0"
82 * rest: ?
83 * Out:
84 * 41 11
85 * Initialize/reset device ?
86 * 41 00 xx 00
87 * ? (xx=00 or 01; 01 on start, 00 on close)
88 * 41 07 vv mm
89 * Set/clear flags vv=value, mm=mask (see RQ 08)
90 * 41 12 xx
91 * Used before the following configuration requests are issued
92 * (with xx=0x0f). I've seen other values<0xf, though.
93 * 41 01 xx xx
94 * Set baud rate. xxxx=ceil(0x384000/rate)=trunc(0x383fff/rate)+1.
95 * 41 03 ps bb
96 * Set byte size and parity. p: 0x20=even,0x10=odd,0x00=no parity
97 * [ 0x30: m, 0x40: s ]
98 * [s: 0: 1 stop bit; 1: 1.5; 2: 2]
99 * bb: bits/byte (seen 7 and 8)
100 * 41 13 -- -- -- -- 10 00 ww 00 00 00 xx 00 00 00 yy 00 00 00 zz 00 00 00
101 * ??
102 * Initialization: 01, 40, 00, 00
103 * Open device: 00 40, 00, 00
104 * yy and zz seem to be equal, either 0x00 or 0x0a
105 * (ww,xx) pairs seen: (00,00), (00,40), (01,40), (09,80), (19,80)
106 * 41 19 -- -- -- -- 06 00 00 00 00 xx 11 13
107 * Used after every "configuration sequence" (RQ 12, RQs 01/03/13).
108 * xx is usually 0x00 but was 0x7e before starting data transfer
109 * in unimodem mode. So, this might be an array of characters that need
110 * special treatment ("commit all bufferd data"?), 11=^Q, 13=^S.
111 *
112 * Unimodem mode: use "modprobe ppp_async flag_time=0" as the device _needs_ two
113 * flags per packet.
114 */
115
116static int gigaset_probe(struct usb_interface *interface,
117 const struct usb_device_id *id);
118static void gigaset_disconnect(struct usb_interface *interface);
119
120static struct gigaset_driver *driver = NULL;
121static struct cardstate *cardstate = NULL;
122
123/* usb specific object needed to register this driver with the usb subsystem */
124static struct usb_driver gigaset_usb_driver = {
125 .name = GIGASET_MODULENAME,
126 .probe = gigaset_probe,
127 .disconnect = gigaset_disconnect,
128 .id_table = gigaset_table,
129};
130
131struct usb_cardstate {
132 struct usb_device *udev; /* save off the usb device pointer */
133 struct usb_interface *interface; /* the interface for this device */
134 atomic_t busy; /* bulk output in progress */
135
136 /* Output buffer for commands (M105: and data)*/
137 unsigned char *bulk_out_buffer; /* the buffer to send data */
138 int bulk_out_size; /* the size of the send buffer */
139 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
140 struct urb *bulk_out_urb; /* the urb used to transmit data */
141
142 /* Input buffer for command responses (M105: and data)*/
143 int rcvbuf_size; /* the size of the receive buffer */
144 struct urb *read_urb; /* the urb used to receive data */
145 __u8 int_in_endpointAddr; /* the address of the bulk in endpoint */
146
147 char bchars[6]; /* req. 0x19 */
148};
149
150struct usb_bc_state {};
151
152static inline unsigned tiocm_to_gigaset(unsigned state)
153{
154 return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0);
155}
156
157#ifdef CONFIG_GIGASET_UNDOCREQ
158/* WARNING: EXPERIMENTAL! */
159static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
160 unsigned new_state)
161{
162 unsigned mask, val;
163 int r;
164
165 mask = tiocm_to_gigaset(old_state ^ new_state);
166 val = tiocm_to_gigaset(new_state);
167
168 dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask);
169 r = usb_control_msg(cs->hw.usb->udev,
170 usb_sndctrlpipe(cs->hw.usb->udev, 0), 7, 0x41,
171 (val & 0xff) | ((mask & 0xff) << 8), 0,
172 NULL, 0, 2000 /*timeout??*/); // don't use this in an interrupt/BH
173 if (r < 0)
174 return r;
175 //..
176 return 0;
177}
178
179static int set_value(struct cardstate *cs, u8 req, u16 val)
180{
181 int r, r2;
182
183 dbg(DEBUG_USBREQ, "request %02x (%04x)", (unsigned)req, (unsigned)val);
184 r = usb_control_msg(cs->hw.usb->udev,
185 usb_sndctrlpipe(cs->hw.usb->udev, 0), 0x12, 0x41,
186 0xf /*?*/, 0,
187 NULL, 0, 2000 /*?*/); /* no idea, what this does */
188 if (r < 0) {
189 err("error %d on request 0x12", -r);
190 return r;
191 }
192
193 r = usb_control_msg(cs->hw.usb->udev,
194 usb_sndctrlpipe(cs->hw.usb->udev, 0), req, 0x41,
195 val, 0,
196 NULL, 0, 2000 /*?*/);
197 if (r < 0)
198 err("error %d on request 0x%02x", -r, (unsigned)req);
199
200 r2 = usb_control_msg(cs->hw.usb->udev,
201 usb_sndctrlpipe(cs->hw.usb->udev, 0), 0x19, 0x41,
202 0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/);
203 if (r2 < 0)
204 err("error %d on request 0x19", -r2);
205
206 return r < 0 ? r : (r2 < 0 ? r2 : 0);
207}
208
209/* WARNING: HIGHLY EXPERIMENTAL! */
210// don't use this in an interrupt/BH
211static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
212{
213 u16 val;
214 u32 rate;
215
216 cflag &= CBAUD;
217
218 switch (cflag) {
219 //FIXME more values?
220 case B300: rate = 300; break;
221 case B600: rate = 600; break;
222 case B1200: rate = 1200; break;
223 case B2400: rate = 2400; break;
224 case B4800: rate = 4800; break;
225 case B9600: rate = 9600; break;
226 case B19200: rate = 19200; break;
227 case B38400: rate = 38400; break;
228 case B57600: rate = 57600; break;
229 case B115200: rate = 115200; break;
230 default:
231 rate = 9600;
232 err("unsupported baudrate request 0x%x,"
233 " using default of B9600", cflag);
234 }
235
236 val = 0x383fff / rate + 1;
237
238 return set_value(cs, 1, val);
239}
240
241/* WARNING: HIGHLY EXPERIMENTAL! */
242// don't use this in an interrupt/BH
243static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
244{
245 u16 val = 0;
246
247 /* set the parity */
248 if (cflag & PARENB)
249 val |= (cflag & PARODD) ? 0x10 : 0x20;
250
251 /* set the number of data bits */
252 switch (cflag & CSIZE) {
253 case CS5:
254 val |= 5 << 8; break;
255 case CS6:
256 val |= 6 << 8; break;
257 case CS7:
258 val |= 7 << 8; break;
259 case CS8:
260 val |= 8 << 8; break;
261 default:
262 err("CSIZE was not CS5-CS8, using default of 8");
263 val |= 8 << 8;
264 break;
265 }
266
267 /* set the number of stop bits */
268 if (cflag & CSTOPB) {
269 if ((cflag & CSIZE) == CS5)
270 val |= 1; /* 1.5 stop bits */ //FIXME is this okay?
271 else
272 val |= 2; /* 2 stop bits */
273 }
274
275 return set_value(cs, 3, val);
276}
277
278#else
279static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
280 unsigned new_state)
281{
282 return -EINVAL;
283}
284
285static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
286{
287 return -EINVAL;
288}
289
290static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
291{
292 return -EINVAL;
293}
294#endif
295
296
297 /*================================================================================================================*/
298static int gigaset_init_bchannel(struct bc_state *bcs)
299{
300 /* nothing to do for M10x */
301 gigaset_bchannel_up(bcs);
302 return 0;
303}
304
305static int gigaset_close_bchannel(struct bc_state *bcs)
306{
307 /* nothing to do for M10x */
308 gigaset_bchannel_down(bcs);
309 return 0;
310}
311
312//void send_ack_to_LL(void *data);
313static int write_modem(struct cardstate *cs);
314static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb);
315
316
317/* Handling of send queue. If there is already a skb opened, put data to
318 * the transfer buffer by calling "write_modem". Otherwise take a new skb out of the queue.
319 * This function will be called by the ISR via "transmit_chars" (USB: B-Channel Bulk callback handler
320 * via immediate task queue) or by writebuf_from_LL if the LL wants to transmit data.
321 */
322static void gigaset_modem_fill(unsigned long data)
323{
324 struct cardstate *cs = (struct cardstate *) data;
325 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
326 struct cmdbuf_t *cb;
327 unsigned long flags;
328 int again;
329
330 dbg(DEBUG_OUTPUT, "modem_fill");
331
332 if (atomic_read(&cs->hw.usb->busy)) {
333 dbg(DEBUG_OUTPUT, "modem_fill: busy");
334 return;
335 }
336
337 do {
338 again = 0;
339 if (!bcs->tx_skb) { /* no skb is being sent */
340 spin_lock_irqsave(&cs->cmdlock, flags);
341 cb = cs->cmdbuf;
342 spin_unlock_irqrestore(&cs->cmdlock, flags);
343 if (cb) { /* commands to send? */
344 dbg(DEBUG_OUTPUT, "modem_fill: cb");
345 if (send_cb(cs, cb) < 0) {
346 dbg(DEBUG_OUTPUT,
347 "modem_fill: send_cb failed");
348 again = 1; /* no callback will be called! */
349 }
350 } else { /* skbs to send? */
351 bcs->tx_skb = skb_dequeue(&bcs->squeue);
352 if (bcs->tx_skb)
353 dbg(DEBUG_INTR,
354 "Dequeued skb (Adr: %lx)!",
355 (unsigned long) bcs->tx_skb);
356 }
357 }
358
359 if (bcs->tx_skb) {
360 dbg(DEBUG_OUTPUT, "modem_fill: tx_skb");
361 if (write_modem(cs) < 0) {
362 dbg(DEBUG_OUTPUT,
363 "modem_fill: write_modem failed");
364 // FIXME should we tell the LL?
365 again = 1; /* no callback will be called! */
366 }
367 }
368 } while (again);
369}
370
371/**
372 * gigaset_read_int_callback
373 *
374 * It is called if the data was received from the device. This is almost similiar to
375 * the interrupt service routine in the serial device.
376 */
377static void gigaset_read_int_callback(struct urb *urb, struct pt_regs *regs)
378{
379 int resubmit = 0;
380 int r;
381 struct cardstate *cs;
382 unsigned numbytes;
383 unsigned char *src;
384 //unsigned long flags;
385 struct inbuf_t *inbuf;
386
387 IFNULLRET(urb);
388 inbuf = (struct inbuf_t *) urb->context;
389 IFNULLRET(inbuf);
390 //spin_lock_irqsave(&inbuf->lock, flags);
391 cs = inbuf->cs;
392 IFNULLGOTO(cs, exit);
393 IFNULLGOTO(cardstate, exit);
394
395 if (!atomic_read(&cs->connected)) {
396 err("%s: disconnected", __func__);
397 goto exit;
398 }
399
400 if (!urb->status) {
401 numbytes = urb->actual_length;
402
403 if (numbytes) {
404 src = inbuf->rcvbuf;
405 if (unlikely(*src))
406 warn("%s: There was no leading 0, but 0x%02x!",
407 __func__, (unsigned) *src);
408 ++src; /* skip leading 0x00 */
409 --numbytes;
410 if (gigaset_fill_inbuf(inbuf, src, numbytes)) {
411 dbg(DEBUG_INTR, "%s-->BH", __func__);
412 gigaset_schedule_event(inbuf->cs);
413 }
414 } else
415 dbg(DEBUG_INTR, "Received zero block length");
416 resubmit = 1;
417 } else {
418 /* The urb might have been killed. */
419 dbg(DEBUG_ANY, "%s - nonzero read bulk status received: %d",
420 __func__, urb->status);
421 if (urb->status != -ENOENT) /* not killed */
422 resubmit = 1;
423 }
424exit:
425 //spin_unlock_irqrestore(&inbuf->lock, flags);
426 if (resubmit) {
427 r = usb_submit_urb(urb, SLAB_ATOMIC);
428 if (r)
429 err("error %d when resubmitting urb.", -r);
430 }
431}
432
433
434/* This callback routine is called when data was transmitted to a B-Channel.
435 * Therefore it has to check if there is still data to transmit. This
436 * happens by calling modem_fill via task queue.
437 *
438 */
439static void gigaset_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
440{
441 struct cardstate *cs = (struct cardstate *) urb->context;
442
443 IFNULLRET(cs);
444#ifdef CONFIG_GIGASET_DEBUG
445 if (!atomic_read(&cs->connected)) {
446 err("%s:not connected", __func__);
447 return;
448 }
449#endif
450 if (urb->status)
451 err("bulk transfer failed (status %d)", -urb->status); /* That's all we can do. Communication problems
452 are handeled by timeouts or network protocols */
453
454 atomic_set(&cs->hw.usb->busy, 0);
455 tasklet_schedule(&cs->write_tasklet);
456}
457
458static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb)
459{
460 struct cmdbuf_t *tcb;
461 unsigned long flags;
462 int count;
463 int status = -ENOENT; // FIXME
464 struct usb_cardstate *ucs = cs->hw.usb;
465
466 do {
467 if (!cb->len) {
468 tcb = cb;
469
470 spin_lock_irqsave(&cs->cmdlock, flags);
471 cs->cmdbytes -= cs->curlen;
472 dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left",
473 cs->curlen, cs->cmdbytes);
474 cs->cmdbuf = cb = cb->next;
475 if (cb) {
476 cb->prev = NULL;
477 cs->curlen = cb->len;
478 } else {
479 cs->lastcmdbuf = NULL;
480 cs->curlen = 0;
481 }
482 spin_unlock_irqrestore(&cs->cmdlock, flags);
483
484 if (tcb->wake_tasklet)
485 tasklet_schedule(tcb->wake_tasklet);
486 kfree(tcb);
487 }
488 if (cb) {
489 count = min(cb->len, ucs->bulk_out_size);
490 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
491 usb_sndbulkpipe(ucs->udev,
492 ucs->bulk_out_endpointAddr & 0x0f),
493 cb->buf + cb->offset, count,
494 gigaset_write_bulk_callback, cs);
495
496 cb->offset += count;
497 cb->len -= count;
498 atomic_set(&ucs->busy, 1);
499 dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count);
500
501 status = usb_submit_urb(ucs->bulk_out_urb, SLAB_ATOMIC);
502 if (status) {
503 atomic_set(&ucs->busy, 0);
504 err("could not submit urb (error %d).",
505 -status);
506 cb->len = 0; /* skip urb => remove cb+wakeup in next loop cycle */
507 }
508 }
509 } while (cb && status); /* bei Fehler naechster Befehl //FIXME: ist das OK? */
510
511 return status;
512}
513
514/* Write string into transbuf and send it to modem.
515 */
516static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
517 int len, struct tasklet_struct *wake_tasklet)
518{
519 struct cmdbuf_t *cb;
520 unsigned long flags;
521
522 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
523 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
524 "CMD Transmit", len, buf, 0);
525
526 if (!atomic_read(&cs->connected)) {
527 err("%s: not connected", __func__);
528 return -ENODEV;
529 }
530
531 if (len <= 0)
532 return 0;
533
534 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
535 err("%s: out of memory", __func__);
536 return -ENOMEM;
537 }
538
539 memcpy(cb->buf, buf, len);
540 cb->len = len;
541 cb->offset = 0;
542 cb->next = NULL;
543 cb->wake_tasklet = wake_tasklet;
544
545 spin_lock_irqsave(&cs->cmdlock, flags);
546 cb->prev = cs->lastcmdbuf;
547 if (cs->lastcmdbuf)
548 cs->lastcmdbuf->next = cb;
549 else {
550 cs->cmdbuf = cb;
551 cs->curlen = len;
552 }
553 cs->cmdbytes += len;
554 cs->lastcmdbuf = cb;
555 spin_unlock_irqrestore(&cs->cmdlock, flags);
556
557 tasklet_schedule(&cs->write_tasklet);
558 return len;
559}
560
561static int gigaset_write_room(struct cardstate *cs)
562{
563 unsigned long flags;
564 unsigned bytes;
565
566 spin_lock_irqsave(&cs->cmdlock, flags);
567 bytes = cs->cmdbytes;
568 spin_unlock_irqrestore(&cs->cmdlock, flags);
569
570 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
571}
572
573static int gigaset_chars_in_buffer(struct cardstate *cs)
574{
575 return cs->cmdbytes;
576}
577
578static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
579{
580#ifdef CONFIG_GIGASET_UNDOCREQ
581 gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf, 0);
582 memcpy(cs->hw.usb->bchars, buf, 6);
583 return usb_control_msg(cs->hw.usb->udev,
584 usb_sndctrlpipe(cs->hw.usb->udev, 0), 0x19, 0x41,
585 0, 0, &buf, 6, 2000);
586#else
587 return -EINVAL;
588#endif
589}
590
591static int gigaset_freebcshw(struct bc_state *bcs)
592{
593 if (!bcs->hw.usb)
594 return 0;
595 //FIXME
596 kfree(bcs->hw.usb);
597 return 1;
598}
599
600/* Initialize the b-channel structure */
601static int gigaset_initbcshw(struct bc_state *bcs)
602{
603 bcs->hw.usb = kmalloc(sizeof(struct usb_bc_state), GFP_KERNEL);
604 if (!bcs->hw.usb)
605 return 0;
606
607 //bcs->hw.usb->trans_flg = READY_TO_TRNSMIT; /* B-Channel ready to transmit */
608 return 1;
609}
610
611static void gigaset_reinitbcshw(struct bc_state *bcs)
612{
613}
614
615static void gigaset_freecshw(struct cardstate *cs)
616{
617 //FIXME
618 tasklet_kill(&cs->write_tasklet);
619 kfree(cs->hw.usb);
620}
621
622static int gigaset_initcshw(struct cardstate *cs)
623{
624 struct usb_cardstate *ucs;
625
626 cs->hw.usb = ucs =
627 kmalloc(sizeof(struct usb_cardstate), GFP_KERNEL);
628 if (!ucs)
629 return 0;
630
631 ucs->bchars[0] = 0;
632 ucs->bchars[1] = 0;
633 ucs->bchars[2] = 0;
634 ucs->bchars[3] = 0;
635 ucs->bchars[4] = 0x11;
636 ucs->bchars[5] = 0x13;
637 ucs->bulk_out_buffer = NULL;
638 ucs->bulk_out_urb = NULL;
639 //ucs->urb_cmd_out = NULL;
640 ucs->read_urb = NULL;
641 tasklet_init(&cs->write_tasklet,
642 &gigaset_modem_fill, (unsigned long) cs);
643
644 return 1;
645}
646
647/* Writes the data of the current open skb into the modem.
648 * We have to protect against multiple calls until the
649 * callback handler () is called , due to the fact that we
650 * are just allowed to send data once to an endpoint. Therefore
651 * we using "trans_flg" to synchonize ...
652 */
653static int write_modem(struct cardstate *cs)
654{
655 int ret;
656 int count;
657 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
658 struct usb_cardstate *ucs = cs->hw.usb;
659 //unsigned long flags;
660
661 IFNULLRETVAL(bcs->tx_skb, -EINVAL);
662
663 dbg(DEBUG_WRITE, "len: %d...", bcs->tx_skb->len);
664
665 ret = -ENODEV;
666 IFNULLGOTO(ucs->bulk_out_buffer, error);
667 IFNULLGOTO(ucs->bulk_out_urb, error);
668 ret = 0;
669
670 if (!bcs->tx_skb->len) {
671 dev_kfree_skb_any(bcs->tx_skb);
672 bcs->tx_skb = NULL;
673 return -EINVAL;
674 }
675
676 /* Copy data to bulk out buffer and // FIXME copying not necessary
677 * transmit data
678 */
679 count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size);
680 memcpy(ucs->bulk_out_buffer, bcs->tx_skb->data, count);
681 skb_pull(bcs->tx_skb, count);
682
683 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
684 usb_sndbulkpipe(ucs->udev,
685 ucs->bulk_out_endpointAddr & 0x0f),
686 ucs->bulk_out_buffer, count,
687 gigaset_write_bulk_callback, cs);
688 atomic_set(&ucs->busy, 1);
689 dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count);
690
691 ret = usb_submit_urb(ucs->bulk_out_urb, SLAB_ATOMIC);
692 if (ret) {
693 err("could not submit urb (error %d).", -ret);
694 atomic_set(&ucs->busy, 0);
695 }
696 if (!bcs->tx_skb->len) {
697 /* skb sent completely */
698 gigaset_skb_sent(bcs, bcs->tx_skb); //FIXME also, when ret<0?
699
700 dbg(DEBUG_INTR,
701 "kfree skb (Adr: %lx)!", (unsigned long) bcs->tx_skb);
702 dev_kfree_skb_any(bcs->tx_skb);
703 bcs->tx_skb = NULL;
704 }
705
706 return ret;
707error:
708 dev_kfree_skb_any(bcs->tx_skb);
709 bcs->tx_skb = NULL;
710 return ret;
711
712}
713
714static int gigaset_probe(struct usb_interface *interface,
715 const struct usb_device_id *id)
716{
717 int retval;
718 struct usb_device *udev = interface_to_usbdev(interface);
719 unsigned int ifnum;
720 struct usb_host_interface *hostif;
721 struct cardstate *cs = NULL;
722 struct usb_cardstate *ucs = NULL;
723 //struct usb_interface_descriptor *iface_desc;
724 struct usb_endpoint_descriptor *endpoint;
725 //isdn_ctrl command;
726 int buffer_size;
727 int alt;
728 //unsigned long flags;
729
730 info("%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
731 __func__, le16_to_cpu(udev->descriptor.idVendor),
732 le16_to_cpu(udev->descriptor.idProduct));
733
734 retval = -ENODEV; //FIXME
735
736 /* See if the device offered us matches what we can accept */
737 if ((le16_to_cpu(udev->descriptor.idVendor != USB_M105_VENDOR_ID)) ||
738 (le16_to_cpu(udev->descriptor.idProduct != USB_M105_PRODUCT_ID)))
739 return -ENODEV;
740
741 /* this starts to become ascii art... */
742 hostif = interface->cur_altsetting;
743 alt = hostif->desc.bAlternateSetting;
744 ifnum = hostif->desc.bInterfaceNumber; // FIXME ?
745
746 if (alt != 0 || ifnum != 0) {
747 warn("ifnum %d, alt %d", ifnum, alt);
748 return -ENODEV;
749 }
750
751 /* Reject application specific intefaces
752 *
753 */
754 if (hostif->desc.bInterfaceClass != 255) {
755 info("%s: Device matched, but iface_desc[%d]->bInterfaceClass==%d !",
756 __func__, ifnum, hostif->desc.bInterfaceClass);
757 return -ENODEV;
758 }
759
760 info("%s: Device matched ... !", __func__);
761
762 cs = gigaset_getunassignedcs(driver);
763 if (!cs) {
764 warn("No free cardstate!");
765 return -ENODEV;
766 }
767 ucs = cs->hw.usb;
768
769#if 0
770 if (usb_set_configuration(udev, udev->config[0].desc.bConfigurationValue) < 0) {
771 warn("set_configuration failed");
772 goto error;
773 }
774
775
776 if (usb_set_interface(udev, ifnum/*==0*/, alt/*==0*/) < 0) {
777 warn("usb_set_interface failed, device %d interface %d altsetting %d",
778 udev->devnum, ifnum, alt);
779 goto error;
780 }
781#endif
782
783 /* set up the endpoint information */
784 /* check out the endpoints */
785 /* We will get 2 endpoints: One for sending commands to the device (bulk out) and one to
786 * poll messages from the device(int in).
787 * Therefore we will have an almost similiar situation as with our serial port handler.
788 * If an connection will be established, we will have to create data in/out pipes
789 * dynamically...
790 */
791
792 endpoint = &hostif->endpoint[0].desc;
793
794 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
795 ucs->bulk_out_size = buffer_size;
796 ucs->bulk_out_endpointAddr = endpoint->bEndpointAddress;
797 ucs->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
798 if (!ucs->bulk_out_buffer) {
799 err("Couldn't allocate bulk_out_buffer");
800 retval = -ENOMEM;
801 goto error;
802 }
803
804 ucs->bulk_out_urb = usb_alloc_urb(0, SLAB_KERNEL);
805 if (!ucs->bulk_out_urb) {
806 err("Couldn't allocate bulk_out_buffer");
807 retval = -ENOMEM;
808 goto error;
809 }
810
811 endpoint = &hostif->endpoint[1].desc;
812
813 atomic_set(&ucs->busy, 0);
814 ucs->udev = udev;
815 ucs->interface = interface;
816
817 ucs->read_urb = usb_alloc_urb(0, SLAB_KERNEL);
818 if (!ucs->read_urb) {
819 err("No free urbs available");
820 retval = -ENOMEM;
821 goto error;
822 }
823 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
824 ucs->rcvbuf_size = buffer_size;
825 ucs->int_in_endpointAddr = endpoint->bEndpointAddress;
826 cs->inbuf[0].rcvbuf = kmalloc(buffer_size, GFP_KERNEL);
827 if (!cs->inbuf[0].rcvbuf) {
828 err("Couldn't allocate rcvbuf");
829 retval = -ENOMEM;
830 goto error;
831 }
832 /* Fill the interrupt urb and send it to the core */
833 usb_fill_int_urb(ucs->read_urb, udev,
834 usb_rcvintpipe(udev,
835 endpoint->bEndpointAddress & 0x0f),
836 cs->inbuf[0].rcvbuf, buffer_size,
837 gigaset_read_int_callback,
838 cs->inbuf + 0, endpoint->bInterval);
839
840 retval = usb_submit_urb(ucs->read_urb, SLAB_KERNEL);
841 if (retval) {
842 err("Could not submit URB!");
843 goto error;
844 }
845
846 /* tell common part that the device is ready */
847 if (startmode == SM_LOCKED)
848 atomic_set(&cs->mstate, MS_LOCKED);
849 if (!gigaset_start(cs)) {
850 tasklet_kill(&cs->write_tasklet);
851 retval = -ENODEV; //FIXME
852 goto error;
853 }
854
855 /* save address of controller structure */
856 usb_set_intfdata(interface, cs);
857
858 /* set up device sysfs */
859 gigaset_init_dev_sysfs(interface);
860 return 0;
861
862error:
863 if (ucs->read_urb)
864 usb_kill_urb(ucs->read_urb);
865 kfree(ucs->bulk_out_buffer);
866 if (ucs->bulk_out_urb != NULL)
867 usb_free_urb(ucs->bulk_out_urb);
868 kfree(cs->inbuf[0].rcvbuf);
869 if (ucs->read_urb != NULL)
870 usb_free_urb(ucs->read_urb);
871 ucs->read_urb = ucs->bulk_out_urb = NULL;
872 cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
873 gigaset_unassign(cs);
874 return retval;
875}
876
877/**
878 * skel_disconnect
879 */
880static void gigaset_disconnect(struct usb_interface *interface)
881{
882 struct cardstate *cs;
883 struct usb_cardstate *ucs;
884
885 cs = usb_get_intfdata(interface);
886
887 /* clear device sysfs */
888 gigaset_free_dev_sysfs(interface);
889
890 usb_set_intfdata(interface, NULL);
891 ucs = cs->hw.usb;
892 usb_kill_urb(ucs->read_urb);
893 //info("GigaSet USB device #%d will be disconnected", minor);
894
895 gigaset_stop(cs);
896
897 tasklet_kill(&cs->write_tasklet);
898
899 usb_kill_urb(ucs->bulk_out_urb); /* FIXME: nur, wenn noetig */
900 //usb_kill_urb(ucs->urb_cmd_out); /* FIXME: nur, wenn noetig */
901
902 kfree(ucs->bulk_out_buffer);
903 if (ucs->bulk_out_urb != NULL)
904 usb_free_urb(ucs->bulk_out_urb);
905 //if(ucs->urb_cmd_out != NULL)
906 // usb_free_urb(ucs->urb_cmd_out);
907 kfree(cs->inbuf[0].rcvbuf);
908 if (ucs->read_urb != NULL)
909 usb_free_urb(ucs->read_urb);
910 ucs->read_urb = ucs->bulk_out_urb/*=ucs->urb_cmd_out*/=NULL;
911 cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
912
913 gigaset_unassign(cs);
914}
915
916static struct gigaset_ops ops = {
917 gigaset_write_cmd,
918 gigaset_write_room,
919 gigaset_chars_in_buffer,
920 gigaset_brkchars,
921 gigaset_init_bchannel,
922 gigaset_close_bchannel,
923 gigaset_initbcshw,
924 gigaset_freebcshw,
925 gigaset_reinitbcshw,
926 gigaset_initcshw,
927 gigaset_freecshw,
928 gigaset_set_modem_ctrl,
929 gigaset_baud_rate,
930 gigaset_set_line_ctrl,
931 gigaset_m10x_send_skb,
932 gigaset_m10x_input,
933};
934
935/**
936 * usb_gigaset_init
937 * This function is called while kernel-module is loaded
938 */
939static int __init usb_gigaset_init(void)
940{
941 int result;
942
943 /* allocate memory for our driver state and intialize it */
944 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
945 GIGASET_MODULENAME, GIGASET_DEVNAME,
946 GIGASET_DEVFSNAME, &ops,
947 THIS_MODULE)) == NULL)
948 goto error;
949
950 /* allocate memory for our device state and intialize it */
951 cardstate = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
952 if (!cardstate)
953 goto error;
954
955 /* register this driver with the USB subsystem */
956 result = usb_register(&gigaset_usb_driver);
957 if (result < 0) {
958 err("usb_gigaset: usb_register failed (error %d)",
959 -result);
960 goto error;
961 }
962
963 info(DRIVER_AUTHOR);
964 info(DRIVER_DESC);
965 return 0;
966
967error: if (cardstate)
968 gigaset_freecs(cardstate);
969 cardstate = NULL;
970 if (driver)
971 gigaset_freedriver(driver);
972 driver = NULL;
973 return -1;
974}
975
976
977/**
978 * usb_gigaset_exit
979 * This function is called while unloading the kernel-module
980 */
981static void __exit usb_gigaset_exit(void)
982{
983 gigaset_blockdriver(driver); /* => probe will fail
984 * => no gigaset_start any more
985 */
986
987 gigaset_shutdown(cardstate);
988 /* from now on, no isdn callback should be possible */
989
990 /* deregister this driver with the USB subsystem */
991 usb_deregister(&gigaset_usb_driver);
992 /* this will call the disconnect-callback */
993 /* from now on, no disconnect/probe callback should be running */
994
995 gigaset_freecs(cardstate);
996 cardstate = NULL;
997 gigaset_freedriver(driver);
998 driver = NULL;
999}
1000
1001
1002module_init(usb_gigaset_init);
1003module_exit(usb_gigaset_exit);
1004
1005MODULE_AUTHOR(DRIVER_AUTHOR);
1006MODULE_DESCRIPTION(DRIVER_DESC);
1007
1008MODULE_LICENSE("GPL");