aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHansjoerg Lipp <hjlipp@web.de>2006-03-26 04:38:34 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-03-26 11:57:05 -0500
commitcf7776dc05b89885a7ec6beff8409d48b077a3ac (patch)
tree78e825e65578526c3400e810759ba676d7014011
parent982da5a196a0859eedc5ca1ff88feaf1bc6d6e4e (diff)
[PATCH] isdn4linux: Siemens Gigaset drivers - direct USB connection
And: Tilman Schmidt <tilman@imap.cc> This patch adds the main source file of the connection-specific module "bas_gigaset", the hardware driver for Gigaset base stations connected directly to the computer via USB. It contains the code for handling probe/disconnect, AT command/response transmission, and call setup and termination. Signed-off-by: Hansjoerg Lipp <hjlipp@web.de> Signed-off-by: Tilman Schmidt <tilman@imap.cc> Cc: Karsten Keil <kkeil@suse.de> Cc: Greg KH <greg@kroah.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r--drivers/isdn/gigaset/bas-gigaset.c2365
1 files changed, 2365 insertions, 0 deletions
diff --git a/drivers/isdn/gigaset/bas-gigaset.c b/drivers/isdn/gigaset/bas-gigaset.c
new file mode 100644
index 000000000000..31f0f07832bc
--- /dev/null
+++ b/drivers/isdn/gigaset/bas-gigaset.c
@@ -0,0 +1,2365 @@
1/*
2 * USB driver for Gigaset 307x base via direct USB connection.
3 *
4 * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
5 * Tilman Schmidt <tilman@imap.cc>,
6 * Stefan Eilers <Eilers.Stefan@epost.de>.
7 *
8 * Based on usb-gigaset.c.
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 * ToDo: ...
17 * =====================================================================
18 * Version: $Id: bas-gigaset.c,v 1.52.4.19 2006/02/04 18:28:16 hjlipp Exp $
19 * =====================================================================
20 */
21
22#include "gigaset.h"
23
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/timer.h>
28#include <linux/usb.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31
32/* Version Information */
33#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers <Eilers.Stefan@epost.de>"
34#define DRIVER_DESC "USB Driver for Gigaset 307x"
35
36
37/* Module parameters */
38
39static int startmode = SM_ISDN;
40static int cidmode = 1;
41
42module_param(startmode, int, S_IRUGO);
43module_param(cidmode, int, S_IRUGO);
44MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
45MODULE_PARM_DESC(cidmode, "Call-ID mode");
46
47#define GIGASET_MINORS 1
48#define GIGASET_MINOR 16
49#define GIGASET_MODULENAME "bas_gigaset"
50#define GIGASET_DEVFSNAME "gig/bas/"
51#define GIGASET_DEVNAME "ttyGB"
52
53#define IF_WRITEBUF 256 //FIXME
54
55/* Values for the Gigaset 307x */
56#define USB_GIGA_VENDOR_ID 0x0681
57#define USB_GIGA_PRODUCT_ID 0x0001
58#define USB_4175_PRODUCT_ID 0x0002
59#define USB_SX303_PRODUCT_ID 0x0021
60#define USB_SX353_PRODUCT_ID 0x0022
61
62/* table of devices that work with this driver */
63static struct usb_device_id gigaset_table [] = {
64 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_GIGA_PRODUCT_ID) },
65 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_4175_PRODUCT_ID) },
66 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
67 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
68 { } /* Terminating entry */
69};
70
71MODULE_DEVICE_TABLE(usb, gigaset_table);
72
73/* Get a minor range for your devices from the usb maintainer */
74#define USB_SKEL_MINOR_BASE 200
75
76/*======================= local function prototypes =============================*/
77
78/* This function is called if a new device is connected to the USB port. It
79 * checks whether this new device belongs to this driver.
80 */
81static int gigaset_probe(struct usb_interface *interface,
82 const struct usb_device_id *id);
83
84/* Function will be called if the device is unplugged */
85static void gigaset_disconnect(struct usb_interface *interface);
86
87
88/*==============================================================================*/
89
90struct bas_cardstate {
91 struct usb_device *udev; /* USB device pointer */
92 struct usb_interface *interface; /* interface for this device */
93 unsigned char minor; /* starting minor number */
94
95 struct urb *urb_ctrl; /* control pipe default URB */
96 struct usb_ctrlrequest dr_ctrl;
97 struct timer_list timer_ctrl; /* control request timeout */
98
99 struct timer_list timer_atrdy; /* AT command ready timeout */
100 struct urb *urb_cmd_out; /* for sending AT commands */
101 struct usb_ctrlrequest dr_cmd_out;
102 int retry_cmd_out;
103
104 struct urb *urb_cmd_in; /* for receiving AT replies */
105 struct usb_ctrlrequest dr_cmd_in;
106 struct timer_list timer_cmd_in; /* receive request timeout */
107 unsigned char *rcvbuf; /* AT reply receive buffer */
108
109 struct urb *urb_int_in; /* URB for interrupt pipe */
110 unsigned char int_in_buf[3];
111
112 spinlock_t lock; /* locks all following */
113 atomic_t basstate; /* bitmap (BS_*) */
114 int pending; /* uncompleted base request */
115 int rcvbuf_size; /* size of AT receive buffer */
116 /* 0: no receive in progress */
117 int retry_cmd_in; /* receive req retry count */
118};
119
120/* status of direct USB connection to 307x base (bits in basstate) */
121#define BS_ATOPEN 0x001
122#define BS_B1OPEN 0x002
123#define BS_B2OPEN 0x004
124#define BS_ATREADY 0x008
125#define BS_INIT 0x010
126#define BS_ATTIMER 0x020
127
128
129static struct gigaset_driver *driver = NULL;
130static struct cardstate *cardstate = NULL;
131
132/* usb specific object needed to register this driver with the usb subsystem */
133static struct usb_driver gigaset_usb_driver = {
134 .name = GIGASET_MODULENAME,
135 .probe = gigaset_probe,
136 .disconnect = gigaset_disconnect,
137 .id_table = gigaset_table,
138};
139
140/* get message text for USB status code
141 */
142static char *get_usb_statmsg(int status)
143{
144 static char unkmsg[28];
145
146 switch (status) {
147 case 0:
148 return "success";
149 case -ENOENT:
150 return "canceled";
151 case -ECONNRESET:
152 return "canceled (async)";
153 case -EINPROGRESS:
154 return "pending";
155 case -EPROTO:
156 return "bit stuffing or unknown USB error";
157 case -EILSEQ:
158 return "Illegal byte sequence (CRC mismatch)";
159 case -EPIPE:
160 return "babble detect or endpoint stalled";
161 case -ENOSR:
162 return "buffer error";
163 case -ETIMEDOUT:
164 return "timed out";
165 case -ENODEV:
166 return "device not present";
167 case -EREMOTEIO:
168 return "short packet detected";
169 case -EXDEV:
170 return "partial isochronous transfer";
171 case -EINVAL:
172 return "invalid argument";
173 case -ENXIO:
174 return "URB already queued";
175 case -EAGAIN:
176 return "isochronous start frame too early or too much scheduled";
177 case -EFBIG:
178 return "too many isochronous frames requested";
179 case -EMSGSIZE:
180 return "endpoint message size zero";
181 case -ESHUTDOWN:
182 return "endpoint shutdown";
183 case -EBUSY:
184 return "another request pending";
185 default:
186 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", status);
187 return unkmsg;
188 }
189}
190
191/* usb_pipetype_str
192 * retrieve string representation of USB pipe type
193 */
194static inline char *usb_pipetype_str(int pipe)
195{
196 if (usb_pipeisoc(pipe))
197 return "Isoc";
198 if (usb_pipeint(pipe))
199 return "Int";
200 if (usb_pipecontrol(pipe))
201 return "Ctrl";
202 if (usb_pipebulk(pipe))
203 return "Bulk";
204 return "?";
205}
206
207/* dump_urb
208 * write content of URB to syslog for debugging
209 */
210static inline void dump_urb(enum debuglevel level, const char *tag,
211 struct urb *urb)
212{
213#ifdef CONFIG_GIGASET_DEBUG
214 int i;
215 IFNULLRET(tag);
216 dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
217 if (urb) {
218 dbg(level,
219 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
220 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
221 (unsigned long) urb->dev,
222 usb_pipetype_str(urb->pipe),
223 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
224 usb_pipein(urb->pipe) ? "in" : "out",
225 urb->status, (unsigned long) urb->hcpriv,
226 urb->transfer_flags);
227 dbg(level,
228 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
229 "bandwidth=%d, setup_packet=0x%08lx,",
230 (unsigned long) urb->transfer_buffer,
231 urb->transfer_buffer_length, urb->actual_length,
232 urb->bandwidth, (unsigned long) urb->setup_packet);
233 dbg(level,
234 " start_frame=%d, number_of_packets=%d, interval=%d, "
235 "error_count=%d,",
236 urb->start_frame, urb->number_of_packets, urb->interval,
237 urb->error_count);
238 dbg(level,
239 " context=0x%08lx, complete=0x%08lx, iso_frame_desc[]={",
240 (unsigned long) urb->context,
241 (unsigned long) urb->complete);
242 for (i = 0; i < urb->number_of_packets; i++) {
243 struct usb_iso_packet_descriptor *pifd = &urb->iso_frame_desc[i];
244 dbg(level,
245 " {offset=%u, length=%u, actual_length=%u, "
246 "status=%u}",
247 pifd->offset, pifd->length, pifd->actual_length,
248 pifd->status);
249 }
250 }
251 dbg(level, "}}");
252#endif
253}
254
255/* read/set modem control bits etc. (m10x only) */
256static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
257 unsigned new_state)
258{
259 return -EINVAL;
260}
261
262static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
263{
264 return -EINVAL;
265}
266
267static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
268{
269 return -EINVAL;
270}
271
272/* error_hangup
273 * hang up any existing connection because of an unrecoverable error
274 * This function may be called from any context and takes care of scheduling
275 * the necessary actions for execution outside of interrupt context.
276 * argument:
277 * B channel control structure
278 */
279static inline void error_hangup(struct bc_state *bcs)
280{
281 struct cardstate *cs = bcs->cs;
282
283 dbg(DEBUG_ANY,
284 "%s: scheduling HUP for channel %d", __func__, bcs->channel);
285
286 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
287 //FIXME what should we do?
288 return;
289 }
290
291 gigaset_schedule_event(cs);
292}
293
294/* error_reset
295 * reset Gigaset device because of an unrecoverable error
296 * This function may be called from any context and takes care of scheduling
297 * the necessary actions for execution outside of interrupt context.
298 * argument:
299 * controller state structure
300 */
301static inline void error_reset(struct cardstate *cs)
302{
303 //FIXME try to recover without bothering the user
304 err("unrecoverable error - please disconnect the Gigaset base to reset");
305}
306
307/* check_pending
308 * check for completion of pending control request
309 * parameter:
310 * urb USB request block of completed request
311 * urb->context = hardware specific controller state structure
312 */
313static void check_pending(struct bas_cardstate *ucs)
314{
315 unsigned long flags;
316
317 IFNULLRET(ucs);
318 IFNULLRET(cardstate);
319
320 spin_lock_irqsave(&ucs->lock, flags);
321 switch (ucs->pending) {
322 case 0:
323 break;
324 case HD_OPEN_ATCHANNEL:
325 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
326 ucs->pending = 0;
327 break;
328 case HD_OPEN_B1CHANNEL:
329 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
330 ucs->pending = 0;
331 break;
332 case HD_OPEN_B2CHANNEL:
333 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
334 ucs->pending = 0;
335 break;
336 case HD_CLOSE_ATCHANNEL:
337 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
338 ucs->pending = 0;
339 //wake_up_interruptible(cs->initwait);
340 //FIXME need own wait queue?
341 break;
342 case HD_CLOSE_B1CHANNEL:
343 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
344 ucs->pending = 0;
345 break;
346 case HD_CLOSE_B2CHANNEL:
347 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
348 ucs->pending = 0;
349 break;
350 case HD_DEVICE_INIT_ACK: /* no reply expected */
351 ucs->pending = 0;
352 break;
353 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
354 * are handled separately and should never end up here
355 */
356 default:
357 warn("unknown pending request 0x%02x cleared", ucs->pending);
358 ucs->pending = 0;
359 }
360
361 if (!ucs->pending)
362 del_timer(&ucs->timer_ctrl);
363
364 spin_unlock_irqrestore(&ucs->lock, flags);
365}
366
367/* cmd_in_timeout
368 * timeout routine for command input request
369 * argument:
370 * controller state structure
371 */
372static void cmd_in_timeout(unsigned long data)
373{
374 struct cardstate *cs = (struct cardstate *) data;
375 struct bas_cardstate *ucs;
376 unsigned long flags;
377
378 IFNULLRET(cs);
379 ucs = cs->hw.bas;
380 IFNULLRET(ucs);
381
382 spin_lock_irqsave(&cs->lock, flags);
383 if (!atomic_read(&cs->connected)) {
384 dbg(DEBUG_USBREQ, "%s: disconnected", __func__);
385 spin_unlock_irqrestore(&cs->lock, flags);
386 return;
387 }
388 if (!ucs->rcvbuf_size) {
389 dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
390 spin_unlock_irqrestore(&cs->lock, flags);
391 return;
392 }
393 spin_unlock_irqrestore(&cs->lock, flags);
394
395 err("timeout reading AT response");
396 error_reset(cs); //FIXME retry?
397}
398
399
400static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs);
401
402/* atread_submit
403 * submit an HD_READ_ATMESSAGE command URB
404 * parameters:
405 * cs controller state structure
406 * timeout timeout in 1/10 sec., 0: none
407 * return value:
408 * 0 on success
409 * -EINVAL if a NULL pointer is encountered somewhere
410 * -EBUSY if another request is pending
411 * any URB submission error code
412 */
413static int atread_submit(struct cardstate *cs, int timeout)
414{
415 struct bas_cardstate *ucs;
416 int ret;
417
418 IFNULLRETVAL(cs, -EINVAL);
419 ucs = cs->hw.bas;
420 IFNULLRETVAL(ucs, -EINVAL);
421 IFNULLRETVAL(ucs->urb_cmd_in, -EINVAL);
422
423 dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)", ucs->rcvbuf_size);
424
425 if (ucs->urb_cmd_in->status == -EINPROGRESS) {
426 err("could not submit HD_READ_ATMESSAGE: URB busy");
427 return -EBUSY;
428 }
429
430 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
431 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
432 ucs->dr_cmd_in.wValue = 0;
433 ucs->dr_cmd_in.wIndex = 0;
434 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
435 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
436 usb_rcvctrlpipe(ucs->udev, 0),
437 (unsigned char*) & ucs->dr_cmd_in,
438 ucs->rcvbuf, ucs->rcvbuf_size,
439 read_ctrl_callback, cs->inbuf);
440
441 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
442 err("could not submit HD_READ_ATMESSAGE: %s",
443 get_usb_statmsg(ret));
444 return ret;
445 }
446
447 if (timeout > 0) {
448 dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
449 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
450 ucs->timer_cmd_in.data = (unsigned long) cs;
451 ucs->timer_cmd_in.function = cmd_in_timeout;
452 add_timer(&ucs->timer_cmd_in);
453 }
454 return 0;
455}
456
457static void stopurbs(struct bas_bc_state *);
458static int start_cbsend(struct cardstate *);
459
460/* set/clear bits in base connection state
461 */
462inline static void update_basstate(struct bas_cardstate *ucs,
463 int set, int clear)
464{
465 unsigned long flags;
466 int state;
467
468 spin_lock_irqsave(&ucs->lock, flags);
469 state = atomic_read(&ucs->basstate);
470 state &= ~clear;
471 state |= set;
472 atomic_set(&ucs->basstate, state);
473 spin_unlock_irqrestore(&ucs->lock, flags);
474}
475
476
477/* read_int_callback
478 * USB completion handler for interrupt pipe input
479 * called by the USB subsystem in interrupt context
480 * parameter:
481 * urb USB request block
482 * urb->context = controller state structure
483 */
484static void read_int_callback(struct urb *urb, struct pt_regs *regs)
485{
486 struct cardstate *cs;
487 struct bas_cardstate *ucs;
488 struct bc_state *bcs;
489 unsigned long flags;
490 int status;
491 unsigned l;
492 int channel;
493
494 IFNULLRET(urb);
495 cs = (struct cardstate *) urb->context;
496 IFNULLRET(cs);
497 ucs = cs->hw.bas;
498 IFNULLRET(ucs);
499
500 if (unlikely(!atomic_read(&cs->connected))) {
501 warn("%s: disconnected", __func__);
502 return;
503 }
504
505 switch (urb->status) {
506 case 0: /* success */
507 break;
508 case -ENOENT: /* canceled */
509 case -ECONNRESET: /* canceled (async) */
510 case -EINPROGRESS: /* pending */
511 /* ignore silently */
512 dbg(DEBUG_USBREQ,
513 "%s: %s", __func__, get_usb_statmsg(urb->status));
514 return;
515 default: /* severe trouble */
516 warn("interrupt read: %s", get_usb_statmsg(urb->status));
517 //FIXME corrective action? resubmission always ok?
518 goto resubmit;
519 }
520
521 l = (unsigned) ucs->int_in_buf[1] +
522 (((unsigned) ucs->int_in_buf[2]) << 8);
523
524 dbg(DEBUG_USBREQ,
525 "<-------%d: 0x%02x (%u [0x%02x 0x%02x])", urb->actual_length,
526 (int)ucs->int_in_buf[0], l,
527 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
528
529 channel = 0;
530
531 switch (ucs->int_in_buf[0]) {
532 case HD_DEVICE_INIT_OK:
533 update_basstate(ucs, BS_INIT, 0);
534 break;
535
536 case HD_READY_SEND_ATDATA:
537 del_timer(&ucs->timer_atrdy);
538 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
539 start_cbsend(cs);
540 break;
541
542 case HD_OPEN_B2CHANNEL_ACK:
543 ++channel;
544 case HD_OPEN_B1CHANNEL_ACK:
545 bcs = cs->bcs + channel;
546 update_basstate(ucs, BS_B1OPEN << channel, 0);
547 gigaset_bchannel_up(bcs);
548 break;
549
550 case HD_OPEN_ATCHANNEL_ACK:
551 update_basstate(ucs, BS_ATOPEN, 0);
552 start_cbsend(cs);
553 break;
554
555 case HD_CLOSE_B2CHANNEL_ACK:
556 ++channel;
557 case HD_CLOSE_B1CHANNEL_ACK:
558 bcs = cs->bcs + channel;
559 update_basstate(ucs, 0, BS_B1OPEN << channel);
560 stopurbs(bcs->hw.bas);
561 gigaset_bchannel_down(bcs);
562 break;
563
564 case HD_CLOSE_ATCHANNEL_ACK:
565 update_basstate(ucs, 0, BS_ATOPEN);
566 break;
567
568 case HD_B2_FLOW_CONTROL:
569 ++channel;
570 case HD_B1_FLOW_CONTROL:
571 bcs = cs->bcs + channel;
572 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
573 &bcs->hw.bas->corrbytes);
574 dbg(DEBUG_ISO,
575 "Flow control (channel %d, sub %d): 0x%02x => %d",
576 channel, bcs->hw.bas->numsub, l,
577 atomic_read(&bcs->hw.bas->corrbytes));
578 break;
579
580 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
581 if (!l) {
582 warn("HD_RECEIVEATDATA_ACK with length 0 ignored");
583 break;
584 }
585 spin_lock_irqsave(&cs->lock, flags);
586 if (ucs->rcvbuf_size) {
587 spin_unlock_irqrestore(&cs->lock, flags);
588 err("receive AT data overrun, %d bytes lost", l);
589 error_reset(cs); //FIXME reschedule
590 break;
591 }
592 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
593 spin_unlock_irqrestore(&cs->lock, flags);
594 err("%s: out of memory, %d bytes lost", __func__, l);
595 error_reset(cs); //FIXME reschedule
596 break;
597 }
598 ucs->rcvbuf_size = l;
599 ucs->retry_cmd_in = 0;
600 if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
601 kfree(ucs->rcvbuf);
602 ucs->rcvbuf = NULL;
603 ucs->rcvbuf_size = 0;
604 error_reset(cs); //FIXME reschedule
605 }
606 spin_unlock_irqrestore(&cs->lock, flags);
607 break;
608
609 case HD_RESET_INTERRUPT_PIPE_ACK:
610 dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
611 break;
612
613 case HD_SUSPEND_END:
614 dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
615 break;
616
617 default:
618 warn("unknown Gigaset signal 0x%02x (%u) ignored",
619 (int) ucs->int_in_buf[0], l);
620 }
621
622 check_pending(ucs);
623
624resubmit:
625 status = usb_submit_urb(urb, SLAB_ATOMIC);
626 if (unlikely(status)) {
627 err("could not resubmit interrupt URB: %s",
628 get_usb_statmsg(status));
629 error_reset(cs);
630 }
631}
632
633/* read_ctrl_callback
634 * USB completion handler for control pipe input
635 * called by the USB subsystem in interrupt context
636 * parameter:
637 * urb USB request block
638 * urb->context = inbuf structure for controller state
639 */
640static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
641{
642 struct cardstate *cs;
643 struct bas_cardstate *ucs;
644 unsigned numbytes;
645 unsigned long flags;
646 struct inbuf_t *inbuf;
647 int have_data = 0;
648
649 IFNULLRET(urb);
650 inbuf = (struct inbuf_t *) urb->context;
651 IFNULLRET(inbuf);
652 cs = inbuf->cs;
653 IFNULLRET(cs);
654 ucs = cs->hw.bas;
655 IFNULLRET(ucs);
656
657 spin_lock_irqsave(&cs->lock, flags);
658 if (!atomic_read(&cs->connected)) {
659 warn("%s: disconnected", __func__);
660 spin_unlock_irqrestore(&cs->lock, flags);
661 return;
662 }
663
664 if (!ucs->rcvbuf_size) {
665 warn("%s: no receive in progress", __func__);
666 spin_unlock_irqrestore(&cs->lock, flags);
667 return;
668 }
669
670 del_timer(&ucs->timer_cmd_in);
671
672 switch (urb->status) {
673 case 0: /* normal completion */
674 numbytes = urb->actual_length;
675 if (unlikely(numbytes == 0)) {
676 warn("control read: empty block received");
677 goto retry;
678 }
679 if (unlikely(numbytes != ucs->rcvbuf_size)) {
680 warn("control read: received %d chars, expected %d",
681 numbytes, ucs->rcvbuf_size);
682 if (numbytes > ucs->rcvbuf_size)
683 numbytes = ucs->rcvbuf_size;
684 }
685
686 /* copy received bytes to inbuf */
687 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
688
689 if (unlikely(numbytes < ucs->rcvbuf_size)) {
690 /* incomplete - resubmit for remaining bytes */
691 ucs->rcvbuf_size -= numbytes;
692 ucs->retry_cmd_in = 0;
693 goto retry;
694 }
695 break;
696
697 case -ENOENT: /* canceled */
698 case -ECONNRESET: /* canceled (async) */
699 case -EINPROGRESS: /* pending */
700 /* no action necessary */
701 dbg(DEBUG_USBREQ,
702 "%s: %s", __func__, get_usb_statmsg(urb->status));
703 break;
704
705 default: /* severe trouble */
706 warn("control read: %s", get_usb_statmsg(urb->status));
707 retry:
708 if (ucs->retry_cmd_in++ < BAS_RETRY) {
709 notice("control read: retry %d", ucs->retry_cmd_in);
710 if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
711 /* resubmitted - bypass regular exit block */
712 spin_unlock_irqrestore(&cs->lock, flags);
713 return;
714 }
715 } else {
716 err("control read: giving up after %d tries",
717 ucs->retry_cmd_in);
718 }
719 error_reset(cs);
720 }
721
722 kfree(ucs->rcvbuf);
723 ucs->rcvbuf = NULL;
724 ucs->rcvbuf_size = 0;
725 spin_unlock_irqrestore(&cs->lock, flags);
726 if (have_data) {
727 dbg(DEBUG_INTR, "%s-->BH", __func__);
728 gigaset_schedule_event(cs);
729 }
730}
731
732/* read_iso_callback
733 * USB completion handler for B channel isochronous input
734 * called by the USB subsystem in interrupt context
735 * parameter:
736 * urb USB request block of completed request
737 * urb->context = bc_state structure
738 */
739static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
740{
741 struct bc_state *bcs;
742 struct bas_bc_state *ubc;
743 unsigned long flags;
744 int i, rc;
745
746 IFNULLRET(urb);
747 IFNULLRET(urb->context);
748 IFNULLRET(cardstate);
749
750 /* status codes not worth bothering the tasklet with */
751 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
752 urb->status == -EINPROGRESS)) {
753 dbg(DEBUG_ISO,
754 "%s: %s", __func__, get_usb_statmsg(urb->status));
755 return;
756 }
757
758 bcs = (struct bc_state *) urb->context;
759 ubc = bcs->hw.bas;
760 IFNULLRET(ubc);
761
762 spin_lock_irqsave(&ubc->isoinlock, flags);
763 if (likely(ubc->isoindone == NULL)) {
764 /* pass URB to tasklet */
765 ubc->isoindone = urb;
766 tasklet_schedule(&ubc->rcvd_tasklet);
767 } else {
768 /* tasklet still busy, drop data and resubmit URB */
769 ubc->loststatus = urb->status;
770 for (i = 0; i < BAS_NUMFRAMES; i++) {
771 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
772 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
773 urb->iso_frame_desc[i].status != -EINPROGRESS)) {
774 ubc->loststatus = urb->iso_frame_desc[i].status;
775 }
776 urb->iso_frame_desc[i].status = 0;
777 urb->iso_frame_desc[i].actual_length = 0;
778 }
779 if (likely(atomic_read(&ubc->running))) {
780 urb->dev = bcs->cs->hw.bas->udev; /* clobbered by USB subsystem */
781 urb->transfer_flags = URB_ISO_ASAP;
782 urb->number_of_packets = BAS_NUMFRAMES;
783 dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit", __func__);
784 rc = usb_submit_urb(urb, SLAB_ATOMIC);
785 if (unlikely(rc != 0)) {
786 err("could not resubmit isochronous read URB: %s",
787 get_usb_statmsg(rc));
788 dump_urb(DEBUG_ISO, "isoc read", urb);
789 error_hangup(bcs);
790 }
791 }
792 }
793 spin_unlock_irqrestore(&ubc->isoinlock, flags);
794}
795
796/* write_iso_callback
797 * USB completion handler for B channel isochronous output
798 * called by the USB subsystem in interrupt context
799 * parameter:
800 * urb USB request block of completed request
801 * urb->context = isow_urbctx_t structure
802 */
803static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
804{
805 struct isow_urbctx_t *ucx;
806 struct bas_bc_state *ubc;
807 unsigned long flags;
808
809 IFNULLRET(urb);
810 IFNULLRET(urb->context);
811 IFNULLRET(cardstate);
812
813 /* status codes not worth bothering the tasklet with */
814 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
815 urb->status == -EINPROGRESS)) {
816 dbg(DEBUG_ISO,
817 "%s: %s", __func__, get_usb_statmsg(urb->status));
818 return;
819 }
820
821 /* pass URB context to tasklet */
822 ucx = (struct isow_urbctx_t *) urb->context;
823 IFNULLRET(ucx->bcs);
824 ubc = ucx->bcs->hw.bas;
825 IFNULLRET(ubc);
826
827 spin_lock_irqsave(&ubc->isooutlock, flags);
828 ubc->isooutovfl = ubc->isooutdone;
829 ubc->isooutdone = ucx;
830 spin_unlock_irqrestore(&ubc->isooutlock, flags);
831 tasklet_schedule(&ubc->sent_tasklet);
832}
833
834/* starturbs
835 * prepare and submit USB request blocks for isochronous input and output
836 * argument:
837 * B channel control structure
838 * return value:
839 * 0 on success
840 * < 0 on error (no URBs submitted)
841 */
842static int starturbs(struct bc_state *bcs)
843{
844 struct urb *urb;
845 struct bas_bc_state *ubc;
846 int j, k;
847 int rc;
848
849 IFNULLRETVAL(bcs, -EFAULT);
850 ubc = bcs->hw.bas;
851 IFNULLRETVAL(ubc, -EFAULT);
852
853 /* initialize L2 reception */
854 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
855 bcs->inputstate |= INS_flag_hunt;
856
857 /* submit all isochronous input URBs */
858 atomic_set(&ubc->running, 1);
859 for (k = 0; k < BAS_INURBS; k++) {
860 urb = ubc->isoinurbs[k];
861 if (!urb) {
862 err("isoinurbs[%d]==NULL", k);
863 rc = -EFAULT;
864 goto error;
865 }
866
867 urb->dev = bcs->cs->hw.bas->udev;
868 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
869 urb->transfer_flags = URB_ISO_ASAP;
870 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
871 urb->transfer_buffer_length = BAS_INBUFSIZE;
872 urb->number_of_packets = BAS_NUMFRAMES;
873 urb->interval = BAS_FRAMETIME;
874 urb->complete = read_iso_callback;
875 urb->context = bcs;
876 for (j = 0; j < BAS_NUMFRAMES; j++) {
877 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
878 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
879 urb->iso_frame_desc[j].status = 0;
880 urb->iso_frame_desc[j].actual_length = 0;
881 }
882
883 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
884 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
885 err("could not submit isochronous read URB %d: %s",
886 k, get_usb_statmsg(rc));
887 goto error;
888 }
889 }
890
891 /* initialize L2 transmission */
892 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
893
894 /* set up isochronous output URBs for flag idling */
895 for (k = 0; k < BAS_OUTURBS; ++k) {
896 urb = ubc->isoouturbs[k].urb;
897 if (!urb) {
898 err("isoouturbs[%d].urb==NULL", k);
899 rc = -EFAULT;
900 goto error;
901 }
902 urb->dev = bcs->cs->hw.bas->udev;
903 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
904 urb->transfer_flags = URB_ISO_ASAP;
905 urb->transfer_buffer = ubc->isooutbuf->data;
906 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
907 urb->number_of_packets = BAS_NUMFRAMES;
908 urb->interval = BAS_FRAMETIME;
909 urb->complete = write_iso_callback;
910 urb->context = &ubc->isoouturbs[k];
911 for (j = 0; j < BAS_NUMFRAMES; ++j) {
912 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
913 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
914 urb->iso_frame_desc[j].status = 0;
915 urb->iso_frame_desc[j].actual_length = 0;
916 }
917 ubc->isoouturbs[k].limit = -1;
918 }
919
920 /* submit two URBs, keep third one */
921 for (k = 0; k < 2; ++k) {
922 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
923 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
924 if (rc != 0) {
925 err("could not submit isochronous write URB %d: %s",
926 k, get_usb_statmsg(rc));
927 goto error;
928 }
929 }
930 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
931 ubc->isooutfree = &ubc->isoouturbs[2];
932 ubc->isooutdone = ubc->isooutovfl = NULL;
933 return 0;
934 error:
935 stopurbs(ubc);
936 return rc;
937}
938
939/* stopurbs
940 * cancel the USB request blocks for isochronous input and output
941 * errors are silently ignored
942 * argument:
943 * B channel control structure
944 */
945static void stopurbs(struct bas_bc_state *ubc)
946{
947 int k, rc;
948
949 IFNULLRET(ubc);
950
951 atomic_set(&ubc->running, 0);
952
953 for (k = 0; k < BAS_INURBS; ++k) {
954 rc = usb_unlink_urb(ubc->isoinurbs[k]);
955 dbg(DEBUG_ISO, "%s: isoc input URB %d unlinked, result = %d",
956 __func__, k, rc);
957 }
958
959 for (k = 0; k < BAS_OUTURBS; ++k) {
960 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
961 dbg(DEBUG_ISO, "%s: isoc output URB %d unlinked, result = %d",
962 __func__, k, rc);
963 }
964}
965
966/* Isochronous Write - Bottom Half */
967/* =============================== */
968
969/* submit_iso_write_urb
970 * fill and submit the next isochronous write URB
971 * parameters:
972 * bcs B channel state structure
973 * return value:
974 * number of frames submitted in URB
975 * 0 if URB not submitted because no data available (isooutbuf busy)
976 * error code < 0 on error
977 */
978static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
979{
980 struct urb *urb;
981 struct bas_bc_state *ubc;
982 struct usb_iso_packet_descriptor *ifd;
983 int corrbytes, nframe, rc;
984
985 IFNULLRETVAL(ucx, -EFAULT);
986 urb = ucx->urb;
987 IFNULLRETVAL(urb, -EFAULT);
988 IFNULLRETVAL(ucx->bcs, -EFAULT);
989 ubc = ucx->bcs->hw.bas;
990 IFNULLRETVAL(ubc, -EFAULT);
991
992 urb->dev = ucx->bcs->cs->hw.bas->udev; /* clobbered by USB subsystem */
993 urb->transfer_flags = URB_ISO_ASAP;
994 urb->transfer_buffer = ubc->isooutbuf->data;
995 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
996
997 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
998 ifd = &urb->iso_frame_desc[nframe];
999
1000 /* compute frame length according to flow control */
1001 ifd->length = BAS_NORMFRAME;
1002 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
1003 dbg(DEBUG_ISO, "%s: corrbytes=%d", __func__, corrbytes);
1004 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1005 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1006 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1007 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1008 ifd->length += corrbytes;
1009 atomic_add(-corrbytes, &ubc->corrbytes);
1010 }
1011 //dbg(DEBUG_ISO, "%s: frame %d length=%d", __func__, nframe, ifd->length);
1012
1013 /* retrieve block of data to send */
1014 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1015 if (ifd->offset < 0) {
1016 if (ifd->offset == -EBUSY) {
1017 dbg(DEBUG_ISO, "%s: buffer busy at frame %d",
1018 __func__, nframe);
1019 /* tasklet will be restarted from gigaset_send_skb() */
1020 } else {
1021 err("%s: buffer error %d at frame %d",
1022 __func__, ifd->offset, nframe);
1023 return ifd->offset;
1024 }
1025 break;
1026 }
1027 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1028 ifd->status = 0;
1029 ifd->actual_length = 0;
1030 }
1031 if ((urb->number_of_packets = nframe) > 0) {
1032 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
1033 err("could not submit isochronous write URB: %s",
1034 get_usb_statmsg(rc));
1035 dump_urb(DEBUG_ISO, "isoc write", urb);
1036 return rc;
1037 }
1038 ++ubc->numsub;
1039 }
1040 return nframe;
1041}
1042
1043/* write_iso_tasklet
1044 * tasklet scheduled when an isochronous output URB from the Gigaset device
1045 * has completed
1046 * parameter:
1047 * data B channel state structure
1048 */
1049static void write_iso_tasklet(unsigned long data)
1050{
1051 struct bc_state *bcs;
1052 struct bas_bc_state *ubc;
1053 struct cardstate *cs;
1054 struct isow_urbctx_t *done, *next, *ovfl;
1055 struct urb *urb;
1056 struct usb_iso_packet_descriptor *ifd;
1057 int offset;
1058 unsigned long flags;
1059 int i;
1060 struct sk_buff *skb;
1061 int len;
1062
1063 bcs = (struct bc_state *) data;
1064 IFNULLRET(bcs);
1065 ubc = bcs->hw.bas;
1066 IFNULLRET(ubc);
1067 cs = bcs->cs;
1068 IFNULLRET(cs);
1069
1070 /* loop while completed URBs arrive in time */
1071 for (;;) {
1072 if (unlikely(!atomic_read(&cs->connected))) {
1073 warn("%s: disconnected", __func__);
1074 return;
1075 }
1076
1077 if (unlikely(!(atomic_read(&ubc->running)))) {
1078 dbg(DEBUG_ISO, "%s: not running", __func__);
1079 return;
1080 }
1081
1082 /* retrieve completed URBs */
1083 spin_lock_irqsave(&ubc->isooutlock, flags);
1084 done = ubc->isooutdone;
1085 ubc->isooutdone = NULL;
1086 ovfl = ubc->isooutovfl;
1087 ubc->isooutovfl = NULL;
1088 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1089 if (ovfl) {
1090 err("isochronous write buffer underrun - buy a faster machine :-)");
1091 error_hangup(bcs);
1092 break;
1093 }
1094 if (!done)
1095 break;
1096
1097 /* submit free URB if available */
1098 spin_lock_irqsave(&ubc->isooutlock, flags);
1099 next = ubc->isooutfree;
1100 ubc->isooutfree = NULL;
1101 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1102 if (next) {
1103 if (submit_iso_write_urb(next) <= 0) {
1104 /* could not submit URB, put it back */
1105 spin_lock_irqsave(&ubc->isooutlock, flags);
1106 if (ubc->isooutfree == NULL) {
1107 ubc->isooutfree = next;
1108 next = NULL;
1109 }
1110 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1111 if (next) {
1112 /* couldn't put it back */
1113 err("losing isochronous write URB");
1114 error_hangup(bcs);
1115 }
1116 }
1117 }
1118
1119 /* process completed URB */
1120 urb = done->urb;
1121 switch (urb->status) {
1122 case 0: /* normal completion */
1123 break;
1124 case -EXDEV: /* inspect individual frames */
1125 /* assumptions (for lack of documentation):
1126 * - actual_length bytes of the frame in error are successfully sent
1127 * - all following frames are not sent at all
1128 */
1129 dbg(DEBUG_ISO, "%s: URB partially completed", __func__);
1130 offset = done->limit; /* just in case */
1131 for (i = 0; i < BAS_NUMFRAMES; i++) {
1132 ifd = &urb->iso_frame_desc[i];
1133 if (ifd->status ||
1134 ifd->actual_length != ifd->length) {
1135 warn("isochronous write: frame %d: %s, "
1136 "only %d of %d bytes sent",
1137 i, get_usb_statmsg(ifd->status),
1138 ifd->actual_length, ifd->length);
1139 offset = (ifd->offset +
1140 ifd->actual_length)
1141 % BAS_OUTBUFSIZE;
1142 break;
1143 }
1144 }
1145#ifdef CONFIG_GIGASET_DEBUG
1146 /* check assumption on remaining frames */
1147 for (; i < BAS_NUMFRAMES; i++) {
1148 ifd = &urb->iso_frame_desc[i];
1149 if (ifd->status != -EINPROGRESS
1150 || ifd->actual_length != 0) {
1151 warn("isochronous write: frame %d: %s, "
1152 "%d of %d bytes sent",
1153 i, get_usb_statmsg(ifd->status),
1154 ifd->actual_length, ifd->length);
1155 offset = (ifd->offset +
1156 ifd->actual_length)
1157 % BAS_OUTBUFSIZE;
1158 break;
1159 }
1160 }
1161#endif
1162 break;
1163 case -EPIPE: //FIXME is this the code for "underrun"?
1164 err("isochronous write stalled");
1165 error_hangup(bcs);
1166 break;
1167 default: /* severe trouble */
1168 warn("isochronous write: %s",
1169 get_usb_statmsg(urb->status));
1170 }
1171
1172 /* mark the write buffer area covered by this URB as free */
1173 if (done->limit >= 0)
1174 atomic_set(&ubc->isooutbuf->read, done->limit);
1175
1176 /* mark URB as free */
1177 spin_lock_irqsave(&ubc->isooutlock, flags);
1178 next = ubc->isooutfree;
1179 ubc->isooutfree = done;
1180 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1181 if (next) {
1182 /* only one URB still active - resubmit one */
1183 if (submit_iso_write_urb(next) <= 0) {
1184 /* couldn't submit */
1185 error_hangup(bcs);
1186 }
1187 }
1188 }
1189
1190 /* process queued SKBs */
1191 while ((skb = skb_dequeue(&bcs->squeue))) {
1192 /* copy to output buffer, doing L2 encapsulation */
1193 len = skb->len;
1194 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1195 /* insufficient buffer space, push back onto queue */
1196 skb_queue_head(&bcs->squeue, skb);
1197 dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1198 __func__, skb_queue_len(&bcs->squeue));
1199 break;
1200 }
1201 skb_pull(skb, len);
1202 gigaset_skb_sent(bcs, skb);
1203 dev_kfree_skb_any(skb);
1204 }
1205}
1206
1207/* Isochronous Read - Bottom Half */
1208/* ============================== */
1209
1210/* read_iso_tasklet
1211 * tasklet scheduled when an isochronous input URB from the Gigaset device
1212 * has completed
1213 * parameter:
1214 * data B channel state structure
1215 */
1216static void read_iso_tasklet(unsigned long data)
1217{
1218 struct bc_state *bcs;
1219 struct bas_bc_state *ubc;
1220 struct cardstate *cs;
1221 struct urb *urb;
1222 char *rcvbuf;
1223 unsigned long flags;
1224 int totleft, numbytes, offset, frame, rc;
1225
1226 bcs = (struct bc_state *) data;
1227 IFNULLRET(bcs);
1228 ubc = bcs->hw.bas;
1229 IFNULLRET(ubc);
1230 cs = bcs->cs;
1231 IFNULLRET(cs);
1232
1233 /* loop while more completed URBs arrive in the meantime */
1234 for (;;) {
1235 if (!atomic_read(&cs->connected)) {
1236 warn("%s: disconnected", __func__);
1237 return;
1238 }
1239
1240 /* retrieve URB */
1241 spin_lock_irqsave(&ubc->isoinlock, flags);
1242 if (!(urb = ubc->isoindone)) {
1243 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1244 return;
1245 }
1246 ubc->isoindone = NULL;
1247 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1248 warn("isochronous read overrun, dropped URB with status: %s, %d bytes lost",
1249 get_usb_statmsg(ubc->loststatus), ubc->isoinlost);
1250 ubc->loststatus = -EINPROGRESS;
1251 }
1252 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1253
1254 if (unlikely(!(atomic_read(&ubc->running)))) {
1255 dbg(DEBUG_ISO, "%s: channel not running, dropped URB with status: %s",
1256 __func__, get_usb_statmsg(urb->status));
1257 return;
1258 }
1259
1260 switch (urb->status) {
1261 case 0: /* normal completion */
1262 break;
1263 case -EXDEV: /* inspect individual frames (we do that anyway) */
1264 dbg(DEBUG_ISO, "%s: URB partially completed", __func__);
1265 break;
1266 case -ENOENT:
1267 case -ECONNRESET:
1268 dbg(DEBUG_ISO, "%s: URB canceled", __func__);
1269 continue; /* -> skip */
1270 case -EINPROGRESS: /* huh? */
1271 dbg(DEBUG_ISO, "%s: URB still pending", __func__);
1272 continue; /* -> skip */
1273 case -EPIPE:
1274 err("isochronous read stalled");
1275 error_hangup(bcs);
1276 continue; /* -> skip */
1277 default: /* severe trouble */
1278 warn("isochronous read: %s",
1279 get_usb_statmsg(urb->status));
1280 goto error;
1281 }
1282
1283 rcvbuf = urb->transfer_buffer;
1284 totleft = urb->actual_length;
1285 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1286 if (unlikely(urb->iso_frame_desc[frame].status)) {
1287 warn("isochronous read: frame %d: %s",
1288 frame, get_usb_statmsg(urb->iso_frame_desc[frame].status));
1289 break;
1290 }
1291 numbytes = urb->iso_frame_desc[frame].actual_length;
1292 if (unlikely(numbytes > BAS_MAXFRAME)) {
1293 warn("isochronous read: frame %d: numbytes (%d) > BAS_MAXFRAME",
1294 frame, numbytes);
1295 break;
1296 }
1297 if (unlikely(numbytes > totleft)) {
1298 warn("isochronous read: frame %d: numbytes (%d) > totleft (%d)",
1299 frame, numbytes, totleft);
1300 break;
1301 }
1302 offset = urb->iso_frame_desc[frame].offset;
1303 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1304 warn("isochronous read: frame %d: offset (%d) + numbytes (%d) > BAS_INBUFSIZE",
1305 frame, offset, numbytes);
1306 break;
1307 }
1308 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1309 totleft -= numbytes;
1310 }
1311 if (unlikely(totleft > 0))
1312 warn("isochronous read: %d data bytes missing",
1313 totleft);
1314
1315 error:
1316 /* URB processed, resubmit */
1317 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1318 urb->iso_frame_desc[frame].status = 0;
1319 urb->iso_frame_desc[frame].actual_length = 0;
1320 }
1321 urb->dev = bcs->cs->hw.bas->udev; /* clobbered by USB subsystem */
1322 urb->transfer_flags = URB_ISO_ASAP;
1323 urb->number_of_packets = BAS_NUMFRAMES;
1324 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
1325 err("could not resubmit isochronous read URB: %s",
1326 get_usb_statmsg(rc));
1327 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1328 error_hangup(bcs);
1329 }
1330 }
1331}
1332
1333/* Channel Operations */
1334/* ================== */
1335
1336/* req_timeout
1337 * timeout routine for control output request
1338 * argument:
1339 * B channel control structure
1340 */
1341static void req_timeout(unsigned long data)
1342{
1343 struct bc_state *bcs = (struct bc_state *) data;
1344 struct bas_cardstate *ucs;
1345 int pending;
1346 unsigned long flags;
1347
1348 IFNULLRET(bcs);
1349 IFNULLRET(bcs->cs);
1350 ucs = bcs->cs->hw.bas;
1351 IFNULLRET(ucs);
1352
1353 check_pending(ucs);
1354
1355 spin_lock_irqsave(&ucs->lock, flags);
1356 pending = ucs->pending;
1357 ucs->pending = 0;
1358 spin_unlock_irqrestore(&ucs->lock, flags);
1359
1360 switch (pending) {
1361 case 0: /* no pending request */
1362 dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1363 break;
1364
1365 case HD_OPEN_ATCHANNEL:
1366 err("timeout opening AT channel");
1367 error_reset(bcs->cs);
1368 break;
1369
1370 case HD_OPEN_B2CHANNEL:
1371 case HD_OPEN_B1CHANNEL:
1372 err("timeout opening channel %d", bcs->channel + 1);
1373 error_hangup(bcs);
1374 break;
1375
1376 case HD_CLOSE_ATCHANNEL:
1377 err("timeout closing AT channel");
1378 //wake_up_interruptible(cs->initwait);
1379 //FIXME need own wait queue?
1380 break;
1381
1382 case HD_CLOSE_B2CHANNEL:
1383 case HD_CLOSE_B1CHANNEL:
1384 err("timeout closing channel %d", bcs->channel + 1);
1385 break;
1386
1387 default:
1388 warn("request 0x%02x timed out, clearing", pending);
1389 }
1390}
1391
1392/* write_ctrl_callback
1393 * USB completion handler for control pipe output
1394 * called by the USB subsystem in interrupt context
1395 * parameter:
1396 * urb USB request block of completed request
1397 * urb->context = hardware specific controller state structure
1398 */
1399static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1400{
1401 struct bas_cardstate *ucs;
1402 unsigned long flags;
1403
1404 IFNULLRET(urb);
1405 IFNULLRET(urb->context);
1406 IFNULLRET(cardstate);
1407
1408 ucs = (struct bas_cardstate *) urb->context;
1409 spin_lock_irqsave(&ucs->lock, flags);
1410 if (urb->status && ucs->pending) {
1411 err("control request 0x%02x failed: %s",
1412 ucs->pending, get_usb_statmsg(urb->status));
1413 del_timer(&ucs->timer_ctrl);
1414 ucs->pending = 0;
1415 }
1416 /* individual handling of specific request types */
1417 switch (ucs->pending) {
1418 case HD_DEVICE_INIT_ACK: /* no reply expected */
1419 ucs->pending = 0;
1420 break;
1421 }
1422 spin_unlock_irqrestore(&ucs->lock, flags);
1423}
1424
1425/* req_submit
1426 * submit a control output request without message buffer to the Gigaset base
1427 * and optionally start a timeout
1428 * parameters:
1429 * bcs B channel control structure
1430 * req control request code (HD_*)
1431 * val control request parameter value (set to 0 if unused)
1432 * timeout timeout in seconds (0: no timeout)
1433 * return value:
1434 * 0 on success
1435 * -EINVAL if a NULL pointer is encountered somewhere
1436 * -EBUSY if another request is pending
1437 * any URB submission error code
1438 */
1439static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1440{
1441 struct bas_cardstate *ucs;
1442 int ret;
1443 unsigned long flags;
1444
1445 IFNULLRETVAL(bcs, -EINVAL);
1446 IFNULLRETVAL(bcs->cs, -EINVAL);
1447 ucs = bcs->cs->hw.bas;
1448 IFNULLRETVAL(ucs, -EINVAL);
1449 IFNULLRETVAL(ucs->urb_ctrl, -EINVAL);
1450
1451 dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1452
1453 spin_lock_irqsave(&ucs->lock, flags);
1454 if (ucs->pending) {
1455 spin_unlock_irqrestore(&ucs->lock, flags);
1456 err("submission of request 0x%02x failed: request 0x%02x still pending",
1457 req, ucs->pending);
1458 return -EBUSY;
1459 }
1460 if (ucs->urb_ctrl->status == -EINPROGRESS) {
1461 spin_unlock_irqrestore(&ucs->lock, flags);
1462 err("could not submit request 0x%02x: URB busy", req);
1463 return -EBUSY;
1464 }
1465
1466 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1467 ucs->dr_ctrl.bRequest = req;
1468 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1469 ucs->dr_ctrl.wIndex = 0;
1470 ucs->dr_ctrl.wLength = 0;
1471 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1472 usb_sndctrlpipe(ucs->udev, 0),
1473 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1474 write_ctrl_callback, ucs);
1475 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
1476 err("could not submit request 0x%02x: %s",
1477 req, get_usb_statmsg(ret));
1478 spin_unlock_irqrestore(&ucs->lock, flags);
1479 return ret;
1480 }
1481 ucs->pending = req;
1482
1483 if (timeout > 0) {
1484 dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1485 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1486 ucs->timer_ctrl.data = (unsigned long) bcs;
1487 ucs->timer_ctrl.function = req_timeout;
1488 add_timer(&ucs->timer_ctrl);
1489 }
1490
1491 spin_unlock_irqrestore(&ucs->lock, flags);
1492 return 0;
1493}
1494
1495/* gigaset_init_bchannel
1496 * called by common.c to connect a B channel
1497 * initialize isochronous I/O and tell the Gigaset base to open the channel
1498 * argument:
1499 * B channel control structure
1500 * return value:
1501 * 0 on success, error code < 0 on error
1502 */
1503static int gigaset_init_bchannel(struct bc_state *bcs)
1504{
1505 int req, ret;
1506
1507 IFNULLRETVAL(bcs, -EINVAL);
1508
1509 if ((ret = starturbs(bcs)) < 0) {
1510 err("could not start isochronous I/O for channel %d",
1511 bcs->channel + 1);
1512 error_hangup(bcs);
1513 return ret;
1514 }
1515
1516 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1517 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
1518 err("could not open channel %d: %s",
1519 bcs->channel + 1, get_usb_statmsg(ret));
1520 stopurbs(bcs->hw.bas);
1521 error_hangup(bcs);
1522 }
1523 return ret;
1524}
1525
1526/* gigaset_close_bchannel
1527 * called by common.c to disconnect a B channel
1528 * tell the Gigaset base to close the channel
1529 * stopping isochronous I/O and LL notification will be done when the
1530 * acknowledgement for the close arrives
1531 * argument:
1532 * B channel control structure
1533 * return value:
1534 * 0 on success, error code < 0 on error
1535 */
1536static int gigaset_close_bchannel(struct bc_state *bcs)
1537{
1538 int req, ret;
1539
1540 IFNULLRETVAL(bcs, -EINVAL);
1541
1542 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1543 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1544 /* channel not running: just signal common.c */
1545 gigaset_bchannel_down(bcs);
1546 return 0;
1547 }
1548
1549 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1550 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1551 err("could not submit HD_CLOSE_BxCHANNEL request: %s",
1552 get_usb_statmsg(ret));
1553 return ret;
1554}
1555
1556/* Device Operations */
1557/* ================= */
1558
1559/* complete_cb
1560 * unqueue first command buffer from queue, waking any sleepers
1561 * must be called with cs->cmdlock held
1562 * parameter:
1563 * cs controller state structure
1564 */
1565static void complete_cb(struct cardstate *cs)
1566{
1567 struct cmdbuf_t *cb;
1568
1569 IFNULLRET(cs);
1570 cb = cs->cmdbuf;
1571 IFNULLRET(cb);
1572
1573 /* unqueue completed buffer */
1574 cs->cmdbytes -= cs->curlen;
1575 dbg(DEBUG_TRANSCMD | DEBUG_LOCKCMD,
1576 "write_command: sent %u bytes, %u left",
1577 cs->curlen, cs->cmdbytes);
1578 if ((cs->cmdbuf = cb->next) != NULL) {
1579 cs->cmdbuf->prev = NULL;
1580 cs->curlen = cs->cmdbuf->len;
1581 } else {
1582 cs->lastcmdbuf = NULL;
1583 cs->curlen = 0;
1584 }
1585
1586 if (cb->wake_tasklet)
1587 tasklet_schedule(cb->wake_tasklet);
1588
1589 kfree(cb);
1590}
1591
1592static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len);
1593
1594/* write_command_callback
1595 * USB completion handler for AT command transmission
1596 * called by the USB subsystem in interrupt context
1597 * parameter:
1598 * urb USB request block of completed request
1599 * urb->context = controller state structure
1600 */
1601static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1602{
1603 struct cardstate *cs;
1604 unsigned long flags;
1605 struct bas_cardstate *ucs;
1606
1607 IFNULLRET(urb);
1608 cs = (struct cardstate *) urb->context;
1609 IFNULLRET(cs);
1610 ucs = cs->hw.bas;
1611 IFNULLRET(ucs);
1612
1613 /* check status */
1614 switch (urb->status) {
1615 case 0: /* normal completion */
1616 break;
1617 case -ENOENT: /* canceled */
1618 case -ECONNRESET: /* canceled (async) */
1619 case -EINPROGRESS: /* pending */
1620 /* ignore silently */
1621 dbg(DEBUG_USBREQ,
1622 "%s: %s", __func__, get_usb_statmsg(urb->status));
1623 return;
1624 default: /* any failure */
1625 if (++ucs->retry_cmd_out > BAS_RETRY) {
1626 warn("command write: %s, giving up after %d retries",
1627 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1628 break;
1629 }
1630 if (cs->cmdbuf == NULL) {
1631 warn("command write: %s, cannot retry - cmdbuf gone",
1632 get_usb_statmsg(urb->status));
1633 break;
1634 }
1635 notice("command write: %s, retry %d",
1636 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1637 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1638 /* resubmitted - bypass regular exit block */
1639 return;
1640 /* command send failed, assume base still waiting */
1641 update_basstate(ucs, BS_ATREADY, 0);
1642 }
1643
1644 spin_lock_irqsave(&cs->cmdlock, flags);
1645 if (cs->cmdbuf != NULL)
1646 complete_cb(cs);
1647 spin_unlock_irqrestore(&cs->cmdlock, flags);
1648}
1649
1650/* atrdy_timeout
1651 * timeout routine for AT command transmission
1652 * argument:
1653 * controller state structure
1654 */
1655static void atrdy_timeout(unsigned long data)
1656{
1657 struct cardstate *cs = (struct cardstate *) data;
1658 struct bas_cardstate *ucs;
1659
1660 IFNULLRET(cs);
1661 ucs = cs->hw.bas;
1662 IFNULLRET(ucs);
1663
1664 warn("timeout waiting for HD_READY_SEND_ATDATA");
1665
1666 /* fake the missing signal - what else can I do? */
1667 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1668 start_cbsend(cs);
1669}
1670
1671/* atwrite_submit
1672 * submit an HD_WRITE_ATMESSAGE command URB
1673 * parameters:
1674 * cs controller state structure
1675 * buf buffer containing command to send
1676 * len length of command to send
1677 * return value:
1678 * 0 on success
1679 * -EFAULT if a NULL pointer is encountered somewhere
1680 * -EBUSY if another request is pending
1681 * any URB submission error code
1682 */
1683static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1684{
1685 struct bas_cardstate *ucs;
1686 int ret;
1687
1688 IFNULLRETVAL(cs, -EFAULT);
1689 ucs = cs->hw.bas;
1690 IFNULLRETVAL(ucs, -EFAULT);
1691 IFNULLRETVAL(ucs->urb_cmd_out, -EFAULT);
1692
1693 dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1694
1695 if (ucs->urb_cmd_out->status == -EINPROGRESS) {
1696 err("could not submit HD_WRITE_ATMESSAGE: URB busy");
1697 return -EBUSY;
1698 }
1699
1700 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1701 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1702 ucs->dr_cmd_out.wValue = 0;
1703 ucs->dr_cmd_out.wIndex = 0;
1704 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1705 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1706 usb_sndctrlpipe(ucs->udev, 0),
1707 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1708 write_command_callback, cs);
1709
1710 if ((ret = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC)) != 0) {
1711 err("could not submit HD_WRITE_ATMESSAGE: %s",
1712 get_usb_statmsg(ret));
1713 return ret;
1714 }
1715
1716 /* submitted successfully */
1717 update_basstate(ucs, 0, BS_ATREADY);
1718
1719 /* start timeout if necessary */
1720 if (!(atomic_read(&ucs->basstate) & BS_ATTIMER)) {
1721 dbg(DEBUG_OUTPUT,
1722 "setting ATREADY timeout of %d/10 secs", ATRDY_TIMEOUT);
1723 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1724 ucs->timer_atrdy.data = (unsigned long) cs;
1725 ucs->timer_atrdy.function = atrdy_timeout;
1726 add_timer(&ucs->timer_atrdy);
1727 update_basstate(ucs, BS_ATTIMER, 0);
1728 }
1729 return 0;
1730}
1731
1732/* start_cbsend
1733 * start transmission of AT command queue if necessary
1734 * parameter:
1735 * cs controller state structure
1736 * return value:
1737 * 0 on success
1738 * error code < 0 on error
1739 */
1740static int start_cbsend(struct cardstate *cs)
1741{
1742 struct cmdbuf_t *cb;
1743 struct bas_cardstate *ucs;
1744 unsigned long flags;
1745 int rc;
1746 int retval = 0;
1747
1748 IFNULLRETVAL(cs, -EFAULT);
1749 ucs = cs->hw.bas;
1750 IFNULLRETVAL(ucs, -EFAULT);
1751
1752 /* check if AT channel is open */
1753 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
1754 dbg(DEBUG_TRANSCMD | DEBUG_LOCKCMD, "AT channel not open");
1755 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1756 if (rc < 0) {
1757 err("could not open AT channel");
1758 /* flush command queue */
1759 spin_lock_irqsave(&cs->cmdlock, flags);
1760 while (cs->cmdbuf != NULL)
1761 complete_cb(cs);
1762 spin_unlock_irqrestore(&cs->cmdlock, flags);
1763 }
1764 return rc;
1765 }
1766
1767 /* try to send first command in queue */
1768 spin_lock_irqsave(&cs->cmdlock, flags);
1769
1770 while ((cb = cs->cmdbuf) != NULL &&
1771 atomic_read(&ucs->basstate) & BS_ATREADY) {
1772 ucs->retry_cmd_out = 0;
1773 rc = atwrite_submit(cs, cb->buf, cb->len);
1774 if (unlikely(rc)) {
1775 retval = rc;
1776 complete_cb(cs);
1777 }
1778 }
1779
1780 spin_unlock_irqrestore(&cs->cmdlock, flags);
1781 return retval;
1782}
1783
1784/* gigaset_write_cmd
1785 * This function is called by the device independent part of the driver
1786 * to transmit an AT command string to the Gigaset device.
1787 * It encapsulates the device specific method for transmission over the
1788 * direct USB connection to the base.
1789 * The command string is added to the queue of commands to send, and
1790 * USB transmission is started if necessary.
1791 * parameters:
1792 * cs controller state structure
1793 * buf command string to send
1794 * len number of bytes to send (max. IF_WRITEBUF)
1795 * wake_tasklet tasklet to run when transmission is completed (NULL if none)
1796 * return value:
1797 * number of bytes queued on success
1798 * error code < 0 on error
1799 */
1800static int gigaset_write_cmd(struct cardstate *cs,
1801 const unsigned char *buf, int len,
1802 struct tasklet_struct *wake_tasklet)
1803{
1804 struct cmdbuf_t *cb;
1805 unsigned long flags;
1806 int status;
1807
1808 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
1809 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1810 "CMD Transmit", len, buf, 0);
1811
1812 if (!atomic_read(&cs->connected)) {
1813 err("%s: not connected", __func__);
1814 return -ENODEV;
1815 }
1816
1817 if (len <= 0)
1818 return 0; /* nothing to do */
1819
1820 if (len > IF_WRITEBUF)
1821 len = IF_WRITEBUF;
1822 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1823 err("%s: out of memory", __func__);
1824 return -ENOMEM;
1825 }
1826
1827 memcpy(cb->buf, buf, len);
1828 cb->len = len;
1829 cb->offset = 0;
1830 cb->next = NULL;
1831 cb->wake_tasklet = wake_tasklet;
1832
1833 spin_lock_irqsave(&cs->cmdlock, flags);
1834 cb->prev = cs->lastcmdbuf;
1835 if (cs->lastcmdbuf)
1836 cs->lastcmdbuf->next = cb;
1837 else {
1838 cs->cmdbuf = cb;
1839 cs->curlen = len;
1840 }
1841 cs->cmdbytes += len;
1842 cs->lastcmdbuf = cb;
1843 spin_unlock_irqrestore(&cs->cmdlock, flags);
1844
1845 status = start_cbsend(cs);
1846
1847 return status < 0 ? status : len;
1848}
1849
1850/* gigaset_write_room
1851 * tty_driver.write_room interface routine
1852 * return number of characters the driver will accept to be written via gigaset_write_cmd
1853 * parameter:
1854 * controller state structure
1855 * return value:
1856 * number of characters
1857 */
1858static int gigaset_write_room(struct cardstate *cs)
1859{
1860 return IF_WRITEBUF;
1861}
1862
1863/* gigaset_chars_in_buffer
1864 * tty_driver.chars_in_buffer interface routine
1865 * return number of characters waiting to be sent
1866 * parameter:
1867 * controller state structure
1868 * return value:
1869 * number of characters
1870 */
1871static int gigaset_chars_in_buffer(struct cardstate *cs)
1872{
1873 unsigned long flags;
1874 unsigned bytes;
1875
1876 spin_lock_irqsave(&cs->cmdlock, flags);
1877 bytes = cs->cmdbytes;
1878 spin_unlock_irqrestore(&cs->cmdlock, flags);
1879
1880 return bytes;
1881}
1882
1883/* gigaset_brkchars
1884 * implementation of ioctl(GIGASET_BRKCHARS)
1885 * parameter:
1886 * controller state structure
1887 * return value:
1888 * -EINVAL (unimplemented function)
1889 */
1890static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1891{
1892 return -EINVAL;
1893}
1894
1895
1896/* Device Initialization/Shutdown */
1897/* ============================== */
1898
1899/* Free hardware dependent part of the B channel structure
1900 * parameter:
1901 * bcs B channel structure
1902 * return value:
1903 * !=0 on success
1904 */
1905static int gigaset_freebcshw(struct bc_state *bcs)
1906{
1907 if (!bcs->hw.bas)
1908 return 0;
1909
1910 if (bcs->hw.bas->isooutbuf)
1911 kfree(bcs->hw.bas->isooutbuf);
1912 kfree(bcs->hw.bas);
1913 bcs->hw.bas = NULL;
1914 return 1;
1915}
1916
1917/* Initialize hardware dependent part of the B channel structure
1918 * parameter:
1919 * bcs B channel structure
1920 * return value:
1921 * !=0 on success
1922 */
1923static int gigaset_initbcshw(struct bc_state *bcs)
1924{
1925 int i;
1926 struct bas_bc_state *ubc;
1927
1928 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1929 if (!ubc) {
1930 err("could not allocate bas_bc_state");
1931 return 0;
1932 }
1933
1934 atomic_set(&ubc->running, 0);
1935 atomic_set(&ubc->corrbytes, 0);
1936 spin_lock_init(&ubc->isooutlock);
1937 for (i = 0; i < BAS_OUTURBS; ++i) {
1938 ubc->isoouturbs[i].urb = NULL;
1939 ubc->isoouturbs[i].bcs = bcs;
1940 }
1941 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1942 ubc->numsub = 0;
1943 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
1944 err("could not allocate isochronous output buffer");
1945 kfree(ubc);
1946 bcs->hw.bas = NULL;
1947 return 0;
1948 }
1949 tasklet_init(&ubc->sent_tasklet,
1950 &write_iso_tasklet, (unsigned long) bcs);
1951
1952 spin_lock_init(&ubc->isoinlock);
1953 for (i = 0; i < BAS_INURBS; ++i)
1954 ubc->isoinurbs[i] = NULL;
1955 ubc->isoindone = NULL;
1956 ubc->loststatus = -EINPROGRESS;
1957 ubc->isoinlost = 0;
1958 ubc->seqlen = 0;
1959 ubc->inbyte = 0;
1960 ubc->inbits = 0;
1961 ubc->goodbytes = 0;
1962 ubc->alignerrs = 0;
1963 ubc->fcserrs = 0;
1964 ubc->frameerrs = 0;
1965 ubc->giants = 0;
1966 ubc->runts = 0;
1967 ubc->aborts = 0;
1968 ubc->shared0s = 0;
1969 ubc->stolen0s = 0;
1970 tasklet_init(&ubc->rcvd_tasklet,
1971 &read_iso_tasklet, (unsigned long) bcs);
1972 return 1;
1973}
1974
1975static void gigaset_reinitbcshw(struct bc_state *bcs)
1976{
1977 struct bas_bc_state *ubc = bcs->hw.bas;
1978
1979 atomic_set(&bcs->hw.bas->running, 0);
1980 atomic_set(&bcs->hw.bas->corrbytes, 0);
1981 bcs->hw.bas->numsub = 0;
1982 spin_lock_init(&ubc->isooutlock);
1983 spin_lock_init(&ubc->isoinlock);
1984 ubc->loststatus = -EINPROGRESS;
1985}
1986
1987static void gigaset_freecshw(struct cardstate *cs)
1988{
1989 struct bas_cardstate *ucs = cs->hw.bas;
1990
1991 del_timer(&ucs->timer_ctrl);
1992 del_timer(&ucs->timer_atrdy);
1993 del_timer(&ucs->timer_cmd_in);
1994
1995 kfree(cs->hw.bas);
1996}
1997
1998static int gigaset_initcshw(struct cardstate *cs)
1999{
2000 struct bas_cardstate *ucs;
2001
2002 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2003 if (!ucs)
2004 return 0;
2005
2006 ucs->urb_cmd_in = NULL;
2007 ucs->urb_cmd_out = NULL;
2008 ucs->rcvbuf = NULL;
2009 ucs->rcvbuf_size = 0;
2010
2011 spin_lock_init(&ucs->lock);
2012 ucs->pending = 0;
2013
2014 atomic_set(&ucs->basstate, 0);
2015 init_timer(&ucs->timer_ctrl);
2016 init_timer(&ucs->timer_atrdy);
2017 init_timer(&ucs->timer_cmd_in);
2018
2019 return 1;
2020}
2021
2022/* freeurbs
2023 * unlink and deallocate all URBs unconditionally
2024 * caller must make sure that no commands are still in progress
2025 * parameter:
2026 * cs controller state structure
2027 */
2028static void freeurbs(struct cardstate *cs)
2029{
2030 struct bas_cardstate *ucs;
2031 struct bas_bc_state *ubc;
2032 int i, j;
2033
2034 IFNULLRET(cs);
2035 ucs = cs->hw.bas;
2036 IFNULLRET(ucs);
2037
2038 for (j = 0; j < 2; ++j) {
2039 ubc = cs->bcs[j].hw.bas;
2040 IFNULLCONT(ubc);
2041 for (i = 0; i < BAS_OUTURBS; ++i)
2042 if (ubc->isoouturbs[i].urb) {
2043 usb_kill_urb(ubc->isoouturbs[i].urb);
2044 dbg(DEBUG_INIT,
2045 "%s: isoc output URB %d/%d unlinked",
2046 __func__, j, i);
2047 usb_free_urb(ubc->isoouturbs[i].urb);
2048 ubc->isoouturbs[i].urb = NULL;
2049 }
2050 for (i = 0; i < BAS_INURBS; ++i)
2051 if (ubc->isoinurbs[i]) {
2052 usb_kill_urb(ubc->isoinurbs[i]);
2053 dbg(DEBUG_INIT,
2054 "%s: isoc input URB %d/%d unlinked",
2055 __func__, j, i);
2056 usb_free_urb(ubc->isoinurbs[i]);
2057 ubc->isoinurbs[i] = NULL;
2058 }
2059 }
2060 if (ucs->urb_int_in) {
2061 usb_kill_urb(ucs->urb_int_in);
2062 dbg(DEBUG_INIT, "%s: interrupt input URB unlinked", __func__);
2063 usb_free_urb(ucs->urb_int_in);
2064 ucs->urb_int_in = NULL;
2065 }
2066 if (ucs->urb_cmd_out) {
2067 usb_kill_urb(ucs->urb_cmd_out);
2068 dbg(DEBUG_INIT, "%s: command output URB unlinked", __func__);
2069 usb_free_urb(ucs->urb_cmd_out);
2070 ucs->urb_cmd_out = NULL;
2071 }
2072 if (ucs->urb_cmd_in) {
2073 usb_kill_urb(ucs->urb_cmd_in);
2074 dbg(DEBUG_INIT, "%s: command input URB unlinked", __func__);
2075 usb_free_urb(ucs->urb_cmd_in);
2076 ucs->urb_cmd_in = NULL;
2077 }
2078 if (ucs->urb_ctrl) {
2079 usb_kill_urb(ucs->urb_ctrl);
2080 dbg(DEBUG_INIT, "%s: control output URB unlinked", __func__);
2081 usb_free_urb(ucs->urb_ctrl);
2082 ucs->urb_ctrl = NULL;
2083 }
2084}
2085
2086/* gigaset_probe
2087 * This function is called when a new USB device is connected.
2088 * It checks whether the new device is handled by this driver.
2089 */
2090static int gigaset_probe(struct usb_interface *interface,
2091 const struct usb_device_id *id)
2092{
2093 struct usb_host_interface *hostif;
2094 struct usb_device *udev = interface_to_usbdev(interface);
2095 struct cardstate *cs = NULL;
2096 struct bas_cardstate *ucs = NULL;
2097 struct bas_bc_state *ubc;
2098 struct usb_endpoint_descriptor *endpoint;
2099 int i, j;
2100 int ret;
2101
2102 IFNULLRETVAL(udev, -ENODEV);
2103
2104 dbg(DEBUG_ANY,
2105 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2106 __func__, le16_to_cpu(udev->descriptor.idVendor),
2107 le16_to_cpu(udev->descriptor.idProduct));
2108
2109 /* See if the device offered us matches what we can accept */
2110 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_GIGA_VENDOR_ID) ||
2111 (le16_to_cpu(udev->descriptor.idProduct) != USB_GIGA_PRODUCT_ID &&
2112 le16_to_cpu(udev->descriptor.idProduct) != USB_4175_PRODUCT_ID &&
2113 le16_to_cpu(udev->descriptor.idProduct) != USB_SX303_PRODUCT_ID &&
2114 le16_to_cpu(udev->descriptor.idProduct) != USB_SX353_PRODUCT_ID)) {
2115 dbg(DEBUG_ANY, "%s: unmatched ID - exiting", __func__);
2116 return -ENODEV;
2117 }
2118
2119 /* set required alternate setting */
2120 hostif = interface->cur_altsetting;
2121 if (hostif->desc.bAlternateSetting != 3) {
2122 dbg(DEBUG_ANY,
2123 "%s: wrong alternate setting %d - trying to switch",
2124 __func__, hostif->desc.bAlternateSetting);
2125 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2126 warn("usb_set_interface failed, device %d interface %d altsetting %d",
2127 udev->devnum, hostif->desc.bInterfaceNumber,
2128 hostif->desc.bAlternateSetting);
2129 return -ENODEV;
2130 }
2131 hostif = interface->cur_altsetting;
2132 }
2133
2134 /* Reject application specific interfaces
2135 */
2136 if (hostif->desc.bInterfaceClass != 255) {
2137 warn("%s: bInterfaceClass == %d",
2138 __func__, hostif->desc.bInterfaceClass);
2139 return -ENODEV;
2140 }
2141
2142 info("%s: Device matched (Vendor: 0x%x, Product: 0x%x)",
2143 __func__, le16_to_cpu(udev->descriptor.idVendor),
2144 le16_to_cpu(udev->descriptor.idProduct));
2145
2146 cs = gigaset_getunassignedcs(driver);
2147 if (!cs) {
2148 err("%s: no free cardstate", __func__);
2149 return -ENODEV;
2150 }
2151 ucs = cs->hw.bas;
2152 ucs->udev = udev;
2153 ucs->interface = interface;
2154
2155 /* allocate URBs:
2156 * - one for the interrupt pipe
2157 * - three for the different uses of the default control pipe
2158 * - three for each isochronous pipe
2159 */
2160 ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL);
2161 if (!ucs->urb_int_in) {
2162 err("No free urbs available");
2163 goto error;
2164 }
2165 ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL);
2166 if (!ucs->urb_cmd_in) {
2167 err("No free urbs available");
2168 goto error;
2169 }
2170 ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL);
2171 if (!ucs->urb_cmd_out) {
2172 err("No free urbs available");
2173 goto error;
2174 }
2175 ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL);
2176 if (!ucs->urb_ctrl) {
2177 err("No free urbs available");
2178 goto error;
2179 }
2180
2181 for (j = 0; j < 2; ++j) {
2182 ubc = cs->bcs[j].hw.bas;
2183 for (i = 0; i < BAS_OUTURBS; ++i) {
2184 ubc->isoouturbs[i].urb =
2185 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2186 if (!ubc->isoouturbs[i].urb) {
2187 err("No free urbs available");
2188 goto error;
2189 }
2190 }
2191 for (i = 0; i < BAS_INURBS; ++i) {
2192 ubc->isoinurbs[i] =
2193 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2194 if (!ubc->isoinurbs[i]) {
2195 err("No free urbs available");
2196 goto error;
2197 }
2198 }
2199 }
2200
2201 ucs->rcvbuf = NULL;
2202 ucs->rcvbuf_size = 0;
2203
2204 /* Fill the interrupt urb and send it to the core */
2205 endpoint = &hostif->endpoint[0].desc;
2206 usb_fill_int_urb(ucs->urb_int_in, udev,
2207 usb_rcvintpipe(udev,
2208 (endpoint->bEndpointAddress) & 0x0f),
2209 ucs->int_in_buf, 3, read_int_callback, cs,
2210 endpoint->bInterval);
2211 ret = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL);
2212 if (ret) {
2213 err("could not submit interrupt URB: %s", get_usb_statmsg(ret));
2214 goto error;
2215 }
2216
2217 /* tell the device that the driver is ready */
2218 if ((ret = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2219 goto error;
2220
2221 /* tell common part that the device is ready */
2222 if (startmode == SM_LOCKED)
2223 atomic_set(&cs->mstate, MS_LOCKED);
2224 if (!gigaset_start(cs))
2225 goto error;
2226
2227 /* save address of controller structure */
2228 usb_set_intfdata(interface, cs);
2229
2230 /* set up device sysfs */
2231 gigaset_init_dev_sysfs(interface);
2232 return 0;
2233
2234error:
2235 freeurbs(cs);
2236 gigaset_unassign(cs);
2237 return -ENODEV;
2238}
2239
2240/* gigaset_disconnect
2241 * This function is called when the Gigaset base is unplugged.
2242 */
2243static void gigaset_disconnect(struct usb_interface *interface)
2244{
2245 struct cardstate *cs;
2246 struct bas_cardstate *ucs;
2247
2248 /* clear device sysfs */
2249 gigaset_free_dev_sysfs(interface);
2250
2251 cs = usb_get_intfdata(interface);
2252 usb_set_intfdata(interface, NULL);
2253
2254 IFNULLRET(cs);
2255 ucs = cs->hw.bas;
2256 IFNULLRET(ucs);
2257
2258 info("disconnecting GigaSet base");
2259 gigaset_stop(cs);
2260 freeurbs(cs);
2261 kfree(ucs->rcvbuf);
2262 ucs->rcvbuf = NULL;
2263 ucs->rcvbuf_size = 0;
2264 atomic_set(&ucs->basstate, 0);
2265 gigaset_unassign(cs);
2266}
2267
2268static struct gigaset_ops gigops = {
2269 gigaset_write_cmd,
2270 gigaset_write_room,
2271 gigaset_chars_in_buffer,
2272 gigaset_brkchars,
2273 gigaset_init_bchannel,
2274 gigaset_close_bchannel,
2275 gigaset_initbcshw,
2276 gigaset_freebcshw,
2277 gigaset_reinitbcshw,
2278 gigaset_initcshw,
2279 gigaset_freecshw,
2280 gigaset_set_modem_ctrl,
2281 gigaset_baud_rate,
2282 gigaset_set_line_ctrl,
2283 gigaset_isoc_send_skb,
2284 gigaset_isoc_input,
2285};
2286
2287/* bas_gigaset_init
2288 * This function is called after the kernel module is loaded.
2289 */
2290static int __init bas_gigaset_init(void)
2291{
2292 int result;
2293
2294 /* allocate memory for our driver state and intialize it */
2295 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2296 GIGASET_MODULENAME, GIGASET_DEVNAME,
2297 GIGASET_DEVFSNAME, &gigops,
2298 THIS_MODULE)) == NULL)
2299 goto error;
2300
2301 /* allocate memory for our device state and intialize it */
2302 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode, GIGASET_MODULENAME);
2303 if (!cardstate)
2304 goto error;
2305
2306 /* register this driver with the USB subsystem */
2307 result = usb_register(&gigaset_usb_driver);
2308 if (result < 0) {
2309 err("usb_register failed (error %d)", -result);
2310 goto error;
2311 }
2312
2313 info(DRIVER_AUTHOR);
2314 info(DRIVER_DESC);
2315 return 0;
2316
2317error: if (cardstate)
2318 gigaset_freecs(cardstate);
2319 cardstate = NULL;
2320 if (driver)
2321 gigaset_freedriver(driver);
2322 driver = NULL;
2323 return -1;
2324}
2325
2326/* bas_gigaset_exit
2327 * This function is called before the kernel module is unloaded.
2328 */
2329static void __exit bas_gigaset_exit(void)
2330{
2331 gigaset_blockdriver(driver); /* => probe will fail
2332 * => no gigaset_start any more
2333 */
2334
2335 gigaset_shutdown(cardstate);
2336 /* from now on, no isdn callback should be possible */
2337
2338 if (atomic_read(&cardstate->hw.bas->basstate) & BS_ATOPEN) {
2339 dbg(DEBUG_ANY, "closing AT channel");
2340 if (req_submit(cardstate->bcs,
2341 HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT) >= 0) {
2342 /* successfully submitted - wait for completion */
2343 //wait_event_interruptible(cs->initwait, !cs->hw.bas->pending);
2344 //FIXME need own wait queue? wakeup?
2345 }
2346 }
2347
2348 /* deregister this driver with the USB subsystem */
2349 usb_deregister(&gigaset_usb_driver);
2350 /* this will call the disconnect-callback */
2351 /* from now on, no disconnect/probe callback should be running */
2352
2353 gigaset_freecs(cardstate);
2354 cardstate = NULL;
2355 gigaset_freedriver(driver);
2356 driver = NULL;
2357}
2358
2359
2360module_init(bas_gigaset_init);
2361module_exit(bas_gigaset_exit);
2362
2363MODULE_AUTHOR(DRIVER_AUTHOR);
2364MODULE_DESCRIPTION(DRIVER_DESC);
2365MODULE_LICENSE("GPL");