ACPI: EC acpi-ecdt-uid-hack
[pandora-kernel.git] / drivers / isdn / gigaset / usb-gigaset.c
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
37 static int startmode = SM_ISDN;
38 static int cidmode = 1;
39
40 module_param(startmode, int, S_IRUGO);
41 module_param(cidmode, int, S_IRUGO);
42 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
43 MODULE_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 */
58 static struct usb_device_id gigaset_table [] = {
59         { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
60         { }                                     /* Terminating entry */
61 };
62
63 MODULE_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
116 static int gigaset_probe(struct usb_interface *interface,
117                          const struct usb_device_id *id);
118 static void gigaset_disconnect(struct usb_interface *interface);
119
120 static struct gigaset_driver *driver = NULL;
121 static struct cardstate *cardstate = NULL;
122
123 /* usb specific object needed to register this driver with the usb subsystem */
124 static 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
131 struct 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
150 struct usb_bc_state {};
151
152 static 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! */
159 static 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
179 static 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
211 static 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
243 static 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
279 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
280                                   unsigned new_state)
281 {
282         return -EINVAL;
283 }
284
285 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
286 {
287         return -EINVAL;
288 }
289
290 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
291 {
292         return -EINVAL;
293 }
294 #endif
295
296
297  /*================================================================================================================*/
298 static 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
305 static 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);
313 static int write_modem(struct cardstate *cs);
314 static 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  */
322 static 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  */
377 static 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         }
424 exit:
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  */
439 static 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
458 static 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  */
516 static 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
561 static 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
573 static int gigaset_chars_in_buffer(struct cardstate *cs)
574 {
575         return cs->cmdbytes;
576 }
577
578 static 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
591 static 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 */
601 static 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
611 static void gigaset_reinitbcshw(struct bc_state *bcs)
612 {
613 }
614
615 static void gigaset_freecshw(struct cardstate *cs)
616 {
617         //FIXME
618         tasklet_kill(&cs->write_tasklet);
619         kfree(cs->hw.usb);
620 }
621
622 static 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  */
653 static 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;
707 error:
708         dev_kfree_skb_any(bcs->tx_skb);
709         bcs->tx_skb = NULL;
710         return ret;
711
712 }
713
714 static 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
862 error:
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  */
880 static 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
916 static 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  */
939 static 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
967 error:  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  */
981 static 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
1002 module_init(usb_gigaset_init);
1003 module_exit(usb_gigaset_exit);
1004
1005 MODULE_AUTHOR(DRIVER_AUTHOR);
1006 MODULE_DESCRIPTION(DRIVER_DESC);
1007
1008 MODULE_LICENSE("GPL");