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