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