blob: 1cf48cfad1de5ae7ecaae3105b77385f54bcba77 [file] [log] [blame]
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001/*
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 * =====================================================================
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080016 */
17
18#include "gigaset.h"
19
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/timer.h>
24#include <linux/usb.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27
28/* Version Information */
29#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers <Eilers.Stefan@epost.de>"
30#define DRIVER_DESC "USB Driver for Gigaset 307x"
31
32
33/* Module parameters */
34
35static int startmode = SM_ISDN;
36static int cidmode = 1;
37
38module_param(startmode, int, S_IRUGO);
39module_param(cidmode, int, S_IRUGO);
40MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
41MODULE_PARM_DESC(cidmode, "Call-ID mode");
42
43#define GIGASET_MINORS 1
44#define GIGASET_MINOR 16
45#define GIGASET_MODULENAME "bas_gigaset"
46#define GIGASET_DEVFSNAME "gig/bas/"
47#define GIGASET_DEVNAME "ttyGB"
48
49#define IF_WRITEBUF 256 //FIXME
50
51/* Values for the Gigaset 307x */
52#define USB_GIGA_VENDOR_ID 0x0681
53#define USB_GIGA_PRODUCT_ID 0x0001
54#define USB_4175_PRODUCT_ID 0x0002
55#define USB_SX303_PRODUCT_ID 0x0021
56#define USB_SX353_PRODUCT_ID 0x0022
57
58/* table of devices that work with this driver */
59static struct usb_device_id gigaset_table [] = {
60 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_GIGA_PRODUCT_ID) },
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_4175_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
63 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
64 { } /* Terminating entry */
65};
66
67MODULE_DEVICE_TABLE(usb, gigaset_table);
68
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080069/*======================= local function prototypes =============================*/
70
71/* This function is called if a new device is connected to the USB port. It
72 * checks whether this new device belongs to this driver.
73 */
74static int gigaset_probe(struct usb_interface *interface,
75 const struct usb_device_id *id);
76
77/* Function will be called if the device is unplugged */
78static void gigaset_disconnect(struct usb_interface *interface);
79
80
81/*==============================================================================*/
82
83struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070084 struct usb_device *udev; /* USB device pointer */
85 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080086 unsigned char minor; /* starting minor number */
87
Tilman Schmidt784d5852006-04-10 22:55:04 -070088 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080089 struct usb_ctrlrequest dr_ctrl;
90 struct timer_list timer_ctrl; /* control request timeout */
91
92 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -070093 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080094 struct usb_ctrlrequest dr_cmd_out;
95 int retry_cmd_out;
96
Tilman Schmidt784d5852006-04-10 22:55:04 -070097 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080098 struct usb_ctrlrequest dr_cmd_in;
99 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700100 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800101
Tilman Schmidt784d5852006-04-10 22:55:04 -0700102 struct urb *urb_int_in; /* URB for interrupt pipe */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800103 unsigned char int_in_buf[3];
104
105 spinlock_t lock; /* locks all following */
106 atomic_t basstate; /* bitmap (BS_*) */
107 int pending; /* uncompleted base request */
108 int rcvbuf_size; /* size of AT receive buffer */
109 /* 0: no receive in progress */
110 int retry_cmd_in; /* receive req retry count */
111};
112
113/* status of direct USB connection to 307x base (bits in basstate) */
114#define BS_ATOPEN 0x001
115#define BS_B1OPEN 0x002
116#define BS_B2OPEN 0x004
117#define BS_ATREADY 0x008
118#define BS_INIT 0x010
119#define BS_ATTIMER 0x020
120
121
122static struct gigaset_driver *driver = NULL;
123static struct cardstate *cardstate = NULL;
124
125/* usb specific object needed to register this driver with the usb subsystem */
126static struct usb_driver gigaset_usb_driver = {
127 .name = GIGASET_MODULENAME,
128 .probe = gigaset_probe,
129 .disconnect = gigaset_disconnect,
130 .id_table = gigaset_table,
131};
132
133/* get message text for USB status code
134 */
135static char *get_usb_statmsg(int status)
136{
137 static char unkmsg[28];
138
139 switch (status) {
140 case 0:
141 return "success";
142 case -ENOENT:
143 return "canceled";
144 case -ECONNRESET:
145 return "canceled (async)";
146 case -EINPROGRESS:
147 return "pending";
148 case -EPROTO:
149 return "bit stuffing or unknown USB error";
150 case -EILSEQ:
151 return "Illegal byte sequence (CRC mismatch)";
152 case -EPIPE:
153 return "babble detect or endpoint stalled";
154 case -ENOSR:
155 return "buffer error";
156 case -ETIMEDOUT:
157 return "timed out";
158 case -ENODEV:
159 return "device not present";
160 case -EREMOTEIO:
161 return "short packet detected";
162 case -EXDEV:
163 return "partial isochronous transfer";
164 case -EINVAL:
165 return "invalid argument";
166 case -ENXIO:
167 return "URB already queued";
168 case -EAGAIN:
169 return "isochronous start frame too early or too much scheduled";
170 case -EFBIG:
171 return "too many isochronous frames requested";
172 case -EMSGSIZE:
173 return "endpoint message size zero";
174 case -ESHUTDOWN:
175 return "endpoint shutdown";
176 case -EBUSY:
177 return "another request pending";
178 default:
179 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", status);
180 return unkmsg;
181 }
182}
183
184/* usb_pipetype_str
185 * retrieve string representation of USB pipe type
186 */
187static inline char *usb_pipetype_str(int pipe)
188{
189 if (usb_pipeisoc(pipe))
190 return "Isoc";
191 if (usb_pipeint(pipe))
192 return "Int";
193 if (usb_pipecontrol(pipe))
194 return "Ctrl";
195 if (usb_pipebulk(pipe))
196 return "Bulk";
197 return "?";
198}
199
200/* dump_urb
201 * write content of URB to syslog for debugging
202 */
203static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700204 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800205{
206#ifdef CONFIG_GIGASET_DEBUG
207 int i;
208 IFNULLRET(tag);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700209 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800210 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700211 gig_dbg(level,
212 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
213 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
214 (unsigned long) urb->dev,
215 usb_pipetype_str(urb->pipe),
216 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
217 usb_pipein(urb->pipe) ? "in" : "out",
218 urb->status, (unsigned long) urb->hcpriv,
219 urb->transfer_flags);
220 gig_dbg(level,
221 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
222 "bandwidth=%d, setup_packet=0x%08lx,",
223 (unsigned long) urb->transfer_buffer,
224 urb->transfer_buffer_length, urb->actual_length,
225 urb->bandwidth, (unsigned long) urb->setup_packet);
226 gig_dbg(level,
227 " start_frame=%d, number_of_packets=%d, interval=%d, "
228 "error_count=%d,",
229 urb->start_frame, urb->number_of_packets, urb->interval,
230 urb->error_count);
231 gig_dbg(level,
232 " context=0x%08lx, complete=0x%08lx, "
233 "iso_frame_desc[]={",
234 (unsigned long) urb->context,
235 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800236 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700237 struct usb_iso_packet_descriptor *pifd
238 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700239 gig_dbg(level,
240 " {offset=%u, length=%u, actual_length=%u, "
241 "status=%u}",
242 pifd->offset, pifd->length, pifd->actual_length,
243 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800244 }
245 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700246 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800247#endif
248}
249
250/* read/set modem control bits etc. (m10x only) */
251static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700252 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800253{
254 return -EINVAL;
255}
256
257static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
258{
259 return -EINVAL;
260}
261
262static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
263{
264 return -EINVAL;
265}
266
267/* error_hangup
268 * hang up any existing connection because of an unrecoverable error
269 * This function may be called from any context and takes care of scheduling
270 * the necessary actions for execution outside of interrupt context.
271 * argument:
272 * B channel control structure
273 */
274static inline void error_hangup(struct bc_state *bcs)
275{
276 struct cardstate *cs = bcs->cs;
277
Tilman Schmidt784d5852006-04-10 22:55:04 -0700278 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
279 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800280
281 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
282 //FIXME what should we do?
283 return;
284 }
285
286 gigaset_schedule_event(cs);
287}
288
289/* error_reset
290 * reset Gigaset device because of an unrecoverable error
291 * This function may be called from any context and takes care of scheduling
292 * the necessary actions for execution outside of interrupt context.
293 * argument:
294 * controller state structure
295 */
296static inline void error_reset(struct cardstate *cs)
297{
298 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700299 dev_err(cs->dev,
300 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800301}
302
303/* check_pending
304 * check for completion of pending control request
305 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700306 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800307 */
308static void check_pending(struct bas_cardstate *ucs)
309{
310 unsigned long flags;
311
312 IFNULLRET(ucs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800313
314 spin_lock_irqsave(&ucs->lock, flags);
315 switch (ucs->pending) {
316 case 0:
317 break;
318 case HD_OPEN_ATCHANNEL:
319 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
320 ucs->pending = 0;
321 break;
322 case HD_OPEN_B1CHANNEL:
323 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
324 ucs->pending = 0;
325 break;
326 case HD_OPEN_B2CHANNEL:
327 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
328 ucs->pending = 0;
329 break;
330 case HD_CLOSE_ATCHANNEL:
331 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
332 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800333 break;
334 case HD_CLOSE_B1CHANNEL:
335 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
336 ucs->pending = 0;
337 break;
338 case HD_CLOSE_B2CHANNEL:
339 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
340 ucs->pending = 0;
341 break;
342 case HD_DEVICE_INIT_ACK: /* no reply expected */
343 ucs->pending = 0;
344 break;
345 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
346 * are handled separately and should never end up here
347 */
348 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700349 dev_warn(&ucs->interface->dev,
350 "unknown pending request 0x%02x cleared\n",
351 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800352 ucs->pending = 0;
353 }
354
355 if (!ucs->pending)
356 del_timer(&ucs->timer_ctrl);
357
358 spin_unlock_irqrestore(&ucs->lock, flags);
359}
360
361/* cmd_in_timeout
362 * timeout routine for command input request
363 * argument:
364 * controller state structure
365 */
366static void cmd_in_timeout(unsigned long data)
367{
368 struct cardstate *cs = (struct cardstate *) data;
369 struct bas_cardstate *ucs;
370 unsigned long flags;
371
372 IFNULLRET(cs);
373 ucs = cs->hw.bas;
374 IFNULLRET(ucs);
375
376 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700377 if (unlikely(!atomic_read(&cs->connected))) {
378 gig_dbg(DEBUG_USBREQ, "%s: disconnected", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800379 spin_unlock_irqrestore(&cs->lock, flags);
380 return;
381 }
382 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700383 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800384 spin_unlock_irqrestore(&cs->lock, flags);
385 return;
386 }
387 spin_unlock_irqrestore(&cs->lock, flags);
388
Tilman Schmidt784d5852006-04-10 22:55:04 -0700389 dev_err(cs->dev, "timeout reading AT response\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800390 error_reset(cs); //FIXME retry?
391}
392
393
394static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs);
395
396/* atread_submit
397 * submit an HD_READ_ATMESSAGE command URB
398 * parameters:
399 * cs controller state structure
400 * timeout timeout in 1/10 sec., 0: none
401 * return value:
402 * 0 on success
403 * -EINVAL if a NULL pointer is encountered somewhere
404 * -EBUSY if another request is pending
405 * any URB submission error code
406 */
407static int atread_submit(struct cardstate *cs, int timeout)
408{
409 struct bas_cardstate *ucs;
410 int ret;
411
412 IFNULLRETVAL(cs, -EINVAL);
413 ucs = cs->hw.bas;
414 IFNULLRETVAL(ucs, -EINVAL);
415 IFNULLRETVAL(ucs->urb_cmd_in, -EINVAL);
416
Tilman Schmidt784d5852006-04-10 22:55:04 -0700417 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
418 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800419
420 if (ucs->urb_cmd_in->status == -EINPROGRESS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700421 dev_err(cs->dev,
422 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800423 return -EBUSY;
424 }
425
426 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
427 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
428 ucs->dr_cmd_in.wValue = 0;
429 ucs->dr_cmd_in.wIndex = 0;
430 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
431 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700432 usb_rcvctrlpipe(ucs->udev, 0),
433 (unsigned char*) & ucs->dr_cmd_in,
434 ucs->rcvbuf, ucs->rcvbuf_size,
435 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800436
437 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700438 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
439 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800440 return ret;
441 }
442
443 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700444 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800445 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
446 ucs->timer_cmd_in.data = (unsigned long) cs;
447 ucs->timer_cmd_in.function = cmd_in_timeout;
448 add_timer(&ucs->timer_cmd_in);
449 }
450 return 0;
451}
452
453static void stopurbs(struct bas_bc_state *);
454static int start_cbsend(struct cardstate *);
455
456/* set/clear bits in base connection state
457 */
458inline static void update_basstate(struct bas_cardstate *ucs,
459 int set, int clear)
460{
461 unsigned long flags;
462 int state;
463
464 spin_lock_irqsave(&ucs->lock, flags);
465 state = atomic_read(&ucs->basstate);
466 state &= ~clear;
467 state |= set;
468 atomic_set(&ucs->basstate, state);
469 spin_unlock_irqrestore(&ucs->lock, flags);
470}
471
472
473/* read_int_callback
474 * USB completion handler for interrupt pipe input
475 * called by the USB subsystem in interrupt context
476 * parameter:
477 * urb USB request block
478 * urb->context = controller state structure
479 */
480static void read_int_callback(struct urb *urb, struct pt_regs *regs)
481{
482 struct cardstate *cs;
483 struct bas_cardstate *ucs;
484 struct bc_state *bcs;
485 unsigned long flags;
486 int status;
487 unsigned l;
488 int channel;
489
490 IFNULLRET(urb);
491 cs = (struct cardstate *) urb->context;
492 IFNULLRET(cs);
493 ucs = cs->hw.bas;
494 IFNULLRET(ucs);
495
496 if (unlikely(!atomic_read(&cs->connected))) {
497 warn("%s: disconnected", __func__);
498 return;
499 }
500
501 switch (urb->status) {
502 case 0: /* success */
503 break;
504 case -ENOENT: /* canceled */
505 case -ECONNRESET: /* canceled (async) */
506 case -EINPROGRESS: /* pending */
507 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700508 gig_dbg(DEBUG_USBREQ, "%s: %s",
509 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800510 return;
511 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700512 dev_warn(cs->dev, "interrupt read: %s\n",
513 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800514 //FIXME corrective action? resubmission always ok?
515 goto resubmit;
516 }
517
518 l = (unsigned) ucs->int_in_buf[1] +
519 (((unsigned) ucs->int_in_buf[2]) << 8);
520
Tilman Schmidt784d5852006-04-10 22:55:04 -0700521 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
522 urb->actual_length, (int)ucs->int_in_buf[0], l,
523 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800524
525 channel = 0;
526
527 switch (ucs->int_in_buf[0]) {
528 case HD_DEVICE_INIT_OK:
529 update_basstate(ucs, BS_INIT, 0);
530 break;
531
532 case HD_READY_SEND_ATDATA:
533 del_timer(&ucs->timer_atrdy);
534 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
535 start_cbsend(cs);
536 break;
537
538 case HD_OPEN_B2CHANNEL_ACK:
539 ++channel;
540 case HD_OPEN_B1CHANNEL_ACK:
541 bcs = cs->bcs + channel;
542 update_basstate(ucs, BS_B1OPEN << channel, 0);
543 gigaset_bchannel_up(bcs);
544 break;
545
546 case HD_OPEN_ATCHANNEL_ACK:
547 update_basstate(ucs, BS_ATOPEN, 0);
548 start_cbsend(cs);
549 break;
550
551 case HD_CLOSE_B2CHANNEL_ACK:
552 ++channel;
553 case HD_CLOSE_B1CHANNEL_ACK:
554 bcs = cs->bcs + channel;
555 update_basstate(ucs, 0, BS_B1OPEN << channel);
556 stopurbs(bcs->hw.bas);
557 gigaset_bchannel_down(bcs);
558 break;
559
560 case HD_CLOSE_ATCHANNEL_ACK:
561 update_basstate(ucs, 0, BS_ATOPEN);
562 break;
563
564 case HD_B2_FLOW_CONTROL:
565 ++channel;
566 case HD_B1_FLOW_CONTROL:
567 bcs = cs->bcs + channel;
568 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700569 &bcs->hw.bas->corrbytes);
570 gig_dbg(DEBUG_ISO,
571 "Flow control (channel %d, sub %d): 0x%02x => %d",
572 channel, bcs->hw.bas->numsub, l,
573 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800574 break;
575
576 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
577 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 dev_warn(cs->dev,
579 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800580 break;
581 }
582 spin_lock_irqsave(&cs->lock, flags);
583 if (ucs->rcvbuf_size) {
584 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700585 dev_err(cs->dev,
586 "receive AT data overrun, %d bytes lost\n", l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800587 error_reset(cs); //FIXME reschedule
588 break;
589 }
590 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
591 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700592 dev_err(cs->dev, "out of memory, %d bytes lost\n", l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800593 error_reset(cs); //FIXME reschedule
594 break;
595 }
596 ucs->rcvbuf_size = l;
597 ucs->retry_cmd_in = 0;
598 if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
599 kfree(ucs->rcvbuf);
600 ucs->rcvbuf = NULL;
601 ucs->rcvbuf_size = 0;
602 error_reset(cs); //FIXME reschedule
603 }
604 spin_unlock_irqrestore(&cs->lock, flags);
605 break;
606
607 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700608 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800609 break;
610
611 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700612 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800613 break;
614
615 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700616 dev_warn(cs->dev,
617 "unknown Gigaset signal 0x%02x (%u) ignored\n",
618 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800619 }
620
621 check_pending(ucs);
622
623resubmit:
624 status = usb_submit_urb(urb, SLAB_ATOMIC);
625 if (unlikely(status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700626 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
627 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800628 error_reset(cs);
629 }
630}
631
632/* read_ctrl_callback
633 * USB completion handler for control pipe input
634 * called by the USB subsystem in interrupt context
635 * parameter:
636 * urb USB request block
637 * urb->context = inbuf structure for controller state
638 */
639static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
640{
641 struct cardstate *cs;
642 struct bas_cardstate *ucs;
643 unsigned numbytes;
644 unsigned long flags;
645 struct inbuf_t *inbuf;
646 int have_data = 0;
647
648 IFNULLRET(urb);
649 inbuf = (struct inbuf_t *) urb->context;
650 IFNULLRET(inbuf);
651 cs = inbuf->cs;
652 IFNULLRET(cs);
653 ucs = cs->hw.bas;
654 IFNULLRET(ucs);
655
656 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700657 if (unlikely(!atomic_read(&cs->connected))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800658 warn("%s: disconnected", __func__);
659 spin_unlock_irqrestore(&cs->lock, flags);
660 return;
661 }
662
663 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700664 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800665 spin_unlock_irqrestore(&cs->lock, flags);
666 return;
667 }
668
669 del_timer(&ucs->timer_cmd_in);
670
671 switch (urb->status) {
672 case 0: /* normal completion */
673 numbytes = urb->actual_length;
674 if (unlikely(numbytes == 0)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700675 dev_warn(cs->dev,
676 "control read: empty block received\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800677 goto retry;
678 }
679 if (unlikely(numbytes != ucs->rcvbuf_size)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700680 dev_warn(cs->dev,
681 "control read: received %d chars, expected %d\n",
682 numbytes, ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800683 if (numbytes > ucs->rcvbuf_size)
684 numbytes = ucs->rcvbuf_size;
685 }
686
687 /* copy received bytes to inbuf */
688 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
689
690 if (unlikely(numbytes < ucs->rcvbuf_size)) {
691 /* incomplete - resubmit for remaining bytes */
692 ucs->rcvbuf_size -= numbytes;
693 ucs->retry_cmd_in = 0;
694 goto retry;
695 }
696 break;
697
698 case -ENOENT: /* canceled */
699 case -ECONNRESET: /* canceled (async) */
700 case -EINPROGRESS: /* pending */
701 /* no action necessary */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700702 gig_dbg(DEBUG_USBREQ, "%s: %s",
703 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800704 break;
705
706 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700707 dev_warn(cs->dev, "control read: %s\n",
708 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800709 retry:
710 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700711 dev_notice(cs->dev, "control read: retry %d\n",
712 ucs->retry_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800713 if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
714 /* resubmitted - bypass regular exit block */
715 spin_unlock_irqrestore(&cs->lock, flags);
716 return;
717 }
718 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700719 dev_err(cs->dev,
720 "control read: giving up after %d tries\n",
721 ucs->retry_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800722 }
723 error_reset(cs);
724 }
725
726 kfree(ucs->rcvbuf);
727 ucs->rcvbuf = NULL;
728 ucs->rcvbuf_size = 0;
729 spin_unlock_irqrestore(&cs->lock, flags);
730 if (have_data) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700731 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800732 gigaset_schedule_event(cs);
733 }
734}
735
736/* read_iso_callback
737 * USB completion handler for B channel isochronous input
738 * called by the USB subsystem in interrupt context
739 * parameter:
740 * urb USB request block of completed request
741 * urb->context = bc_state structure
742 */
743static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
744{
745 struct bc_state *bcs;
746 struct bas_bc_state *ubc;
747 unsigned long flags;
748 int i, rc;
749
750 IFNULLRET(urb);
751 IFNULLRET(urb->context);
752 IFNULLRET(cardstate);
753
754 /* status codes not worth bothering the tasklet with */
755 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
Tilman Schmidt784d5852006-04-10 22:55:04 -0700756 urb->status == -EINPROGRESS)) {
757 gig_dbg(DEBUG_ISO, "%s: %s",
758 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800759 return;
760 }
761
762 bcs = (struct bc_state *) urb->context;
763 ubc = bcs->hw.bas;
764 IFNULLRET(ubc);
765
766 spin_lock_irqsave(&ubc->isoinlock, flags);
767 if (likely(ubc->isoindone == NULL)) {
768 /* pass URB to tasklet */
769 ubc->isoindone = urb;
770 tasklet_schedule(&ubc->rcvd_tasklet);
771 } else {
772 /* tasklet still busy, drop data and resubmit URB */
773 ubc->loststatus = urb->status;
774 for (i = 0; i < BAS_NUMFRAMES; i++) {
775 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
776 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
777 urb->iso_frame_desc[i].status != -EINPROGRESS)) {
778 ubc->loststatus = urb->iso_frame_desc[i].status;
779 }
780 urb->iso_frame_desc[i].status = 0;
781 urb->iso_frame_desc[i].actual_length = 0;
782 }
783 if (likely(atomic_read(&ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700784 /* urb->dev is clobbered by USB subsystem */
785 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800786 urb->transfer_flags = URB_ISO_ASAP;
787 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700788 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
789 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800790 rc = usb_submit_urb(urb, SLAB_ATOMIC);
791 if (unlikely(rc != 0)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700792 dev_err(bcs->cs->dev,
793 "could not resubmit isochronous read "
794 "URB: %s\n", get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800795 dump_urb(DEBUG_ISO, "isoc read", urb);
796 error_hangup(bcs);
797 }
798 }
799 }
800 spin_unlock_irqrestore(&ubc->isoinlock, flags);
801}
802
803/* write_iso_callback
804 * USB completion handler for B channel isochronous output
805 * called by the USB subsystem in interrupt context
806 * parameter:
807 * urb USB request block of completed request
808 * urb->context = isow_urbctx_t structure
809 */
810static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
811{
812 struct isow_urbctx_t *ucx;
813 struct bas_bc_state *ubc;
814 unsigned long flags;
815
816 IFNULLRET(urb);
817 IFNULLRET(urb->context);
818 IFNULLRET(cardstate);
819
820 /* status codes not worth bothering the tasklet with */
821 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
Tilman Schmidt784d5852006-04-10 22:55:04 -0700822 urb->status == -EINPROGRESS)) {
823 gig_dbg(DEBUG_ISO, "%s: %s",
824 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800825 return;
826 }
827
828 /* pass URB context to tasklet */
829 ucx = (struct isow_urbctx_t *) urb->context;
830 IFNULLRET(ucx->bcs);
831 ubc = ucx->bcs->hw.bas;
832 IFNULLRET(ubc);
833
834 spin_lock_irqsave(&ubc->isooutlock, flags);
835 ubc->isooutovfl = ubc->isooutdone;
836 ubc->isooutdone = ucx;
837 spin_unlock_irqrestore(&ubc->isooutlock, flags);
838 tasklet_schedule(&ubc->sent_tasklet);
839}
840
841/* starturbs
842 * prepare and submit USB request blocks for isochronous input and output
843 * argument:
844 * B channel control structure
845 * return value:
846 * 0 on success
847 * < 0 on error (no URBs submitted)
848 */
849static int starturbs(struct bc_state *bcs)
850{
851 struct urb *urb;
852 struct bas_bc_state *ubc;
853 int j, k;
854 int rc;
855
856 IFNULLRETVAL(bcs, -EFAULT);
857 ubc = bcs->hw.bas;
858 IFNULLRETVAL(ubc, -EFAULT);
859
860 /* initialize L2 reception */
861 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
862 bcs->inputstate |= INS_flag_hunt;
863
864 /* submit all isochronous input URBs */
865 atomic_set(&ubc->running, 1);
866 for (k = 0; k < BAS_INURBS; k++) {
867 urb = ubc->isoinurbs[k];
868 if (!urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700869 dev_err(bcs->cs->dev, "isoinurbs[%d]==NULL\n", k);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800870 rc = -EFAULT;
871 goto error;
872 }
873
874 urb->dev = bcs->cs->hw.bas->udev;
875 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
876 urb->transfer_flags = URB_ISO_ASAP;
877 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
878 urb->transfer_buffer_length = BAS_INBUFSIZE;
879 urb->number_of_packets = BAS_NUMFRAMES;
880 urb->interval = BAS_FRAMETIME;
881 urb->complete = read_iso_callback;
882 urb->context = bcs;
883 for (j = 0; j < BAS_NUMFRAMES; j++) {
884 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
885 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
886 urb->iso_frame_desc[j].status = 0;
887 urb->iso_frame_desc[j].actual_length = 0;
888 }
889
890 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
891 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700892 dev_err(bcs->cs->dev,
893 "could not submit isochronous read URB %d: %s\n",
894 k, get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800895 goto error;
896 }
897 }
898
899 /* initialize L2 transmission */
900 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
901
902 /* set up isochronous output URBs for flag idling */
903 for (k = 0; k < BAS_OUTURBS; ++k) {
904 urb = ubc->isoouturbs[k].urb;
905 if (!urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700906 dev_err(bcs->cs->dev, "isoouturbs[%d].urb==NULL\n", k);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800907 rc = -EFAULT;
908 goto error;
909 }
910 urb->dev = bcs->cs->hw.bas->udev;
911 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
912 urb->transfer_flags = URB_ISO_ASAP;
913 urb->transfer_buffer = ubc->isooutbuf->data;
914 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
915 urb->number_of_packets = BAS_NUMFRAMES;
916 urb->interval = BAS_FRAMETIME;
917 urb->complete = write_iso_callback;
918 urb->context = &ubc->isoouturbs[k];
919 for (j = 0; j < BAS_NUMFRAMES; ++j) {
920 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
921 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
922 urb->iso_frame_desc[j].status = 0;
923 urb->iso_frame_desc[j].actual_length = 0;
924 }
925 ubc->isoouturbs[k].limit = -1;
926 }
927
928 /* submit two URBs, keep third one */
929 for (k = 0; k < 2; ++k) {
930 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
931 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
932 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700933 dev_err(bcs->cs->dev,
934 "could not submit isochronous write URB %d: %s\n",
935 k, get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800936 goto error;
937 }
938 }
939 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
940 ubc->isooutfree = &ubc->isoouturbs[2];
941 ubc->isooutdone = ubc->isooutovfl = NULL;
942 return 0;
943 error:
944 stopurbs(ubc);
945 return rc;
946}
947
948/* stopurbs
949 * cancel the USB request blocks for isochronous input and output
950 * errors are silently ignored
951 * argument:
952 * B channel control structure
953 */
954static void stopurbs(struct bas_bc_state *ubc)
955{
956 int k, rc;
957
958 IFNULLRET(ubc);
959
960 atomic_set(&ubc->running, 0);
961
962 for (k = 0; k < BAS_INURBS; ++k) {
963 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700964 gig_dbg(DEBUG_ISO,
965 "%s: isoc input URB %d unlinked, result = %d",
966 __func__, k, rc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800967 }
968
969 for (k = 0; k < BAS_OUTURBS; ++k) {
970 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700971 gig_dbg(DEBUG_ISO,
972 "%s: isoc output URB %d unlinked, result = %d",
973 __func__, k, rc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800974 }
975}
976
977/* Isochronous Write - Bottom Half */
978/* =============================== */
979
980/* submit_iso_write_urb
981 * fill and submit the next isochronous write URB
982 * parameters:
983 * bcs B channel state structure
984 * return value:
985 * number of frames submitted in URB
986 * 0 if URB not submitted because no data available (isooutbuf busy)
987 * error code < 0 on error
988 */
989static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
990{
991 struct urb *urb;
992 struct bas_bc_state *ubc;
993 struct usb_iso_packet_descriptor *ifd;
994 int corrbytes, nframe, rc;
995
996 IFNULLRETVAL(ucx, -EFAULT);
997 urb = ucx->urb;
998 IFNULLRETVAL(urb, -EFAULT);
999 IFNULLRETVAL(ucx->bcs, -EFAULT);
1000 ubc = ucx->bcs->hw.bas;
1001 IFNULLRETVAL(ubc, -EFAULT);
1002
Tilman Schmidt784d5852006-04-10 22:55:04 -07001003 /* urb->dev is clobbered by USB subsystem */
1004 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001005 urb->transfer_flags = URB_ISO_ASAP;
1006 urb->transfer_buffer = ubc->isooutbuf->data;
1007 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1008
1009 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1010 ifd = &urb->iso_frame_desc[nframe];
1011
1012 /* compute frame length according to flow control */
1013 ifd->length = BAS_NORMFRAME;
1014 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001015 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1016 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001017 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1018 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1019 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1020 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1021 ifd->length += corrbytes;
1022 atomic_add(-corrbytes, &ubc->corrbytes);
1023 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001024
1025 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -07001026 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1027 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001028 if (ifd->offset < 0) {
1029 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001030 gig_dbg(DEBUG_ISO,
1031 "%s: buffer busy at frame %d",
1032 __func__, nframe);
1033 /* tasklet will be restarted from
1034 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001035 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001036 dev_err(ucx->bcs->cs->dev,
1037 "%s: buffer error %d at frame %d\n",
1038 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001039 return ifd->offset;
1040 }
1041 break;
1042 }
1043 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1044 ifd->status = 0;
1045 ifd->actual_length = 0;
1046 }
1047 if ((urb->number_of_packets = nframe) > 0) {
1048 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001049 dev_err(ucx->bcs->cs->dev,
1050 "could not submit isochronous write URB: %s\n",
1051 get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001052 dump_urb(DEBUG_ISO, "isoc write", urb);
1053 return rc;
1054 }
1055 ++ubc->numsub;
1056 }
1057 return nframe;
1058}
1059
1060/* write_iso_tasklet
1061 * tasklet scheduled when an isochronous output URB from the Gigaset device
1062 * has completed
1063 * parameter:
1064 * data B channel state structure
1065 */
1066static void write_iso_tasklet(unsigned long data)
1067{
1068 struct bc_state *bcs;
1069 struct bas_bc_state *ubc;
1070 struct cardstate *cs;
1071 struct isow_urbctx_t *done, *next, *ovfl;
1072 struct urb *urb;
1073 struct usb_iso_packet_descriptor *ifd;
1074 int offset;
1075 unsigned long flags;
1076 int i;
1077 struct sk_buff *skb;
1078 int len;
1079
1080 bcs = (struct bc_state *) data;
1081 IFNULLRET(bcs);
1082 ubc = bcs->hw.bas;
1083 IFNULLRET(ubc);
1084 cs = bcs->cs;
1085 IFNULLRET(cs);
1086
1087 /* loop while completed URBs arrive in time */
1088 for (;;) {
1089 if (unlikely(!atomic_read(&cs->connected))) {
1090 warn("%s: disconnected", __func__);
1091 return;
1092 }
1093
1094 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001095 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001096 return;
1097 }
1098
1099 /* retrieve completed URBs */
1100 spin_lock_irqsave(&ubc->isooutlock, flags);
1101 done = ubc->isooutdone;
1102 ubc->isooutdone = NULL;
1103 ovfl = ubc->isooutovfl;
1104 ubc->isooutovfl = NULL;
1105 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1106 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001107 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001108 error_hangup(bcs);
1109 break;
1110 }
1111 if (!done)
1112 break;
1113
1114 /* submit free URB if available */
1115 spin_lock_irqsave(&ubc->isooutlock, flags);
1116 next = ubc->isooutfree;
1117 ubc->isooutfree = NULL;
1118 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1119 if (next) {
1120 if (submit_iso_write_urb(next) <= 0) {
1121 /* could not submit URB, put it back */
1122 spin_lock_irqsave(&ubc->isooutlock, flags);
1123 if (ubc->isooutfree == NULL) {
1124 ubc->isooutfree = next;
1125 next = NULL;
1126 }
1127 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1128 if (next) {
1129 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001130 dev_err(cs->dev,
1131 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001132 error_hangup(bcs);
1133 }
1134 }
1135 }
1136
1137 /* process completed URB */
1138 urb = done->urb;
1139 switch (urb->status) {
1140 case 0: /* normal completion */
1141 break;
1142 case -EXDEV: /* inspect individual frames */
1143 /* assumptions (for lack of documentation):
Tilman Schmidt917f5082006-04-10 22:55:00 -07001144 * - actual_length bytes of the frame in error are
1145 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001146 * - all following frames are not sent at all
1147 */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001148 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1149 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001150 offset = done->limit; /* just in case */
1151 for (i = 0; i < BAS_NUMFRAMES; i++) {
1152 ifd = &urb->iso_frame_desc[i];
1153 if (ifd->status ||
1154 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001155 dev_warn(cs->dev,
1156 "isochronous write: frame %d: %s, "
1157 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001158 i, get_usb_statmsg(ifd->status),
1159 ifd->actual_length, ifd->length);
1160 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001161 ifd->actual_length)
1162 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001163 break;
1164 }
1165 }
1166#ifdef CONFIG_GIGASET_DEBUG
1167 /* check assumption on remaining frames */
1168 for (; i < BAS_NUMFRAMES; i++) {
1169 ifd = &urb->iso_frame_desc[i];
1170 if (ifd->status != -EINPROGRESS
1171 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001172 dev_warn(cs->dev,
1173 "isochronous write: frame %d: %s, "
1174 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001175 i, get_usb_statmsg(ifd->status),
1176 ifd->actual_length, ifd->length);
1177 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001178 ifd->actual_length)
1179 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001180 break;
1181 }
1182 }
1183#endif
1184 break;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001185 case -EPIPE: //FIXME is this the code for "underrun"?
1186 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001187 error_hangup(bcs);
1188 break;
1189 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001190 dev_warn(cs->dev, "isochronous write: %s\n",
1191 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001192 }
1193
1194 /* mark the write buffer area covered by this URB as free */
1195 if (done->limit >= 0)
1196 atomic_set(&ubc->isooutbuf->read, done->limit);
1197
1198 /* mark URB as free */
1199 spin_lock_irqsave(&ubc->isooutlock, flags);
1200 next = ubc->isooutfree;
1201 ubc->isooutfree = done;
1202 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1203 if (next) {
1204 /* only one URB still active - resubmit one */
1205 if (submit_iso_write_urb(next) <= 0) {
1206 /* couldn't submit */
1207 error_hangup(bcs);
1208 }
1209 }
1210 }
1211
1212 /* process queued SKBs */
1213 while ((skb = skb_dequeue(&bcs->squeue))) {
1214 /* copy to output buffer, doing L2 encapsulation */
1215 len = skb->len;
1216 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1217 /* insufficient buffer space, push back onto queue */
1218 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001219 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1220 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001221 break;
1222 }
1223 skb_pull(skb, len);
1224 gigaset_skb_sent(bcs, skb);
1225 dev_kfree_skb_any(skb);
1226 }
1227}
1228
1229/* Isochronous Read - Bottom Half */
1230/* ============================== */
1231
1232/* read_iso_tasklet
1233 * tasklet scheduled when an isochronous input URB from the Gigaset device
1234 * has completed
1235 * parameter:
1236 * data B channel state structure
1237 */
1238static void read_iso_tasklet(unsigned long data)
1239{
1240 struct bc_state *bcs;
1241 struct bas_bc_state *ubc;
1242 struct cardstate *cs;
1243 struct urb *urb;
1244 char *rcvbuf;
1245 unsigned long flags;
1246 int totleft, numbytes, offset, frame, rc;
1247
1248 bcs = (struct bc_state *) data;
1249 IFNULLRET(bcs);
1250 ubc = bcs->hw.bas;
1251 IFNULLRET(ubc);
1252 cs = bcs->cs;
1253 IFNULLRET(cs);
1254
1255 /* loop while more completed URBs arrive in the meantime */
1256 for (;;) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001257 if (unlikely(!atomic_read(&cs->connected))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001258 warn("%s: disconnected", __func__);
1259 return;
1260 }
1261
1262 /* retrieve URB */
1263 spin_lock_irqsave(&ubc->isoinlock, flags);
1264 if (!(urb = ubc->isoindone)) {
1265 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1266 return;
1267 }
1268 ubc->isoindone = NULL;
1269 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001270 dev_warn(cs->dev,
1271 "isochronous read overrun, "
1272 "dropped URB with status: %s, %d bytes lost\n",
1273 get_usb_statmsg(ubc->loststatus),
1274 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001275 ubc->loststatus = -EINPROGRESS;
1276 }
1277 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1278
1279 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001280 gig_dbg(DEBUG_ISO,
1281 "%s: channel not running, "
1282 "dropped URB with status: %s",
1283 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001284 return;
1285 }
1286
1287 switch (urb->status) {
1288 case 0: /* normal completion */
1289 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001290 case -EXDEV: /* inspect individual frames
1291 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001292 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1293 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001294 break;
1295 case -ENOENT:
1296 case -ECONNRESET:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001297 gig_dbg(DEBUG_ISO, "%s: URB canceled", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001298 continue; /* -> skip */
1299 case -EINPROGRESS: /* huh? */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001300 gig_dbg(DEBUG_ISO, "%s: URB still pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001301 continue; /* -> skip */
1302 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001303 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001304 error_hangup(bcs);
1305 continue; /* -> skip */
1306 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001307 dev_warn(cs->dev, "isochronous read: %s\n",
1308 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001309 goto error;
1310 }
1311
1312 rcvbuf = urb->transfer_buffer;
1313 totleft = urb->actual_length;
1314 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1315 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001316 dev_warn(cs->dev,
1317 "isochronous read: frame %d: %s\n",
1318 frame,
1319 get_usb_statmsg(
1320 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001321 break;
1322 }
1323 numbytes = urb->iso_frame_desc[frame].actual_length;
1324 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001325 dev_warn(cs->dev,
1326 "isochronous read: frame %d: "
1327 "numbytes (%d) > BAS_MAXFRAME\n",
1328 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001329 break;
1330 }
1331 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001332 dev_warn(cs->dev,
1333 "isochronous read: frame %d: "
1334 "numbytes (%d) > totleft (%d)\n",
1335 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001336 break;
1337 }
1338 offset = urb->iso_frame_desc[frame].offset;
1339 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001340 dev_warn(cs->dev,
1341 "isochronous read: frame %d: "
1342 "offset (%d) + numbytes (%d) "
1343 "> BAS_INBUFSIZE\n",
1344 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001345 break;
1346 }
1347 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1348 totleft -= numbytes;
1349 }
1350 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001351 dev_warn(cs->dev,
1352 "isochronous read: %d data bytes missing\n",
1353 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001354
1355 error:
1356 /* URB processed, resubmit */
1357 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1358 urb->iso_frame_desc[frame].status = 0;
1359 urb->iso_frame_desc[frame].actual_length = 0;
1360 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001361 /* urb->dev is clobbered by USB subsystem */
1362 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001363 urb->transfer_flags = URB_ISO_ASAP;
1364 urb->number_of_packets = BAS_NUMFRAMES;
1365 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001366 dev_err(cs->dev,
1367 "could not resubmit isochronous read URB: %s\n",
1368 get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001369 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1370 error_hangup(bcs);
1371 }
1372 }
1373}
1374
1375/* Channel Operations */
1376/* ================== */
1377
1378/* req_timeout
1379 * timeout routine for control output request
1380 * argument:
1381 * B channel control structure
1382 */
1383static void req_timeout(unsigned long data)
1384{
1385 struct bc_state *bcs = (struct bc_state *) data;
1386 struct bas_cardstate *ucs;
1387 int pending;
1388 unsigned long flags;
1389
1390 IFNULLRET(bcs);
1391 IFNULLRET(bcs->cs);
1392 ucs = bcs->cs->hw.bas;
1393 IFNULLRET(ucs);
1394
1395 check_pending(ucs);
1396
1397 spin_lock_irqsave(&ucs->lock, flags);
1398 pending = ucs->pending;
1399 ucs->pending = 0;
1400 spin_unlock_irqrestore(&ucs->lock, flags);
1401
1402 switch (pending) {
1403 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001404 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001405 break;
1406
1407 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001408 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001409 error_reset(bcs->cs);
1410 break;
1411
1412 case HD_OPEN_B2CHANNEL:
1413 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001414 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1415 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001416 error_hangup(bcs);
1417 break;
1418
1419 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001420 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001421 break;
1422
1423 case HD_CLOSE_B2CHANNEL:
1424 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001425 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1426 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001427 break;
1428
1429 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001430 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1431 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001432 }
1433}
1434
1435/* write_ctrl_callback
1436 * USB completion handler for control pipe output
1437 * called by the USB subsystem in interrupt context
1438 * parameter:
1439 * urb USB request block of completed request
1440 * urb->context = hardware specific controller state structure
1441 */
1442static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1443{
1444 struct bas_cardstate *ucs;
1445 unsigned long flags;
1446
1447 IFNULLRET(urb);
1448 IFNULLRET(urb->context);
1449 IFNULLRET(cardstate);
1450
1451 ucs = (struct bas_cardstate *) urb->context;
1452 spin_lock_irqsave(&ucs->lock, flags);
1453 if (urb->status && ucs->pending) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001454 dev_err(&ucs->interface->dev,
1455 "control request 0x%02x failed: %s\n",
1456 ucs->pending, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001457 del_timer(&ucs->timer_ctrl);
1458 ucs->pending = 0;
1459 }
1460 /* individual handling of specific request types */
1461 switch (ucs->pending) {
1462 case HD_DEVICE_INIT_ACK: /* no reply expected */
1463 ucs->pending = 0;
1464 break;
1465 }
1466 spin_unlock_irqrestore(&ucs->lock, flags);
1467}
1468
1469/* req_submit
1470 * submit a control output request without message buffer to the Gigaset base
1471 * and optionally start a timeout
1472 * parameters:
1473 * bcs B channel control structure
1474 * req control request code (HD_*)
1475 * val control request parameter value (set to 0 if unused)
1476 * timeout timeout in seconds (0: no timeout)
1477 * return value:
1478 * 0 on success
1479 * -EINVAL if a NULL pointer is encountered somewhere
1480 * -EBUSY if another request is pending
1481 * any URB submission error code
1482 */
1483static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1484{
1485 struct bas_cardstate *ucs;
1486 int ret;
1487 unsigned long flags;
1488
1489 IFNULLRETVAL(bcs, -EINVAL);
1490 IFNULLRETVAL(bcs->cs, -EINVAL);
1491 ucs = bcs->cs->hw.bas;
1492 IFNULLRETVAL(ucs, -EINVAL);
1493 IFNULLRETVAL(ucs->urb_ctrl, -EINVAL);
1494
Tilman Schmidt784d5852006-04-10 22:55:04 -07001495 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001496
1497 spin_lock_irqsave(&ucs->lock, flags);
1498 if (ucs->pending) {
1499 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001500 dev_err(bcs->cs->dev,
1501 "submission of request 0x%02x failed: "
1502 "request 0x%02x still pending\n",
1503 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001504 return -EBUSY;
1505 }
1506 if (ucs->urb_ctrl->status == -EINPROGRESS) {
1507 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001508 dev_err(bcs->cs->dev,
1509 "could not submit request 0x%02x: URB busy\n", req);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001510 return -EBUSY;
1511 }
1512
1513 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1514 ucs->dr_ctrl.bRequest = req;
1515 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1516 ucs->dr_ctrl.wIndex = 0;
1517 ucs->dr_ctrl.wLength = 0;
1518 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001519 usb_sndctrlpipe(ucs->udev, 0),
1520 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1521 write_ctrl_callback, ucs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001522 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001523 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1524 req, get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001525 spin_unlock_irqrestore(&ucs->lock, flags);
1526 return ret;
1527 }
1528 ucs->pending = req;
1529
1530 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001531 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001532 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1533 ucs->timer_ctrl.data = (unsigned long) bcs;
1534 ucs->timer_ctrl.function = req_timeout;
1535 add_timer(&ucs->timer_ctrl);
1536 }
1537
1538 spin_unlock_irqrestore(&ucs->lock, flags);
1539 return 0;
1540}
1541
1542/* gigaset_init_bchannel
1543 * called by common.c to connect a B channel
1544 * initialize isochronous I/O and tell the Gigaset base to open the channel
1545 * argument:
1546 * B channel control structure
1547 * return value:
1548 * 0 on success, error code < 0 on error
1549 */
1550static int gigaset_init_bchannel(struct bc_state *bcs)
1551{
1552 int req, ret;
1553
1554 IFNULLRETVAL(bcs, -EINVAL);
1555
1556 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001557 dev_err(bcs->cs->dev,
1558 "could not start isochronous I/O for channel %d\n",
1559 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001560 error_hangup(bcs);
1561 return ret;
1562 }
1563
1564 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1565 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001566 dev_err(bcs->cs->dev, "could not open channel %d: %s\n",
1567 bcs->channel + 1, get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001568 stopurbs(bcs->hw.bas);
1569 error_hangup(bcs);
1570 }
1571 return ret;
1572}
1573
1574/* gigaset_close_bchannel
1575 * called by common.c to disconnect a B channel
1576 * tell the Gigaset base to close the channel
1577 * stopping isochronous I/O and LL notification will be done when the
1578 * acknowledgement for the close arrives
1579 * argument:
1580 * B channel control structure
1581 * return value:
1582 * 0 on success, error code < 0 on error
1583 */
1584static int gigaset_close_bchannel(struct bc_state *bcs)
1585{
1586 int req, ret;
1587
1588 IFNULLRETVAL(bcs, -EINVAL);
1589
1590 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1591 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1592 /* channel not running: just signal common.c */
1593 gigaset_bchannel_down(bcs);
1594 return 0;
1595 }
1596
1597 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1598 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidt784d5852006-04-10 22:55:04 -07001599 dev_err(bcs->cs->dev,
1600 "could not submit HD_CLOSE_BxCHANNEL request: %s\n",
1601 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001602 return ret;
1603}
1604
1605/* Device Operations */
1606/* ================= */
1607
1608/* complete_cb
1609 * unqueue first command buffer from queue, waking any sleepers
1610 * must be called with cs->cmdlock held
1611 * parameter:
1612 * cs controller state structure
1613 */
1614static void complete_cb(struct cardstate *cs)
1615{
1616 struct cmdbuf_t *cb;
1617
1618 IFNULLRET(cs);
1619 cb = cs->cmdbuf;
1620 IFNULLRET(cb);
1621
1622 /* unqueue completed buffer */
1623 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001624 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1625 "write_command: sent %u bytes, %u left",
1626 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001627 if ((cs->cmdbuf = cb->next) != NULL) {
1628 cs->cmdbuf->prev = NULL;
1629 cs->curlen = cs->cmdbuf->len;
1630 } else {
1631 cs->lastcmdbuf = NULL;
1632 cs->curlen = 0;
1633 }
1634
1635 if (cb->wake_tasklet)
1636 tasklet_schedule(cb->wake_tasklet);
1637
1638 kfree(cb);
1639}
1640
1641static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len);
1642
1643/* write_command_callback
1644 * USB completion handler for AT command transmission
1645 * called by the USB subsystem in interrupt context
1646 * parameter:
1647 * urb USB request block of completed request
1648 * urb->context = controller state structure
1649 */
1650static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1651{
1652 struct cardstate *cs;
1653 unsigned long flags;
1654 struct bas_cardstate *ucs;
1655
1656 IFNULLRET(urb);
1657 cs = (struct cardstate *) urb->context;
1658 IFNULLRET(cs);
1659 ucs = cs->hw.bas;
1660 IFNULLRET(ucs);
1661
1662 /* check status */
1663 switch (urb->status) {
1664 case 0: /* normal completion */
1665 break;
1666 case -ENOENT: /* canceled */
1667 case -ECONNRESET: /* canceled (async) */
1668 case -EINPROGRESS: /* pending */
1669 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001670 gig_dbg(DEBUG_USBREQ, "%s: %s",
1671 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001672 return;
1673 default: /* any failure */
1674 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001675 dev_warn(cs->dev,
1676 "command write: %s, "
1677 "giving up after %d retries\n",
1678 get_usb_statmsg(urb->status),
1679 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001680 break;
1681 }
1682 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001683 dev_warn(cs->dev,
1684 "command write: %s, "
1685 "cannot retry - cmdbuf gone\n",
1686 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001687 break;
1688 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001689 dev_notice(cs->dev, "command write: %s, retry %d\n",
1690 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001691 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1692 /* resubmitted - bypass regular exit block */
1693 return;
1694 /* command send failed, assume base still waiting */
1695 update_basstate(ucs, BS_ATREADY, 0);
1696 }
1697
1698 spin_lock_irqsave(&cs->cmdlock, flags);
1699 if (cs->cmdbuf != NULL)
1700 complete_cb(cs);
1701 spin_unlock_irqrestore(&cs->cmdlock, flags);
1702}
1703
1704/* atrdy_timeout
1705 * timeout routine for AT command transmission
1706 * argument:
1707 * controller state structure
1708 */
1709static void atrdy_timeout(unsigned long data)
1710{
1711 struct cardstate *cs = (struct cardstate *) data;
1712 struct bas_cardstate *ucs;
1713
1714 IFNULLRET(cs);
1715 ucs = cs->hw.bas;
1716 IFNULLRET(ucs);
1717
Tilman Schmidt784d5852006-04-10 22:55:04 -07001718 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001719
1720 /* fake the missing signal - what else can I do? */
1721 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1722 start_cbsend(cs);
1723}
1724
1725/* atwrite_submit
1726 * submit an HD_WRITE_ATMESSAGE command URB
1727 * parameters:
1728 * cs controller state structure
1729 * buf buffer containing command to send
1730 * len length of command to send
1731 * return value:
1732 * 0 on success
1733 * -EFAULT if a NULL pointer is encountered somewhere
1734 * -EBUSY if another request is pending
1735 * any URB submission error code
1736 */
1737static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1738{
1739 struct bas_cardstate *ucs;
1740 int ret;
1741
1742 IFNULLRETVAL(cs, -EFAULT);
1743 ucs = cs->hw.bas;
1744 IFNULLRETVAL(ucs, -EFAULT);
1745 IFNULLRETVAL(ucs->urb_cmd_out, -EFAULT);
1746
Tilman Schmidt784d5852006-04-10 22:55:04 -07001747 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001748
1749 if (ucs->urb_cmd_out->status == -EINPROGRESS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001750 dev_err(cs->dev,
1751 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001752 return -EBUSY;
1753 }
1754
1755 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1756 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1757 ucs->dr_cmd_out.wValue = 0;
1758 ucs->dr_cmd_out.wIndex = 0;
1759 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1760 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1761 usb_sndctrlpipe(ucs->udev, 0),
1762 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1763 write_command_callback, cs);
1764
1765 if ((ret = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001766 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1767 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001768 return ret;
1769 }
1770
1771 /* submitted successfully */
1772 update_basstate(ucs, 0, BS_ATREADY);
1773
1774 /* start timeout if necessary */
1775 if (!(atomic_read(&ucs->basstate) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001776 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1777 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001778 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1779 ucs->timer_atrdy.data = (unsigned long) cs;
1780 ucs->timer_atrdy.function = atrdy_timeout;
1781 add_timer(&ucs->timer_atrdy);
1782 update_basstate(ucs, BS_ATTIMER, 0);
1783 }
1784 return 0;
1785}
1786
1787/* start_cbsend
1788 * start transmission of AT command queue if necessary
1789 * parameter:
1790 * cs controller state structure
1791 * return value:
1792 * 0 on success
1793 * error code < 0 on error
1794 */
1795static int start_cbsend(struct cardstate *cs)
1796{
1797 struct cmdbuf_t *cb;
1798 struct bas_cardstate *ucs;
1799 unsigned long flags;
1800 int rc;
1801 int retval = 0;
1802
1803 IFNULLRETVAL(cs, -EFAULT);
1804 ucs = cs->hw.bas;
1805 IFNULLRETVAL(ucs, -EFAULT);
1806
1807 /* check if AT channel is open */
1808 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001809 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001810 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1811 if (rc < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001812 dev_err(cs->dev, "could not open AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001813 /* flush command queue */
1814 spin_lock_irqsave(&cs->cmdlock, flags);
1815 while (cs->cmdbuf != NULL)
1816 complete_cb(cs);
1817 spin_unlock_irqrestore(&cs->cmdlock, flags);
1818 }
1819 return rc;
1820 }
1821
1822 /* try to send first command in queue */
1823 spin_lock_irqsave(&cs->cmdlock, flags);
1824
1825 while ((cb = cs->cmdbuf) != NULL &&
1826 atomic_read(&ucs->basstate) & BS_ATREADY) {
1827 ucs->retry_cmd_out = 0;
1828 rc = atwrite_submit(cs, cb->buf, cb->len);
1829 if (unlikely(rc)) {
1830 retval = rc;
1831 complete_cb(cs);
1832 }
1833 }
1834
1835 spin_unlock_irqrestore(&cs->cmdlock, flags);
1836 return retval;
1837}
1838
1839/* gigaset_write_cmd
1840 * This function is called by the device independent part of the driver
1841 * to transmit an AT command string to the Gigaset device.
1842 * It encapsulates the device specific method for transmission over the
1843 * direct USB connection to the base.
1844 * The command string is added to the queue of commands to send, and
1845 * USB transmission is started if necessary.
1846 * parameters:
1847 * cs controller state structure
1848 * buf command string to send
1849 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001850 * wake_tasklet tasklet to run when transmission is completed
1851 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001852 * return value:
1853 * number of bytes queued on success
1854 * error code < 0 on error
1855 */
1856static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001857 const unsigned char *buf, int len,
1858 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001859{
1860 struct cmdbuf_t *cb;
1861 unsigned long flags;
1862 int status;
1863
1864 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001865 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1866 "CMD Transmit", len, buf, 0);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001867
Tilman Schmidt784d5852006-04-10 22:55:04 -07001868 if (unlikely(!atomic_read(&cs->connected))) {
1869 err("%s: disconnected", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001870 return -ENODEV;
1871 }
1872
1873 if (len <= 0)
1874 return 0; /* nothing to do */
1875
1876 if (len > IF_WRITEBUF)
1877 len = IF_WRITEBUF;
1878 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001879 dev_err(cs->dev, "%s: out of memory\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001880 return -ENOMEM;
1881 }
1882
1883 memcpy(cb->buf, buf, len);
1884 cb->len = len;
1885 cb->offset = 0;
1886 cb->next = NULL;
1887 cb->wake_tasklet = wake_tasklet;
1888
1889 spin_lock_irqsave(&cs->cmdlock, flags);
1890 cb->prev = cs->lastcmdbuf;
1891 if (cs->lastcmdbuf)
1892 cs->lastcmdbuf->next = cb;
1893 else {
1894 cs->cmdbuf = cb;
1895 cs->curlen = len;
1896 }
1897 cs->cmdbytes += len;
1898 cs->lastcmdbuf = cb;
1899 spin_unlock_irqrestore(&cs->cmdlock, flags);
1900
1901 status = start_cbsend(cs);
1902
1903 return status < 0 ? status : len;
1904}
1905
1906/* gigaset_write_room
1907 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001908 * return number of characters the driver will accept to be written via
1909 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001910 * parameter:
1911 * controller state structure
1912 * return value:
1913 * number of characters
1914 */
1915static int gigaset_write_room(struct cardstate *cs)
1916{
1917 return IF_WRITEBUF;
1918}
1919
1920/* gigaset_chars_in_buffer
1921 * tty_driver.chars_in_buffer interface routine
1922 * return number of characters waiting to be sent
1923 * parameter:
1924 * controller state structure
1925 * return value:
1926 * number of characters
1927 */
1928static int gigaset_chars_in_buffer(struct cardstate *cs)
1929{
1930 unsigned long flags;
1931 unsigned bytes;
1932
1933 spin_lock_irqsave(&cs->cmdlock, flags);
1934 bytes = cs->cmdbytes;
1935 spin_unlock_irqrestore(&cs->cmdlock, flags);
1936
1937 return bytes;
1938}
1939
1940/* gigaset_brkchars
1941 * implementation of ioctl(GIGASET_BRKCHARS)
1942 * parameter:
1943 * controller state structure
1944 * return value:
1945 * -EINVAL (unimplemented function)
1946 */
1947static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1948{
1949 return -EINVAL;
1950}
1951
1952
1953/* Device Initialization/Shutdown */
1954/* ============================== */
1955
1956/* Free hardware dependent part of the B channel structure
1957 * parameter:
1958 * bcs B channel structure
1959 * return value:
1960 * !=0 on success
1961 */
1962static int gigaset_freebcshw(struct bc_state *bcs)
1963{
1964 if (!bcs->hw.bas)
1965 return 0;
1966
1967 if (bcs->hw.bas->isooutbuf)
1968 kfree(bcs->hw.bas->isooutbuf);
1969 kfree(bcs->hw.bas);
1970 bcs->hw.bas = NULL;
1971 return 1;
1972}
1973
1974/* Initialize hardware dependent part of the B channel structure
1975 * parameter:
1976 * bcs B channel structure
1977 * return value:
1978 * !=0 on success
1979 */
1980static int gigaset_initbcshw(struct bc_state *bcs)
1981{
1982 int i;
1983 struct bas_bc_state *ubc;
1984
1985 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1986 if (!ubc) {
1987 err("could not allocate bas_bc_state");
1988 return 0;
1989 }
1990
1991 atomic_set(&ubc->running, 0);
1992 atomic_set(&ubc->corrbytes, 0);
1993 spin_lock_init(&ubc->isooutlock);
1994 for (i = 0; i < BAS_OUTURBS; ++i) {
1995 ubc->isoouturbs[i].urb = NULL;
1996 ubc->isoouturbs[i].bcs = bcs;
1997 }
1998 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1999 ubc->numsub = 0;
2000 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2001 err("could not allocate isochronous output buffer");
2002 kfree(ubc);
2003 bcs->hw.bas = NULL;
2004 return 0;
2005 }
2006 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002007 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002008
2009 spin_lock_init(&ubc->isoinlock);
2010 for (i = 0; i < BAS_INURBS; ++i)
2011 ubc->isoinurbs[i] = NULL;
2012 ubc->isoindone = NULL;
2013 ubc->loststatus = -EINPROGRESS;
2014 ubc->isoinlost = 0;
2015 ubc->seqlen = 0;
2016 ubc->inbyte = 0;
2017 ubc->inbits = 0;
2018 ubc->goodbytes = 0;
2019 ubc->alignerrs = 0;
2020 ubc->fcserrs = 0;
2021 ubc->frameerrs = 0;
2022 ubc->giants = 0;
2023 ubc->runts = 0;
2024 ubc->aborts = 0;
2025 ubc->shared0s = 0;
2026 ubc->stolen0s = 0;
2027 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002028 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002029 return 1;
2030}
2031
2032static void gigaset_reinitbcshw(struct bc_state *bcs)
2033{
2034 struct bas_bc_state *ubc = bcs->hw.bas;
2035
2036 atomic_set(&bcs->hw.bas->running, 0);
2037 atomic_set(&bcs->hw.bas->corrbytes, 0);
2038 bcs->hw.bas->numsub = 0;
2039 spin_lock_init(&ubc->isooutlock);
2040 spin_lock_init(&ubc->isoinlock);
2041 ubc->loststatus = -EINPROGRESS;
2042}
2043
2044static void gigaset_freecshw(struct cardstate *cs)
2045{
2046 struct bas_cardstate *ucs = cs->hw.bas;
2047
2048 del_timer(&ucs->timer_ctrl);
2049 del_timer(&ucs->timer_atrdy);
2050 del_timer(&ucs->timer_cmd_in);
2051
2052 kfree(cs->hw.bas);
2053}
2054
2055static int gigaset_initcshw(struct cardstate *cs)
2056{
2057 struct bas_cardstate *ucs;
2058
2059 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2060 if (!ucs)
2061 return 0;
2062
2063 ucs->urb_cmd_in = NULL;
2064 ucs->urb_cmd_out = NULL;
2065 ucs->rcvbuf = NULL;
2066 ucs->rcvbuf_size = 0;
2067
2068 spin_lock_init(&ucs->lock);
2069 ucs->pending = 0;
2070
2071 atomic_set(&ucs->basstate, 0);
2072 init_timer(&ucs->timer_ctrl);
2073 init_timer(&ucs->timer_atrdy);
2074 init_timer(&ucs->timer_cmd_in);
2075
2076 return 1;
2077}
2078
2079/* freeurbs
2080 * unlink and deallocate all URBs unconditionally
2081 * caller must make sure that no commands are still in progress
2082 * parameter:
2083 * cs controller state structure
2084 */
2085static void freeurbs(struct cardstate *cs)
2086{
2087 struct bas_cardstate *ucs;
2088 struct bas_bc_state *ubc;
2089 int i, j;
2090
2091 IFNULLRET(cs);
2092 ucs = cs->hw.bas;
2093 IFNULLRET(ucs);
2094
2095 for (j = 0; j < 2; ++j) {
2096 ubc = cs->bcs[j].hw.bas;
2097 IFNULLCONT(ubc);
2098 for (i = 0; i < BAS_OUTURBS; ++i)
2099 if (ubc->isoouturbs[i].urb) {
2100 usb_kill_urb(ubc->isoouturbs[i].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002101 gig_dbg(DEBUG_INIT,
2102 "%s: isoc output URB %d/%d unlinked",
2103 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002104 usb_free_urb(ubc->isoouturbs[i].urb);
2105 ubc->isoouturbs[i].urb = NULL;
2106 }
2107 for (i = 0; i < BAS_INURBS; ++i)
2108 if (ubc->isoinurbs[i]) {
2109 usb_kill_urb(ubc->isoinurbs[i]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002110 gig_dbg(DEBUG_INIT,
2111 "%s: isoc input URB %d/%d unlinked",
2112 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002113 usb_free_urb(ubc->isoinurbs[i]);
2114 ubc->isoinurbs[i] = NULL;
2115 }
2116 }
2117 if (ucs->urb_int_in) {
2118 usb_kill_urb(ucs->urb_int_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002119 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2120 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002121 usb_free_urb(ucs->urb_int_in);
2122 ucs->urb_int_in = NULL;
2123 }
2124 if (ucs->urb_cmd_out) {
2125 usb_kill_urb(ucs->urb_cmd_out);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002126 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2127 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002128 usb_free_urb(ucs->urb_cmd_out);
2129 ucs->urb_cmd_out = NULL;
2130 }
2131 if (ucs->urb_cmd_in) {
2132 usb_kill_urb(ucs->urb_cmd_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002133 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2134 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002135 usb_free_urb(ucs->urb_cmd_in);
2136 ucs->urb_cmd_in = NULL;
2137 }
2138 if (ucs->urb_ctrl) {
2139 usb_kill_urb(ucs->urb_ctrl);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002140 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2141 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002142 usb_free_urb(ucs->urb_ctrl);
2143 ucs->urb_ctrl = NULL;
2144 }
2145}
2146
2147/* gigaset_probe
2148 * This function is called when a new USB device is connected.
2149 * It checks whether the new device is handled by this driver.
2150 */
2151static int gigaset_probe(struct usb_interface *interface,
2152 const struct usb_device_id *id)
2153{
2154 struct usb_host_interface *hostif;
2155 struct usb_device *udev = interface_to_usbdev(interface);
2156 struct cardstate *cs = NULL;
2157 struct bas_cardstate *ucs = NULL;
2158 struct bas_bc_state *ubc;
2159 struct usb_endpoint_descriptor *endpoint;
2160 int i, j;
2161 int ret;
2162
2163 IFNULLRETVAL(udev, -ENODEV);
2164
Tilman Schmidt784d5852006-04-10 22:55:04 -07002165 gig_dbg(DEBUG_ANY,
2166 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2167 __func__, le16_to_cpu(udev->descriptor.idVendor),
2168 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002169
2170 /* See if the device offered us matches what we can accept */
2171 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_GIGA_VENDOR_ID) ||
2172 (le16_to_cpu(udev->descriptor.idProduct) != USB_GIGA_PRODUCT_ID &&
2173 le16_to_cpu(udev->descriptor.idProduct) != USB_4175_PRODUCT_ID &&
2174 le16_to_cpu(udev->descriptor.idProduct) != USB_SX303_PRODUCT_ID &&
2175 le16_to_cpu(udev->descriptor.idProduct) != USB_SX353_PRODUCT_ID)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002176 gig_dbg(DEBUG_ANY, "%s: unmatched ID - exiting", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002177 return -ENODEV;
2178 }
2179
2180 /* set required alternate setting */
2181 hostif = interface->cur_altsetting;
2182 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002183 gig_dbg(DEBUG_ANY,
2184 "%s: wrong alternate setting %d - trying to switch",
2185 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002186 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002187 dev_warn(&udev->dev, "usb_set_interface failed, "
2188 "device %d interface %d altsetting %d\n",
2189 udev->devnum, hostif->desc.bInterfaceNumber,
2190 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002191 return -ENODEV;
2192 }
2193 hostif = interface->cur_altsetting;
2194 }
2195
2196 /* Reject application specific interfaces
2197 */
2198 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002199 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2200 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002201 return -ENODEV;
2202 }
2203
Tilman Schmidt784d5852006-04-10 22:55:04 -07002204 dev_info(&udev->dev,
2205 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2206 __func__, le16_to_cpu(udev->descriptor.idVendor),
2207 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002208
2209 cs = gigaset_getunassignedcs(driver);
2210 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002211 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002212 return -ENODEV;
2213 }
2214 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002215
2216 /* save off device structure ptrs for later use */
2217 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002218 ucs->udev = udev;
2219 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002220 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002221
2222 /* allocate URBs:
2223 * - one for the interrupt pipe
2224 * - three for the different uses of the default control pipe
2225 * - three for each isochronous pipe
2226 */
2227 ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL);
2228 if (!ucs->urb_int_in) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002229 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002230 goto error;
2231 }
2232 ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL);
2233 if (!ucs->urb_cmd_in) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002234 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002235 goto error;
2236 }
2237 ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL);
2238 if (!ucs->urb_cmd_out) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002239 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002240 goto error;
2241 }
2242 ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL);
2243 if (!ucs->urb_ctrl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002244 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002245 goto error;
2246 }
2247
2248 for (j = 0; j < 2; ++j) {
2249 ubc = cs->bcs[j].hw.bas;
2250 for (i = 0; i < BAS_OUTURBS; ++i) {
2251 ubc->isoouturbs[i].urb =
2252 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2253 if (!ubc->isoouturbs[i].urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002254 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002255 goto error;
2256 }
2257 }
2258 for (i = 0; i < BAS_INURBS; ++i) {
2259 ubc->isoinurbs[i] =
2260 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2261 if (!ubc->isoinurbs[i]) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002262 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002263 goto error;
2264 }
2265 }
2266 }
2267
2268 ucs->rcvbuf = NULL;
2269 ucs->rcvbuf_size = 0;
2270
2271 /* Fill the interrupt urb and send it to the core */
2272 endpoint = &hostif->endpoint[0].desc;
2273 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002274 usb_rcvintpipe(udev,
2275 (endpoint->bEndpointAddress) & 0x0f),
2276 ucs->int_in_buf, 3, read_int_callback, cs,
2277 endpoint->bInterval);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002278 ret = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL);
2279 if (ret) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002280 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2281 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002282 goto error;
2283 }
2284
2285 /* tell the device that the driver is ready */
2286 if ((ret = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2287 goto error;
2288
2289 /* tell common part that the device is ready */
2290 if (startmode == SM_LOCKED)
2291 atomic_set(&cs->mstate, MS_LOCKED);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002292
2293 /* save address of controller structure */
2294 usb_set_intfdata(interface, cs);
2295
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002296 if (!gigaset_start(cs))
2297 goto error;
2298
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002299 return 0;
2300
2301error:
2302 freeurbs(cs);
2303 gigaset_unassign(cs);
2304 return -ENODEV;
2305}
2306
2307/* gigaset_disconnect
2308 * This function is called when the Gigaset base is unplugged.
2309 */
2310static void gigaset_disconnect(struct usb_interface *interface)
2311{
2312 struct cardstate *cs;
2313 struct bas_cardstate *ucs;
2314
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002315 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002316
2317 IFNULLRET(cs);
2318 ucs = cs->hw.bas;
2319 IFNULLRET(ucs);
2320
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002321 dev_info(cs->dev, "disconnecting Gigaset base\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002322 gigaset_stop(cs);
2323 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002324 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002325 kfree(ucs->rcvbuf);
2326 ucs->rcvbuf = NULL;
2327 ucs->rcvbuf_size = 0;
2328 atomic_set(&ucs->basstate, 0);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002329 usb_put_dev(ucs->udev);
2330 ucs->interface = NULL;
2331 ucs->udev = NULL;
2332 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002333 gigaset_unassign(cs);
2334}
2335
2336static struct gigaset_ops gigops = {
2337 gigaset_write_cmd,
2338 gigaset_write_room,
2339 gigaset_chars_in_buffer,
2340 gigaset_brkchars,
2341 gigaset_init_bchannel,
2342 gigaset_close_bchannel,
2343 gigaset_initbcshw,
2344 gigaset_freebcshw,
2345 gigaset_reinitbcshw,
2346 gigaset_initcshw,
2347 gigaset_freecshw,
2348 gigaset_set_modem_ctrl,
2349 gigaset_baud_rate,
2350 gigaset_set_line_ctrl,
2351 gigaset_isoc_send_skb,
2352 gigaset_isoc_input,
2353};
2354
2355/* bas_gigaset_init
2356 * This function is called after the kernel module is loaded.
2357 */
2358static int __init bas_gigaset_init(void)
2359{
2360 int result;
2361
2362 /* allocate memory for our driver state and intialize it */
2363 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002364 GIGASET_MODULENAME, GIGASET_DEVNAME,
2365 GIGASET_DEVFSNAME, &gigops,
2366 THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002367 goto error;
2368
2369 /* allocate memory for our device state and intialize it */
Tilman Schmidt917f5082006-04-10 22:55:00 -07002370 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2371 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002372 if (!cardstate)
2373 goto error;
2374
2375 /* register this driver with the USB subsystem */
2376 result = usb_register(&gigaset_usb_driver);
2377 if (result < 0) {
2378 err("usb_register failed (error %d)", -result);
2379 goto error;
2380 }
2381
2382 info(DRIVER_AUTHOR);
2383 info(DRIVER_DESC);
2384 return 0;
2385
2386error: if (cardstate)
2387 gigaset_freecs(cardstate);
2388 cardstate = NULL;
2389 if (driver)
2390 gigaset_freedriver(driver);
2391 driver = NULL;
2392 return -1;
2393}
2394
2395/* bas_gigaset_exit
2396 * This function is called before the kernel module is unloaded.
2397 */
2398static void __exit bas_gigaset_exit(void)
2399{
2400 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002401 * => no gigaset_start any more
2402 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002403
2404 gigaset_shutdown(cardstate);
2405 /* from now on, no isdn callback should be possible */
2406
2407 if (atomic_read(&cardstate->hw.bas->basstate) & BS_ATOPEN) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002408 gig_dbg(DEBUG_ANY, "closing AT channel");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002409 if (req_submit(cardstate->bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002410 HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT) >= 0) {
2411 /* successfully submitted */
2412 //FIXME wait for completion?
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002413 }
2414 }
2415
2416 /* deregister this driver with the USB subsystem */
2417 usb_deregister(&gigaset_usb_driver);
2418 /* this will call the disconnect-callback */
2419 /* from now on, no disconnect/probe callback should be running */
2420
2421 gigaset_freecs(cardstate);
2422 cardstate = NULL;
2423 gigaset_freedriver(driver);
2424 driver = NULL;
2425}
2426
2427
2428module_init(bas_gigaset_init);
2429module_exit(bas_gigaset_exit);
2430
2431MODULE_AUTHOR(DRIVER_AUTHOR);
2432MODULE_DESCRIPTION(DRIVER_DESC);
2433MODULE_LICENSE("GPL");