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