Merge branch 'x86-txt-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / isdn / gigaset / usb-gigaset.c
CommitLineData
07dc1f9f
HL
1/*
2 * USB driver for Gigaset 307x directly or using M105 Data.
3 *
70440cf2 4 * Copyright (c) 2001 by Stefan Eilers
07dc1f9f
HL
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 * =====================================================================
07dc1f9f
HL
16 */
17
18#include "gigaset.h"
07dc1f9f
HL
19#include <linux/usb.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22
23/* Version Information */
70440cf2 24#define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
07dc1f9f
HL
25#define DRIVER_DESC "USB Driver for Gigaset 307x using M105"
26
27/* Module parameters */
28
29static int startmode = SM_ISDN;
30static int cidmode = 1;
31
32module_param(startmode, int, S_IRUGO);
33module_param(cidmode, int, S_IRUGO);
34MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
35MODULE_PARM_DESC(cidmode, "Call-ID mode");
36
37#define GIGASET_MINORS 1
38#define GIGASET_MINOR 8
39#define GIGASET_MODULENAME "usb_gigaset"
07dc1f9f
HL
40#define GIGASET_DEVNAME "ttyGU"
41
2032e2c2 42#define IF_WRITEBUF 2000 /* arbitrary limit */
07dc1f9f
HL
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 */
2032e2c2 49static const struct usb_device_id gigaset_table[] = {
07dc1f9f
HL
50 { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
51 { } /* Terminating entry */
52};
53
54MODULE_DEVICE_TABLE(usb, gigaset_table);
55
07dc1f9f
HL
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
2032e2c2
TS
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.
07dc1f9f
HL
98 *
99 * Unimodem mode: use "modprobe ppp_async flag_time=0" as the device _needs_ two
100 * flags per packet.
101 */
102
c652cbd8 103/* functions called if a device of this driver is connected/disconnected */
07dc1f9f 104static int gigaset_probe(struct usb_interface *interface,
784d5858 105 const struct usb_device_id *id);
07dc1f9f
HL
106static void gigaset_disconnect(struct usb_interface *interface);
107
1ff0a529
TS
108/* functions called before/after suspend */
109static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
110static int gigaset_resume(struct usb_interface *intf);
111static int gigaset_pre_reset(struct usb_interface *intf);
112
2032e2c2 113static struct gigaset_driver *driver;
07dc1f9f
HL
114
115/* usb specific object needed to register this driver with the usb subsystem */
116static struct usb_driver gigaset_usb_driver = {
917f5085
TS
117 .name = GIGASET_MODULENAME,
118 .probe = gigaset_probe,
119 .disconnect = gigaset_disconnect,
120 .id_table = gigaset_table,
1ff0a529
TS
121 .suspend = gigaset_suspend,
122 .resume = gigaset_resume,
123 .reset_resume = gigaset_resume,
124 .pre_reset = gigaset_pre_reset,
125 .post_reset = gigaset_resume,
07dc1f9f
HL
126};
127
128struct usb_cardstate {
917f5085
TS
129 struct usb_device *udev; /* usb device pointer */
130 struct usb_interface *interface; /* interface for this device */
9d4bee2b 131 int busy; /* bulk output in progress */
917f5085
TS
132
133 /* Output buffer */
784d5858
TS
134 unsigned char *bulk_out_buffer;
135 int bulk_out_size;
136 __u8 bulk_out_endpointAddr;
137 struct urb *bulk_out_urb;
917f5085
TS
138
139 /* Input buffer */
2032e2c2 140 unsigned char *rcvbuf;
784d5858
TS
141 int rcvbuf_size;
142 struct urb *read_urb;
143 __u8 int_in_endpointAddr;
917f5085 144
784d5858 145 char bchars[6]; /* for request 0x19 */
07dc1f9f
HL
146};
147
07dc1f9f
HL
148static inline unsigned tiocm_to_gigaset(unsigned state)
149{
150 return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0);
151}
152
07dc1f9f 153static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
784d5858 154 unsigned new_state)
07dc1f9f 155{
784d5858 156 struct usb_device *udev = cs->hw.usb->udev;
07dc1f9f
HL
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
784d5858 163 gig_dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask);
784d5858
TS
164 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 7, 0x41,
165 (val & 0xff) | ((mask & 0xff) << 8), 0,
166 NULL, 0, 2000 /* timeout? */);
07dc1f9f
HL
167 if (r < 0)
168 return r;
07dc1f9f
HL
169 return 0;
170}
171
b88bd956
TS
172/*
173 * Set M105 configuration value
174 * using undocumented device commands reverse engineered from USB traces
175 * of the Siemens Windows driver
176 */
07dc1f9f
HL
177static int set_value(struct cardstate *cs, u8 req, u16 val)
178{
784d5858 179 struct usb_device *udev = cs->hw.usb->udev;
07dc1f9f
HL
180 int r, r2;
181
784d5858
TS
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 */
07dc1f9f 187 if (r < 0) {
784d5858 188 dev_err(&udev->dev, "error %d on request 0x12\n", -r);
07dc1f9f
HL
189 return r;
190 }
191
784d5858
TS
192 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), req, 0x41,
193 val, 0, NULL, 0, 2000 /*?*/);
07dc1f9f 194 if (r < 0)
784d5858
TS
195 dev_err(&udev->dev, "error %d on request 0x%02x\n",
196 -r, (unsigned)req);
07dc1f9f 197
784d5858
TS
198 r2 = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
199 0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/);
07dc1f9f 200 if (r2 < 0)
784d5858 201 dev_err(&udev->dev, "error %d on request 0x19\n", -r2);
07dc1f9f
HL
202
203 return r < 0 ? r : (r2 < 0 ? r2 : 0);
204}
205
b88bd956
TS
206/*
207 * set the baud rate on the internal serial adapter
208 * using the undocumented parameter setting command
209 */
07dc1f9f
HL
210static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
211{
212 u16 val;
213 u32 rate;
214
215 cflag &= CBAUD;
216
217 switch (cflag) {
07dc1f9f
HL
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;
784d5858
TS
230 dev_err(cs->dev, "unsupported baudrate request 0x%x,"
231 " using default of B9600\n", cflag);
07dc1f9f
HL
232 }
233
234 val = 0x383fff / rate + 1;
235
236 return set_value(cs, 1, val);
237}
238
b88bd956
TS
239/*
240 * set the line format on the internal serial adapter
241 * using the undocumented parameter setting command
242 */
07dc1f9f
HL
243static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
244{
245 u16 val = 0;
246
247 /* set the parity */
248 if (cflag & PARENB)
249 val |= (cflag & PARODD) ? 0x10 : 0x20;
250
251 /* set the number of data bits */
252 switch (cflag & CSIZE) {
253 case CS5:
254 val |= 5 << 8; break;
255 case CS6:
256 val |= 6 << 8; break;
257 case CS7:
258 val |= 7 << 8; break;
259 case CS8:
260 val |= 8 << 8; break;
261 default:
784d5858 262 dev_err(cs->dev, "CSIZE was not CS5-CS8, using default of 8\n");
07dc1f9f
HL
263 val |= 8 << 8;
264 break;
265 }
266
267 /* set the number of stop bits */
268 if (cflag & CSTOPB) {
269 if ((cflag & CSIZE) == CS5)
2032e2c2 270 val |= 1; /* 1.5 stop bits */
07dc1f9f
HL
271 else
272 val |= 2; /* 2 stop bits */
273 }
274
275 return set_value(cs, 3, val);
276}
277
07dc1f9f 278
2032e2c2 279/*============================================================================*/
07dc1f9f
HL
280static 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
287static 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
07dc1f9f
HL
294static int write_modem(struct cardstate *cs);
295static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb);
296
297
917f5085
TS
298/* Write tasklet handler: Continue sending current skb, or send command, or
299 * start sending an skb from the send queue.
07dc1f9f
HL
300 */
301static 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;
07dc1f9f
HL
306 int again;
307
784d5858 308 gig_dbg(DEBUG_OUTPUT, "modem_fill");
07dc1f9f 309
9d4bee2b 310 if (cs->hw.usb->busy) {
784d5858 311 gig_dbg(DEBUG_OUTPUT, "modem_fill: busy");
07dc1f9f
HL
312 return;
313 }
314
315 do {
316 again = 0;
317 if (!bcs->tx_skb) { /* no skb is being sent */
07dc1f9f 318 cb = cs->cmdbuf;
07dc1f9f 319 if (cb) { /* commands to send? */
784d5858 320 gig_dbg(DEBUG_OUTPUT, "modem_fill: cb");
07dc1f9f 321 if (send_cb(cs, cb) < 0) {
784d5858
TS
322 gig_dbg(DEBUG_OUTPUT,
323 "modem_fill: send_cb failed");
917f5085
TS
324 again = 1; /* no callback will be
325 called! */
07dc1f9f
HL
326 }
327 } else { /* skbs to send? */
328 bcs->tx_skb = skb_dequeue(&bcs->squeue);
329 if (bcs->tx_skb)
784d5858
TS
330 gig_dbg(DEBUG_INTR,
331 "Dequeued skb (Adr: %lx)!",
332 (unsigned long) bcs->tx_skb);
07dc1f9f
HL
333 }
334 }
335
336 if (bcs->tx_skb) {
784d5858 337 gig_dbg(DEBUG_OUTPUT, "modem_fill: tx_skb");
07dc1f9f 338 if (write_modem(cs) < 0) {
784d5858
TS
339 gig_dbg(DEBUG_OUTPUT,
340 "modem_fill: write_modem failed");
07dc1f9f
HL
341 again = 1; /* no callback will be called! */
342 }
343 }
344 } while (again);
345}
346
b88bd956
TS
347/*
348 * Interrupt Input URB completion routine
07dc1f9f 349 */
7d12e780 350static void gigaset_read_int_callback(struct urb *urb)
07dc1f9f 351{
2032e2c2
TS
352 struct cardstate *cs = urb->context;
353 struct inbuf_t *inbuf = cs->inbuf;
dbd98231 354 int status = urb->status;
07dc1f9f 355 int r;
07dc1f9f
HL
356 unsigned numbytes;
357 unsigned char *src;
69049cc8 358 unsigned long flags;
07dc1f9f 359
dbd98231 360 if (!status) {
07dc1f9f
HL
361 numbytes = urb->actual_length;
362
363 if (numbytes) {
2032e2c2 364 src = cs->hw.usb->rcvbuf;
07dc1f9f 365 if (unlikely(*src))
784d5858
TS
366 dev_warn(cs->dev,
367 "%s: There was no leading 0, but 0x%02x!\n",
368 __func__, (unsigned) *src);
07dc1f9f
HL
369 ++src; /* skip leading 0x00 */
370 --numbytes;
371 if (gigaset_fill_inbuf(inbuf, src, numbytes)) {
784d5858 372 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
07dc1f9f
HL
373 gigaset_schedule_event(inbuf->cs);
374 }
375 } else
784d5858 376 gig_dbg(DEBUG_INTR, "Received zero block length");
07dc1f9f
HL
377 } else {
378 /* The urb might have been killed. */
c652cbd8 379 gig_dbg(DEBUG_ANY, "%s - nonzero status received: %d",
dbd98231 380 __func__, status);
c652cbd8
TS
381 if (status == -ENOENT || status == -ESHUTDOWN)
382 /* killed or endpoint shutdown: don't resubmit */
383 return;
07dc1f9f 384 }
784d5858 385
c652cbd8
TS
386 /* resubmit URB */
387 spin_lock_irqsave(&cs->lock, flags);
388 if (!cs->connected) {
69049cc8 389 spin_unlock_irqrestore(&cs->lock, flags);
c8770dca 390 pr_err("%s: disconnected\n", __func__);
c652cbd8 391 return;
07dc1f9f 392 }
c652cbd8
TS
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);
07dc1f9f
HL
397}
398
399
917f5085 400/* This callback routine is called when data was transmitted to the device. */
7d12e780 401static void gigaset_write_bulk_callback(struct urb *urb)
07dc1f9f 402{
d48c7784 403 struct cardstate *cs = urb->context;
dbd98231 404 int status = urb->status;
69049cc8 405 unsigned long flags;
07dc1f9f 406
c652cbd8
TS
407 switch (status) {
408 case 0: /* normal completion */
409 break;
410 case -ENOENT: /* killed */
411 gig_dbg(DEBUG_ANY, "%s: killed", __func__);
9d4bee2b 412 cs->hw.usb->busy = 0;
c652cbd8
TS
413 return;
414 default:
784d5858 415 dev_err(cs->dev, "bulk transfer failed (status %d)\n",
dbd98231 416 -status);
917f5085 417 /* That's all we can do. Communication problems
784d5858 418 are handled by timeouts or network protocols. */
c652cbd8 419 }
07dc1f9f 420
69049cc8
TS
421 spin_lock_irqsave(&cs->lock, flags);
422 if (!cs->connected) {
c8770dca 423 pr_err("%s: disconnected\n", __func__);
69049cc8 424 } else {
9d4bee2b 425 cs->hw.usb->busy = 0;
69049cc8
TS
426 tasklet_schedule(&cs->write_tasklet);
427 }
428 spin_unlock_irqrestore(&cs->lock, flags);
07dc1f9f
HL
429}
430
431static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb)
432{
433 struct cmdbuf_t *tcb;
434 unsigned long flags;
435 int count;
2032e2c2 436 int status = -ENOENT;
07dc1f9f
HL
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;
784d5858
TS
445 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left",
446 cs->curlen, cs->cmdbytes);
07dc1f9f
HL
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);
69049cc8
TS
463 gig_dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count);
464
07dc1f9f 465 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
784d5858
TS
466 usb_sndbulkpipe(ucs->udev,
467 ucs->bulk_out_endpointAddr & 0x0f),
468 cb->buf + cb->offset, count,
469 gigaset_write_bulk_callback, cs);
07dc1f9f
HL
470
471 cb->offset += count;
472 cb->len -= count;
9d4bee2b 473 ucs->busy = 1;
07dc1f9f 474
69049cc8 475 spin_lock_irqsave(&cs->lock, flags);
2032e2c2
TS
476 status = cs->connected ?
477 usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC) :
478 -ENODEV;
69049cc8
TS
479 spin_unlock_irqrestore(&cs->lock, flags);
480
07dc1f9f 481 if (status) {
9d4bee2b 482 ucs->busy = 0;
5002779d
TS
483 dev_err(cs->dev,
484 "could not submit urb (error %d)\n",
485 -status);
917f5085
TS
486 cb->len = 0; /* skip urb => remove cb+wakeup
487 in next loop cycle */
07dc1f9f
HL
488 }
489 }
917f5085 490 } while (cb && status); /* next command on error */
07dc1f9f
HL
491
492 return status;
493}
494
917f5085 495/* Send command to device. */
07dc1f9f 496static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
784d5858 497 int len, struct tasklet_struct *wake_tasklet)
07dc1f9f
HL
498{
499 struct cmdbuf_t *cb;
500 unsigned long flags;
501
9d4bee2b 502 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
784d5858 503 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
01371500 504 "CMD Transmit", len, buf);
07dc1f9f 505
07dc1f9f
HL
506 if (len <= 0)
507 return 0;
2032e2c2
TS
508 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
509 if (!cb) {
784d5858 510 dev_err(cs->dev, "%s: out of memory\n", __func__);
07dc1f9f
HL
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
69049cc8
TS
532 spin_lock_irqsave(&cs->lock, flags);
533 if (cs->connected)
534 tasklet_schedule(&cs->write_tasklet);
535 spin_unlock_irqrestore(&cs->lock, flags);
07dc1f9f
HL
536 return len;
537}
538
539static int gigaset_write_room(struct cardstate *cs)
540{
07dc1f9f
HL
541 unsigned bytes;
542
07dc1f9f 543 bytes = cs->cmdbytes;
07dc1f9f
HL
544 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
545}
546
547static int gigaset_chars_in_buffer(struct cardstate *cs)
548{
549 return cs->cmdbytes;
550}
551
b88bd956
TS
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 */
07dc1f9f
HL
557static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
558{
784d5858
TS
559 struct usb_device *udev = cs->hw.usb->udev;
560
01371500 561 gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf);
07dc1f9f 562 memcpy(cs->hw.usb->bchars, buf, 6);
784d5858
TS
563 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
564 0, 0, &buf, 6, 2000);
07dc1f9f
HL
565}
566
567static int gigaset_freebcshw(struct bc_state *bcs)
568{
21d36495 569 /* unused */
07dc1f9f
HL
570 return 1;
571}
572
573/* Initialize the b-channel structure */
574static int gigaset_initbcshw(struct bc_state *bcs)
575{
21d36495
TS
576 /* unused */
577 bcs->hw.usb = NULL;
07dc1f9f
HL
578 return 1;
579}
580
581static void gigaset_reinitbcshw(struct bc_state *bcs)
582{
21d36495 583 /* nothing to do for M10x */
07dc1f9f
HL
584}
585
586static void gigaset_freecshw(struct cardstate *cs)
587{
07dc1f9f
HL
588 tasklet_kill(&cs->write_tasklet);
589 kfree(cs->hw.usb);
590}
591
592static 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);
c8770dca
TS
598 if (!ucs) {
599 pr_err("out of memory\n");
07dc1f9f 600 return 0;
c8770dca 601 }
07dc1f9f
HL
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;
07dc1f9f
HL
611 ucs->read_urb = NULL;
612 tasklet_init(&cs->write_tasklet,
5452fee2 613 gigaset_modem_fill, (unsigned long) cs);
07dc1f9f
HL
614
615 return 1;
616}
617
917f5085 618/* Send data from current skb to the device. */
07dc1f9f
HL
619static int write_modem(struct cardstate *cs)
620{
d48c7784 621 int ret = 0;
07dc1f9f
HL
622 int count;
623 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
624 struct usb_cardstate *ucs = cs->hw.usb;
69049cc8 625 unsigned long flags;
07dc1f9f 626
1528b18f 627 gig_dbg(DEBUG_OUTPUT, "len: %d...", bcs->tx_skb->len);
07dc1f9f 628
07dc1f9f
HL
629 if (!bcs->tx_skb->len) {
630 dev_kfree_skb_any(bcs->tx_skb);
631 bcs->tx_skb = NULL;
632 return -EINVAL;
633 }
634
2032e2c2 635 /* Copy data to bulk out buffer and transmit data */
07dc1f9f 636 count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size);
d626f62b 637 skb_copy_from_linear_data(bcs->tx_skb, ucs->bulk_out_buffer, count);
07dc1f9f 638 skb_pull(bcs->tx_skb, count);
9d4bee2b 639 ucs->busy = 1;
784d5858 640 gig_dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count);
07dc1f9f 641
69049cc8
TS
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,
2032e2c2
TS
646 ucs->bulk_out_endpointAddr &
647 0x0f),
69049cc8
TS
648 ucs->bulk_out_buffer, count,
649 gigaset_write_bulk_callback, cs);
54e6ecb2 650 ret = usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC);
69049cc8
TS
651 } else {
652 ret = -ENODEV;
653 }
654 spin_unlock_irqrestore(&cs->lock, flags);
655
07dc1f9f 656 if (ret) {
5002779d 657 dev_err(cs->dev, "could not submit urb (error %d)\n", -ret);
9d4bee2b 658 ucs->busy = 0;
07dc1f9f 659 }
69049cc8 660
07dc1f9f
HL
661 if (!bcs->tx_skb->len) {
662 /* skb sent completely */
2032e2c2 663 gigaset_skb_sent(bcs, bcs->tx_skb);
07dc1f9f 664
784d5858
TS
665 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
666 (unsigned long) bcs->tx_skb);
07dc1f9f
HL
667 dev_kfree_skb_any(bcs->tx_skb);
668 bcs->tx_skb = NULL;
669 }
670
671 return ret;
07dc1f9f
HL
672}
673
674static int gigaset_probe(struct usb_interface *interface,
784d5858 675 const struct usb_device_id *id)
07dc1f9f
HL
676{
677 int retval;
678 struct usb_device *udev = interface_to_usbdev(interface);
c652cbd8 679 struct usb_host_interface *hostif = interface->cur_altsetting;
07dc1f9f
HL
680 struct cardstate *cs = NULL;
681 struct usb_cardstate *ucs = NULL;
07dc1f9f 682 struct usb_endpoint_descriptor *endpoint;
07dc1f9f 683 int buffer_size;
07dc1f9f 684
c652cbd8 685 gig_dbg(DEBUG_ANY, "%s: Check if device matches ...", __func__);
07dc1f9f
HL
686
687 /* See if the device offered us matches what we can accept */
bfe2e934 688 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_M105_VENDOR_ID) ||
c652cbd8
TS
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));
07dc1f9f 693 return -ENODEV;
c652cbd8
TS
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);
07dc1f9f
HL
703 return -ENODEV;
704 }
07dc1f9f 705 if (hostif->desc.bInterfaceClass != 255) {
c652cbd8
TS
706 dev_notice(&udev->dev, "unsupported interface class %d - skip",
707 hostif->desc.bInterfaceClass);
07dc1f9f
HL
708 return -ENODEV;
709 }
710
784d5858 711 dev_info(&udev->dev, "%s: Device matched ... !\n", __func__);
07dc1f9f 712
e468c048
TS
713 /* allocate memory for our device state and intialize it */
714 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
715 if (!cs)
07dc1f9f 716 return -ENODEV;
07dc1f9f
HL
717 ucs = cs->hw.usb;
718
784d5858
TS
719 /* save off device structure ptrs for later use */
720 usb_get_dev(udev);
721 ucs->udev = udev;
722 ucs->interface = interface;
b1d47464
TS
723 cs->dev = &interface->dev;
724
725 /* save address of controller structure */
b88bd956 726 usb_set_intfdata(interface, cs);
784d5858 727
07dc1f9f
HL
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) {
784d5858 735 dev_err(cs->dev, "Couldn't allocate bulk_out_buffer\n");
07dc1f9f
HL
736 retval = -ENOMEM;
737 goto error;
738 }
739
e94b1766 740 ucs->bulk_out_urb = usb_alloc_urb(0, GFP_KERNEL);
07dc1f9f 741 if (!ucs->bulk_out_urb) {
784d5858 742 dev_err(cs->dev, "Couldn't allocate bulk_out_urb\n");
07dc1f9f
HL
743 retval = -ENOMEM;
744 goto error;
745 }
746
747 endpoint = &hostif->endpoint[1].desc;
748
9d4bee2b 749 ucs->busy = 0;
07dc1f9f 750
e94b1766 751 ucs->read_urb = usb_alloc_urb(0, GFP_KERNEL);
07dc1f9f 752 if (!ucs->read_urb) {
784d5858 753 dev_err(cs->dev, "No free urbs available\n");
07dc1f9f
HL
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;
2032e2c2
TS
760 ucs->rcvbuf = kmalloc(buffer_size, GFP_KERNEL);
761 if (!ucs->rcvbuf) {
784d5858 762 dev_err(cs->dev, "Couldn't allocate rcvbuf\n");
07dc1f9f
HL
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,
784d5858
TS
768 usb_rcvintpipe(udev,
769 endpoint->bEndpointAddress & 0x0f),
2032e2c2 770 ucs->rcvbuf, buffer_size,
784d5858 771 gigaset_read_int_callback,
2032e2c2 772 cs, endpoint->bInterval);
07dc1f9f 773
e94b1766 774 retval = usb_submit_urb(ucs->read_urb, GFP_KERNEL);
07dc1f9f 775 if (retval) {
784d5858 776 dev_err(cs->dev, "Could not submit URB (error %d)\n", -retval);
07dc1f9f
HL
777 goto error;
778 }
779
780 /* tell common part that the device is ready */
781 if (startmode == SM_LOCKED)
9d4bee2b 782 cs->mstate = MS_LOCKED;
b1d47464 783
07dc1f9f
HL
784 if (!gigaset_start(cs)) {
785 tasklet_kill(&cs->write_tasklet);
2032e2c2 786 retval = -ENODEV;
07dc1f9f
HL
787 goto error;
788 }
07dc1f9f
HL
789 return 0;
790
791error:
ead54fcd 792 usb_kill_urb(ucs->read_urb);
07dc1f9f 793 kfree(ucs->bulk_out_buffer);
ead54fcd 794 usb_free_urb(ucs->bulk_out_urb);
2032e2c2 795 kfree(ucs->rcvbuf);
ead54fcd 796 usb_free_urb(ucs->read_urb);
b1d47464 797 usb_set_intfdata(interface, NULL);
07dc1f9f 798 ucs->read_urb = ucs->bulk_out_urb = NULL;
2032e2c2 799 ucs->rcvbuf = ucs->bulk_out_buffer = NULL;
784d5858
TS
800 usb_put_dev(ucs->udev);
801 ucs->udev = NULL;
802 ucs->interface = NULL;
e468c048 803 gigaset_freecs(cs);
07dc1f9f
HL
804 return retval;
805}
806
07dc1f9f
HL
807static void gigaset_disconnect(struct usb_interface *interface)
808{
809 struct cardstate *cs;
810 struct usb_cardstate *ucs;
811
812 cs = usb_get_intfdata(interface);
07dc1f9f 813 ucs = cs->hw.usb;
c652cbd8
TS
814
815 dev_info(cs->dev, "disconnecting Gigaset USB adapter\n");
816
07dc1f9f 817 usb_kill_urb(ucs->read_urb);
07dc1f9f
HL
818
819 gigaset_stop(cs);
820
b1d47464 821 usb_set_intfdata(interface, NULL);
07dc1f9f
HL
822 tasklet_kill(&cs->write_tasklet);
823
c652cbd8 824 usb_kill_urb(ucs->bulk_out_urb);
07dc1f9f
HL
825
826 kfree(ucs->bulk_out_buffer);
ead54fcd 827 usb_free_urb(ucs->bulk_out_urb);
2032e2c2 828 kfree(ucs->rcvbuf);
ead54fcd 829 usb_free_urb(ucs->read_urb);
917f5085 830 ucs->read_urb = ucs->bulk_out_urb = NULL;
2032e2c2 831 ucs->rcvbuf = ucs->bulk_out_buffer = NULL;
07dc1f9f 832
b1d47464
TS
833 usb_put_dev(ucs->udev);
834 ucs->interface = NULL;
835 ucs->udev = NULL;
836 cs->dev = NULL;
e468c048 837 gigaset_freecs(cs);
07dc1f9f
HL
838}
839
1ff0a529
TS
840/* gigaset_suspend
841 * This function is called before the USB connection is suspended or reset.
842 */
843static 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 */
860static 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 */
880static int gigaset_pre_reset(struct usb_interface *intf)
881{
882 /* same as suspend */
883 return gigaset_suspend(intf, PMSG_ON);
884}
885
35dc8457 886static const struct gigaset_ops ops = {
07dc1f9f
HL
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
b88bd956 905/*
07dc1f9f
HL
906 * This function is called while kernel-module is loaded
907 */
908static int __init usb_gigaset_init(void)
909{
910 int result;
911
912 /* allocate memory for our driver state and intialize it */
2032e2c2
TS
913 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
914 GIGASET_MODULENAME, GIGASET_DEVNAME,
915 &ops, THIS_MODULE);
916 if (driver == NULL)
07dc1f9f
HL
917 goto error;
918
07dc1f9f
HL
919 /* register this driver with the USB subsystem */
920 result = usb_register(&gigaset_usb_driver);
921 if (result < 0) {
c8770dca 922 pr_err("error %d registering USB driver\n", -result);
07dc1f9f
HL
923 goto error;
924 }
925
c8770dca 926 pr_info(DRIVER_DESC "\n");
07dc1f9f
HL
927 return 0;
928
e468c048 929error:
07dc1f9f
HL
930 if (driver)
931 gigaset_freedriver(driver);
932 driver = NULL;
933 return -1;
934}
935
b88bd956 936/*
07dc1f9f
HL
937 * This function is called while unloading the kernel-module
938 */
939static void __exit usb_gigaset_exit(void)
940{
e468c048
TS
941 int i;
942
07dc1f9f 943 gigaset_blockdriver(driver); /* => probe will fail
784d5858
TS
944 * => no gigaset_start any more
945 */
07dc1f9f 946
e468c048
TS
947 /* stop all connected devices */
948 for (i = 0; i < driver->minors; i++)
949 gigaset_shutdown(driver->cs + i);
950
07dc1f9f
HL
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
07dc1f9f
HL
958 gigaset_freedriver(driver);
959 driver = NULL;
960}
961
962
963module_init(usb_gigaset_init);
964module_exit(usb_gigaset_exit);
965
966MODULE_AUTHOR(DRIVER_AUTHOR);
967MODULE_DESCRIPTION(DRIVER_DESC);
968
969MODULE_LICENSE("GPL");