0f5aa46cf8f38de5d5eb3b996e5e15a6b6d1d640
[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  */
17
18 #include "gigaset.h"
19
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26
27 /* Version Information */
28 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers <Eilers.Stefan@epost.de>"
29 #define DRIVER_DESC "USB Driver for Gigaset 307x using M105"
30
31 /* Module parameters */
32
33 static int startmode = SM_ISDN;
34 static int cidmode = 1;
35
36 module_param(startmode, int, S_IRUGO);
37 module_param(cidmode, int, S_IRUGO);
38 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
39 MODULE_PARM_DESC(cidmode, "Call-ID mode");
40
41 #define GIGASET_MINORS     1
42 #define GIGASET_MINOR      8
43 #define GIGASET_MODULENAME "usb_gigaset"
44 #define GIGASET_DEVFSNAME  "gig/usb/"
45 #define GIGASET_DEVNAME    "ttyGU"
46
47 #define IF_WRITEBUF 2000 //FIXME  // WAKEUP_CHARS: 256
48
49 /* Values for the Gigaset M105 Data */
50 #define USB_M105_VENDOR_ID      0x0681
51 #define USB_M105_PRODUCT_ID     0x0009
52
53 /* table of devices that work with this driver */
54 static struct usb_device_id gigaset_table [] = {
55         { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
56         { }                                     /* Terminating entry */
57 };
58
59 MODULE_DEVICE_TABLE(usb, gigaset_table);
60
61 /*
62  * Control requests (empty fields: 00)
63  *
64  *       RT|RQ|VALUE|INDEX|LEN  |DATA
65  * In:
66  *       C1 08             01
67  *            Get flags (1 byte). Bits: 0=dtr,1=rts,3-7:?
68  *       C1 0F             ll ll
69  *            Get device information/status (llll: 0x200 and 0x40 seen).
70  *            Real size: I only saw MIN(llll,0x64).
71  *            Contents: seems to be always the same...
72  *              offset 0x00: Length of this structure (0x64) (len: 1,2,3 bytes)
73  *              offset 0x3c: String (16 bit chars): "MCCI USB Serial V2.0"
74  *              rest:        ?
75  * Out:
76  *       41 11
77  *            Initialize/reset device ?
78  *       41 00 xx 00
79  *            ? (xx=00 or 01; 01 on start, 00 on close)
80  *       41 07 vv mm
81  *            Set/clear flags vv=value, mm=mask (see RQ 08)
82  *       41 12 xx
83  *            Used before the following configuration requests are issued
84  *            (with xx=0x0f). I've seen other values<0xf, though.
85  *       41 01 xx xx
86  *            Set baud rate. xxxx=ceil(0x384000/rate)=trunc(0x383fff/rate)+1.
87  *       41 03 ps bb
88  *            Set byte size and parity. p:  0x20=even,0x10=odd,0x00=no parity
89  *                                     [    0x30: m, 0x40: s           ]
90  *                                     [s:  0: 1 stop bit; 1: 1.5; 2: 2]
91  *                                      bb: bits/byte (seen 7 and 8)
92  *       41 13 -- -- -- -- 10 00 ww 00 00 00 xx 00 00 00 yy 00 00 00 zz 00 00 00
93  *            ??
94  *            Initialization: 01, 40, 00, 00
95  *            Open device:    00  40, 00, 00
96  *            yy and zz seem to be equal, either 0x00 or 0x0a
97  *            (ww,xx) pairs seen: (00,00), (00,40), (01,40), (09,80), (19,80)
98  *       41 19 -- -- -- -- 06 00 00 00 00 xx 11 13
99  *            Used after every "configuration sequence" (RQ 12, RQs 01/03/13).
100  *            xx is usually 0x00 but was 0x7e before starting data transfer
101  *            in unimodem mode. So, this might be an array of characters that need
102  *            special treatment ("commit all bufferd data"?), 11=^Q, 13=^S.
103  *
104  * Unimodem mode: use "modprobe ppp_async flag_time=0" as the device _needs_ two
105  * flags per packet.
106  */
107
108 static int gigaset_probe(struct usb_interface *interface,
109                          const struct usb_device_id *id);
110 static void gigaset_disconnect(struct usb_interface *interface);
111
112 static struct gigaset_driver *driver = NULL;
113 static struct cardstate *cardstate = NULL;
114
115 /* usb specific object needed to register this driver with the usb subsystem */
116 static struct usb_driver gigaset_usb_driver = {
117         .name =         GIGASET_MODULENAME,
118         .probe =        gigaset_probe,
119         .disconnect =   gigaset_disconnect,
120         .id_table =     gigaset_table,
121 };
122
123 struct usb_cardstate {
124         struct usb_device       *udev;          /* usb device pointer */
125         struct usb_interface    *interface;     /* interface for this device */
126         atomic_t                busy;           /* bulk output in progress */
127
128         /* Output buffer */
129         unsigned char           *bulk_out_buffer;       /* send buffer */
130         int                     bulk_out_size;          /* send buffer size */
131         __u8                    bulk_out_endpointAddr;  /* bulk out endpoint */
132         struct urb              *bulk_out_urb;          /* bulk out urb */
133
134         /* Input buffer */
135         int                     rcvbuf_size;            /* rcv buffer */
136         struct urb              *read_urb;              /* rcv buffer size */
137         __u8                    int_in_endpointAddr;    /* int in endpoint */
138
139         char                    bchars[6];              /* request 0x19 */
140 };
141
142 struct usb_bc_state {};
143
144 static inline unsigned tiocm_to_gigaset(unsigned state)
145 {
146         return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0);
147 }
148
149 #ifdef CONFIG_GIGASET_UNDOCREQ
150 /* WARNING: EXPERIMENTAL! */
151 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
152                                   unsigned new_state)
153 {
154         unsigned mask, val;
155         int r;
156
157         mask = tiocm_to_gigaset(old_state ^ new_state);
158         val = tiocm_to_gigaset(new_state);
159
160         dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask);
161         // don't use this in an interrupt/BH
162         r = usb_control_msg(cs->hw.usb->udev,
163                             usb_sndctrlpipe(cs->hw.usb->udev, 0), 7, 0x41,
164                             (val & 0xff) | ((mask & 0xff) << 8), 0,
165                             NULL, 0, 2000 /* timeout? */);
166         if (r < 0)
167                 return r;
168         //..
169         return 0;
170 }
171
172 static int set_value(struct cardstate *cs, u8 req, u16 val)
173 {
174         int r, r2;
175
176         dbg(DEBUG_USBREQ, "request %02x (%04x)", (unsigned)req, (unsigned)val);
177         r = usb_control_msg(cs->hw.usb->udev,
178                             usb_sndctrlpipe(cs->hw.usb->udev, 0), 0x12, 0x41,
179                             0xf /*?*/, 0,
180                             NULL, 0, 2000 /*?*/); /* no idea, what this does */
181         if (r < 0) {
182                 err("error %d on request 0x12", -r);
183                 return r;
184         }
185
186         r = usb_control_msg(cs->hw.usb->udev,
187                             usb_sndctrlpipe(cs->hw.usb->udev, 0), req, 0x41,
188                             val, 0,
189                             NULL, 0, 2000 /*?*/);
190         if (r < 0)
191                 err("error %d on request 0x%02x", -r, (unsigned)req);
192
193         r2 = usb_control_msg(cs->hw.usb->udev,
194                              usb_sndctrlpipe(cs->hw.usb->udev, 0), 0x19, 0x41,
195                              0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/);
196         if (r2 < 0)
197                 err("error %d on request 0x19", -r2);
198
199         return r < 0 ? r : (r2 < 0 ? r2 : 0);
200 }
201
202 /* WARNING: HIGHLY EXPERIMENTAL! */
203 // don't use this in an interrupt/BH
204 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
205 {
206         u16 val;
207         u32 rate;
208
209         cflag &= CBAUD;
210
211         switch (cflag) {
212         //FIXME more values?
213         case    B300: rate =     300; break;
214         case    B600: rate =     600; break;
215         case   B1200: rate =    1200; break;
216         case   B2400: rate =    2400; break;
217         case   B4800: rate =    4800; break;
218         case   B9600: rate =    9600; break;
219         case  B19200: rate =   19200; break;
220         case  B38400: rate =   38400; break;
221         case  B57600: rate =   57600; break;
222         case B115200: rate =  115200; break;
223         default:
224                 rate =  9600;
225                 err("unsupported baudrate request 0x%x,"
226                     " using default of B9600", cflag);
227         }
228
229         val = 0x383fff / rate + 1;
230
231         return set_value(cs, 1, val);
232 }
233
234 /* WARNING: HIGHLY EXPERIMENTAL! */
235 // don't use this in an interrupt/BH
236 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
237 {
238         u16 val = 0;
239
240         /* set the parity */
241         if (cflag & PARENB)
242                 val |= (cflag & PARODD) ? 0x10 : 0x20;
243
244         /* set the number of data bits */
245         switch (cflag & CSIZE) {
246         case CS5:
247                 val |= 5 << 8; break;
248         case CS6:
249                 val |= 6 << 8; break;
250         case CS7:
251                 val |= 7 << 8; break;
252         case CS8:
253                 val |= 8 << 8; break;
254         default:
255                 err("CSIZE was not CS5-CS8, using default of 8");
256                 val |= 8 << 8;
257                 break;
258         }
259
260         /* set the number of stop bits */
261         if (cflag & CSTOPB) {
262                 if ((cflag & CSIZE) == CS5)
263                         val |= 1; /* 1.5 stop bits */ //FIXME is this okay?
264                 else
265                         val |= 2; /* 2 stop bits */
266         }
267
268         return set_value(cs, 3, val);
269 }
270
271 #else
272 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
273                                   unsigned new_state)
274 {
275         return -EINVAL;
276 }
277
278 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
279 {
280         return -EINVAL;
281 }
282
283 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
284 {
285         return -EINVAL;
286 }
287 #endif
288
289
290  /*================================================================================================================*/
291 static int gigaset_init_bchannel(struct bc_state *bcs)
292 {
293         /* nothing to do for M10x */
294         gigaset_bchannel_up(bcs);
295         return 0;
296 }
297
298 static int gigaset_close_bchannel(struct bc_state *bcs)
299 {
300         /* nothing to do for M10x */
301         gigaset_bchannel_down(bcs);
302         return 0;
303 }
304
305 static int write_modem(struct cardstate *cs);
306 static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb);
307
308
309 /* Write tasklet handler: Continue sending current skb, or send command, or
310  * start sending an skb from the send queue.
311  */
312 static void gigaset_modem_fill(unsigned long data)
313 {
314         struct cardstate *cs = (struct cardstate *) data;
315         struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
316         struct cmdbuf_t *cb;
317         unsigned long flags;
318         int again;
319
320         dbg(DEBUG_OUTPUT, "modem_fill");
321
322         if (atomic_read(&cs->hw.usb->busy)) {
323                 dbg(DEBUG_OUTPUT, "modem_fill: busy");
324                 return;
325         }
326
327         do {
328                 again = 0;
329                 if (!bcs->tx_skb) { /* no skb is being sent */
330                         spin_lock_irqsave(&cs->cmdlock, flags);
331                         cb = cs->cmdbuf;
332                         spin_unlock_irqrestore(&cs->cmdlock, flags);
333                         if (cb) { /* commands to send? */
334                                 dbg(DEBUG_OUTPUT, "modem_fill: cb");
335                                 if (send_cb(cs, cb) < 0) {
336                                         dbg(DEBUG_OUTPUT,
337                                             "modem_fill: send_cb failed");
338                                         again = 1; /* no callback will be
339                                                       called! */
340                                 }
341                         } else { /* skbs to send? */
342                                 bcs->tx_skb = skb_dequeue(&bcs->squeue);
343                                 if (bcs->tx_skb)
344                                         dbg(DEBUG_INTR,
345                                             "Dequeued skb (Adr: %lx)!",
346                                             (unsigned long) bcs->tx_skb);
347                         }
348                 }
349
350                 if (bcs->tx_skb) {
351                         dbg(DEBUG_OUTPUT, "modem_fill: tx_skb");
352                         if (write_modem(cs) < 0) {
353                                 dbg(DEBUG_OUTPUT,
354                                     "modem_fill: write_modem failed");
355                                 // FIXME should we tell the LL?
356                                 again = 1; /* no callback will be called! */
357                         }
358                 }
359         } while (again);
360 }
361
362 /**
363  *      gigaset_read_int_callback
364  *
365  *      It is called if the data was received from the device.
366  */
367 static void gigaset_read_int_callback(struct urb *urb, struct pt_regs *regs)
368 {
369         int resubmit = 0;
370         int r;
371         struct cardstate *cs;
372         unsigned numbytes;
373         unsigned char *src;
374         struct inbuf_t *inbuf;
375
376         IFNULLRET(urb);
377         inbuf = (struct inbuf_t *) urb->context;
378         IFNULLRET(inbuf);
379         cs = inbuf->cs;
380         IFNULLGOTO(cs, exit);
381         IFNULLGOTO(cardstate, exit);
382
383         if (!atomic_read(&cs->connected)) {
384                 err("%s: disconnected", __func__);
385                 goto exit;
386         }
387
388         if (!urb->status) {
389                 numbytes = urb->actual_length;
390
391                 if (numbytes) {
392                         src = inbuf->rcvbuf;
393                         if (unlikely(*src))
394                                 warn("%s: There was no leading 0, but 0x%02x!",
395                                      __func__, (unsigned) *src);
396                         ++src; /* skip leading 0x00 */
397                         --numbytes;
398                         if (gigaset_fill_inbuf(inbuf, src, numbytes)) {
399                                 dbg(DEBUG_INTR, "%s-->BH", __func__);
400                                 gigaset_schedule_event(inbuf->cs);
401                         }
402                 } else
403                         dbg(DEBUG_INTR, "Received zero block length");
404                 resubmit = 1;
405         } else {
406                 /* The urb might have been killed. */
407                 dbg(DEBUG_ANY, "%s - nonzero read bulk status received: %d",
408                     __func__, urb->status);
409                 if (urb->status != -ENOENT) /* not killed */
410                         resubmit = 1;
411         }
412 exit:
413         if (resubmit) {
414                 r = usb_submit_urb(urb, SLAB_ATOMIC);
415                 if (r)
416                         err("error %d when resubmitting urb.", -r);
417         }
418 }
419
420
421 /* This callback routine is called when data was transmitted to the device. */
422 static void gigaset_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
423 {
424         struct cardstate *cs = (struct cardstate *) urb->context;
425
426         IFNULLRET(cs);
427 #ifdef CONFIG_GIGASET_DEBUG
428         if (!atomic_read(&cs->connected)) {
429                 err("%s:not connected", __func__);
430                 return;
431         }
432 #endif
433         if (urb->status)
434                 err("bulk transfer failed (status %d)", -urb->status);
435                 /* That's all we can do. Communication problems
436                    are handeled by timeouts or network protocols */
437
438         atomic_set(&cs->hw.usb->busy, 0);
439         tasklet_schedule(&cs->write_tasklet);
440 }
441
442 static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb)
443 {
444         struct cmdbuf_t *tcb;
445         unsigned long flags;
446         int count;
447         int status = -ENOENT; // FIXME
448         struct usb_cardstate *ucs = cs->hw.usb;
449
450         do {
451                 if (!cb->len) {
452                         tcb = cb;
453
454                         spin_lock_irqsave(&cs->cmdlock, flags);
455                         cs->cmdbytes -= cs->curlen;
456                         dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left",
457                             cs->curlen, cs->cmdbytes);
458                         cs->cmdbuf = cb = cb->next;
459                         if (cb) {
460                                 cb->prev = NULL;
461                                 cs->curlen = cb->len;
462                         } else {
463                                 cs->lastcmdbuf = NULL;
464                                 cs->curlen = 0;
465                         }
466                         spin_unlock_irqrestore(&cs->cmdlock, flags);
467
468                         if (tcb->wake_tasklet)
469                                 tasklet_schedule(tcb->wake_tasklet);
470                         kfree(tcb);
471                 }
472                 if (cb) {
473                         count = min(cb->len, ucs->bulk_out_size);
474                         usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
475                                           usb_sndbulkpipe(ucs->udev,
476                                              ucs->bulk_out_endpointAddr & 0x0f),
477                                           cb->buf + cb->offset, count,
478                                           gigaset_write_bulk_callback, cs);
479
480                         cb->offset += count;
481                         cb->len -= count;
482                         atomic_set(&ucs->busy, 1);
483                         dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count);
484
485                         status = usb_submit_urb(ucs->bulk_out_urb, SLAB_ATOMIC);
486                         if (status) {
487                                 atomic_set(&ucs->busy, 0);
488                                 err("could not submit urb (error %d).",
489                                     -status);
490                                 cb->len = 0; /* skip urb => remove cb+wakeup
491                                                 in next loop cycle */
492                         }
493                 }
494         } while (cb && status); /* next command on error */
495
496         return status;
497 }
498
499 /* Send command to device. */
500 static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
501                              int len, struct tasklet_struct *wake_tasklet)
502 {
503         struct cmdbuf_t *cb;
504         unsigned long flags;
505
506         gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
507                              DEBUG_TRANSCMD : DEBUG_LOCKCMD,
508                            "CMD Transmit", len, buf, 0);
509
510         if (!atomic_read(&cs->connected)) {
511                 err("%s: not connected", __func__);
512                 return -ENODEV;
513         }
514
515         if (len <= 0)
516                 return 0;
517
518         if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
519                 err("%s: out of memory", __func__);
520                 return -ENOMEM;
521         }
522
523         memcpy(cb->buf, buf, len);
524         cb->len = len;
525         cb->offset = 0;
526         cb->next = NULL;
527         cb->wake_tasklet = wake_tasklet;
528
529         spin_lock_irqsave(&cs->cmdlock, flags);
530         cb->prev = cs->lastcmdbuf;
531         if (cs->lastcmdbuf)
532                 cs->lastcmdbuf->next = cb;
533         else {
534                 cs->cmdbuf = cb;
535                 cs->curlen = len;
536         }
537         cs->cmdbytes += len;
538         cs->lastcmdbuf = cb;
539         spin_unlock_irqrestore(&cs->cmdlock, flags);
540
541         tasklet_schedule(&cs->write_tasklet);
542         return len;
543 }
544
545 static int gigaset_write_room(struct cardstate *cs)
546 {
547         unsigned long flags;
548         unsigned bytes;
549
550         spin_lock_irqsave(&cs->cmdlock, flags);
551         bytes = cs->cmdbytes;
552         spin_unlock_irqrestore(&cs->cmdlock, flags);
553
554         return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
555 }
556
557 static int gigaset_chars_in_buffer(struct cardstate *cs)
558 {
559         return cs->cmdbytes;
560 }
561
562 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
563 {
564 #ifdef CONFIG_GIGASET_UNDOCREQ
565         gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf, 0);
566         memcpy(cs->hw.usb->bchars, buf, 6);
567         return usb_control_msg(cs->hw.usb->udev,
568                                usb_sndctrlpipe(cs->hw.usb->udev, 0), 0x19, 0x41,
569                                0, 0, &buf, 6, 2000);
570 #else
571         return -EINVAL;
572 #endif
573 }
574
575 static int gigaset_freebcshw(struct bc_state *bcs)
576 {
577         if (!bcs->hw.usb)
578                 return 0;
579         //FIXME
580         kfree(bcs->hw.usb);
581         return 1;
582 }
583
584 /* Initialize the b-channel structure */
585 static int gigaset_initbcshw(struct bc_state *bcs)
586 {
587         bcs->hw.usb = kmalloc(sizeof(struct usb_bc_state), GFP_KERNEL);
588         if (!bcs->hw.usb)
589                 return 0;
590
591         return 1;
592 }
593
594 static void gigaset_reinitbcshw(struct bc_state *bcs)
595 {
596 }
597
598 static void gigaset_freecshw(struct cardstate *cs)
599 {
600         tasklet_kill(&cs->write_tasklet);
601         kfree(cs->hw.usb);
602 }
603
604 static int gigaset_initcshw(struct cardstate *cs)
605 {
606         struct usb_cardstate *ucs;
607
608         cs->hw.usb = ucs =
609                 kmalloc(sizeof(struct usb_cardstate), GFP_KERNEL);
610         if (!ucs)
611                 return 0;
612
613         ucs->bchars[0] = 0;
614         ucs->bchars[1] = 0;
615         ucs->bchars[2] = 0;
616         ucs->bchars[3] = 0;
617         ucs->bchars[4] = 0x11;
618         ucs->bchars[5] = 0x13;
619         ucs->bulk_out_buffer = NULL;
620         ucs->bulk_out_urb = NULL;
621         //ucs->urb_cmd_out = NULL;
622         ucs->read_urb = NULL;
623         tasklet_init(&cs->write_tasklet,
624                      &gigaset_modem_fill, (unsigned long) cs);
625
626         return 1;
627 }
628
629 /* Send data from current skb to the device. */
630 static int write_modem(struct cardstate *cs)
631 {
632         int ret;
633         int count;
634         struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
635         struct usb_cardstate *ucs = cs->hw.usb;
636
637         IFNULLRETVAL(bcs->tx_skb, -EINVAL);
638
639         dbg(DEBUG_WRITE, "len: %d...", bcs->tx_skb->len);
640
641         ret = -ENODEV;
642         IFNULLGOTO(ucs->bulk_out_buffer, error);
643         IFNULLGOTO(ucs->bulk_out_urb, error);
644         ret = 0;
645
646         if (!bcs->tx_skb->len) {
647                 dev_kfree_skb_any(bcs->tx_skb);
648                 bcs->tx_skb = NULL;
649                 return -EINVAL;
650         }
651
652         /* Copy data to bulk out buffer and  // FIXME copying not necessary
653          * transmit data
654          */
655         count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size);
656         memcpy(ucs->bulk_out_buffer, bcs->tx_skb->data, count);
657         skb_pull(bcs->tx_skb, count);
658
659         usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
660                           usb_sndbulkpipe(ucs->udev,
661                                           ucs->bulk_out_endpointAddr & 0x0f),
662                           ucs->bulk_out_buffer, count,
663                           gigaset_write_bulk_callback, cs);
664         atomic_set(&ucs->busy, 1);
665         dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count);
666
667         ret = usb_submit_urb(ucs->bulk_out_urb, SLAB_ATOMIC);
668         if (ret) {
669                 err("could not submit urb (error %d).", -ret);
670                 atomic_set(&ucs->busy, 0);
671         }
672         if (!bcs->tx_skb->len) {
673                 /* skb sent completely */
674                 gigaset_skb_sent(bcs, bcs->tx_skb); //FIXME also, when ret<0?
675
676                 dbg(DEBUG_INTR,
677                     "kfree skb (Adr: %lx)!", (unsigned long) bcs->tx_skb);
678                 dev_kfree_skb_any(bcs->tx_skb);
679                 bcs->tx_skb = NULL;
680         }
681
682         return ret;
683 error:
684         dev_kfree_skb_any(bcs->tx_skb);
685         bcs->tx_skb = NULL;
686         return ret;
687
688 }
689
690 static int gigaset_probe(struct usb_interface *interface,
691                          const struct usb_device_id *id)
692 {
693         int retval;
694         struct usb_device *udev = interface_to_usbdev(interface);
695         unsigned int ifnum;
696         struct usb_host_interface *hostif;
697         struct cardstate *cs = NULL;
698         struct usb_cardstate *ucs = NULL;
699         struct usb_endpoint_descriptor *endpoint;
700         int buffer_size;
701         int alt;
702
703         info("%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
704             __func__, le16_to_cpu(udev->descriptor.idVendor),
705             le16_to_cpu(udev->descriptor.idProduct));
706
707         retval = -ENODEV; //FIXME
708
709         /* See if the device offered us matches what we can accept */
710         if ((le16_to_cpu(udev->descriptor.idVendor  != USB_M105_VENDOR_ID)) ||
711             (le16_to_cpu(udev->descriptor.idProduct != USB_M105_PRODUCT_ID)))
712                 return -ENODEV;
713
714         /* this starts to become ascii art... */
715         hostif = interface->cur_altsetting;
716         alt = hostif->desc.bAlternateSetting;
717         ifnum = hostif->desc.bInterfaceNumber; // FIXME ?
718
719         if (alt != 0 || ifnum != 0) {
720                 warn("ifnum %d, alt %d", ifnum, alt);
721                 return -ENODEV;
722         }
723
724         /* Reject application specific intefaces
725          *
726          */
727         if (hostif->desc.bInterfaceClass != 255) {
728                 info("%s: Device matched, but iface_desc[%d]->bInterfaceClass==%d !",
729                        __func__, ifnum, hostif->desc.bInterfaceClass);
730                 return -ENODEV;
731         }
732
733         info("%s: Device matched ... !", __func__);
734
735         cs = gigaset_getunassignedcs(driver);
736         if (!cs) {
737                 warn("No free cardstate!");
738                 return -ENODEV;
739         }
740         ucs = cs->hw.usb;
741
742         endpoint = &hostif->endpoint[0].desc;
743
744         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
745         ucs->bulk_out_size = buffer_size;
746         ucs->bulk_out_endpointAddr = endpoint->bEndpointAddress;
747         ucs->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
748         if (!ucs->bulk_out_buffer) {
749                 err("Couldn't allocate bulk_out_buffer");
750                 retval = -ENOMEM;
751                 goto error;
752         }
753
754         ucs->bulk_out_urb = usb_alloc_urb(0, SLAB_KERNEL);
755         if (!ucs->bulk_out_urb) {
756                 err("Couldn't allocate bulk_out_buffer");
757                 retval = -ENOMEM;
758                 goto error;
759         }
760
761         endpoint = &hostif->endpoint[1].desc;
762
763         atomic_set(&ucs->busy, 0);
764         ucs->udev = udev;
765         ucs->interface = interface;
766
767         ucs->read_urb = usb_alloc_urb(0, SLAB_KERNEL);
768         if (!ucs->read_urb) {
769                 err("No free urbs available");
770                 retval = -ENOMEM;
771                 goto error;
772         }
773         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
774         ucs->rcvbuf_size = buffer_size;
775         ucs->int_in_endpointAddr = endpoint->bEndpointAddress;
776         cs->inbuf[0].rcvbuf = kmalloc(buffer_size, GFP_KERNEL);
777         if (!cs->inbuf[0].rcvbuf) {
778                 err("Couldn't allocate rcvbuf");
779                 retval = -ENOMEM;
780                 goto error;
781         }
782         /* Fill the interrupt urb and send it to the core */
783         usb_fill_int_urb(ucs->read_urb, udev,
784                          usb_rcvintpipe(udev,
785                                         endpoint->bEndpointAddress & 0x0f),
786                          cs->inbuf[0].rcvbuf, buffer_size,
787                          gigaset_read_int_callback,
788                          cs->inbuf + 0, endpoint->bInterval);
789
790         retval = usb_submit_urb(ucs->read_urb, SLAB_KERNEL);
791         if (retval) {
792                 err("Could not submit URB!");
793                 goto error;
794         }
795
796         /* tell common part that the device is ready */
797         if (startmode == SM_LOCKED)
798                 atomic_set(&cs->mstate, MS_LOCKED);
799         if (!gigaset_start(cs)) {
800                 tasklet_kill(&cs->write_tasklet);
801                 retval = -ENODEV; //FIXME
802                 goto error;
803         }
804
805         /* save address of controller structure */
806         usb_set_intfdata(interface, cs);
807
808         /* set up device sysfs */
809         gigaset_init_dev_sysfs(interface);
810         return 0;
811
812 error:
813         if (ucs->read_urb)
814                 usb_kill_urb(ucs->read_urb);
815         kfree(ucs->bulk_out_buffer);
816         if (ucs->bulk_out_urb != NULL)
817                 usb_free_urb(ucs->bulk_out_urb);
818         kfree(cs->inbuf[0].rcvbuf);
819         if (ucs->read_urb != NULL)
820                 usb_free_urb(ucs->read_urb);
821         ucs->read_urb = ucs->bulk_out_urb = NULL;
822         cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
823         gigaset_unassign(cs);
824         return retval;
825 }
826
827 /**
828  *      skel_disconnect
829  */
830 static void gigaset_disconnect(struct usb_interface *interface)
831 {
832         struct cardstate *cs;
833         struct usb_cardstate *ucs;
834
835         cs = usb_get_intfdata(interface);
836
837         /* clear device sysfs */
838         gigaset_free_dev_sysfs(interface);
839
840         usb_set_intfdata(interface, NULL);
841         ucs = cs->hw.usb;
842         usb_kill_urb(ucs->read_urb);
843         //info("GigaSet USB device #%d will be disconnected", minor);
844
845         gigaset_stop(cs);
846
847         tasklet_kill(&cs->write_tasklet);
848
849         usb_kill_urb(ucs->bulk_out_urb);  /* FIXME: only if active? */
850
851         kfree(ucs->bulk_out_buffer);
852         if (ucs->bulk_out_urb != NULL)
853                 usb_free_urb(ucs->bulk_out_urb);
854         kfree(cs->inbuf[0].rcvbuf);
855         if (ucs->read_urb != NULL)
856                 usb_free_urb(ucs->read_urb);
857         ucs->read_urb = ucs->bulk_out_urb = NULL;
858         cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
859
860         gigaset_unassign(cs);
861 }
862
863 static struct gigaset_ops ops = {
864         gigaset_write_cmd,
865         gigaset_write_room,
866         gigaset_chars_in_buffer,
867         gigaset_brkchars,
868         gigaset_init_bchannel,
869         gigaset_close_bchannel,
870         gigaset_initbcshw,
871         gigaset_freebcshw,
872         gigaset_reinitbcshw,
873         gigaset_initcshw,
874         gigaset_freecshw,
875         gigaset_set_modem_ctrl,
876         gigaset_baud_rate,
877         gigaset_set_line_ctrl,
878         gigaset_m10x_send_skb,
879         gigaset_m10x_input,
880 };
881
882 /**
883  *      usb_gigaset_init
884  * This function is called while kernel-module is loaded
885  */
886 static int __init usb_gigaset_init(void)
887 {
888         int result;
889
890         /* allocate memory for our driver state and intialize it */
891         if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
892                                        GIGASET_MODULENAME, GIGASET_DEVNAME,
893                                        GIGASET_DEVFSNAME, &ops,
894                                        THIS_MODULE)) == NULL)
895                 goto error;
896
897         /* allocate memory for our device state and intialize it */
898         cardstate = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
899         if (!cardstate)
900                 goto error;
901
902         /* register this driver with the USB subsystem */
903         result = usb_register(&gigaset_usb_driver);
904         if (result < 0) {
905                 err("usb_gigaset: usb_register failed (error %d)",
906                     -result);
907                 goto error;
908         }
909
910         info(DRIVER_AUTHOR);
911         info(DRIVER_DESC);
912         return 0;
913
914 error:  if (cardstate)
915                 gigaset_freecs(cardstate);
916         cardstate = NULL;
917         if (driver)
918                 gigaset_freedriver(driver);
919         driver = NULL;
920         return -1;
921 }
922
923
924 /**
925  *      usb_gigaset_exit
926  * This function is called while unloading the kernel-module
927  */
928 static void __exit usb_gigaset_exit(void)
929 {
930         gigaset_blockdriver(driver); /* => probe will fail
931                                       * => no gigaset_start any more
932                                       */
933
934         gigaset_shutdown(cardstate);
935         /* from now on, no isdn callback should be possible */
936
937         /* deregister this driver with the USB subsystem */
938         usb_deregister(&gigaset_usb_driver);
939         /* this will call the disconnect-callback */
940         /* from now on, no disconnect/probe callback should be running */
941
942         gigaset_freecs(cardstate);
943         cardstate = NULL;
944         gigaset_freedriver(driver);
945         driver = NULL;
946 }
947
948
949 module_init(usb_gigaset_init);
950 module_exit(usb_gigaset_exit);
951
952 MODULE_AUTHOR(DRIVER_AUTHOR);
953 MODULE_DESCRIPTION(DRIVER_DESC);
954
955 MODULE_LICENSE("GPL");