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