blob: fa37db68c962efc365a6b134347337e9430cd978 [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>,
Tilman Schmidt70440cf2006-04-10 22:55:14 -07006 * Stefan Eilers.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08007 *
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 */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070029#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080030#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;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700208 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800209 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700210 gig_dbg(level,
211 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
212 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
213 (unsigned long) urb->dev,
214 usb_pipetype_str(urb->pipe),
215 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
216 usb_pipein(urb->pipe) ? "in" : "out",
217 urb->status, (unsigned long) urb->hcpriv,
218 urb->transfer_flags);
219 gig_dbg(level,
220 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
221 "bandwidth=%d, setup_packet=0x%08lx,",
222 (unsigned long) urb->transfer_buffer,
223 urb->transfer_buffer_length, urb->actual_length,
224 urb->bandwidth, (unsigned long) urb->setup_packet);
225 gig_dbg(level,
226 " start_frame=%d, number_of_packets=%d, interval=%d, "
227 "error_count=%d,",
228 urb->start_frame, urb->number_of_packets, urb->interval,
229 urb->error_count);
230 gig_dbg(level,
231 " context=0x%08lx, complete=0x%08lx, "
232 "iso_frame_desc[]={",
233 (unsigned long) urb->context,
234 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800235 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700236 struct usb_iso_packet_descriptor *pifd
237 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700238 gig_dbg(level,
239 " {offset=%u, length=%u, actual_length=%u, "
240 "status=%u}",
241 pifd->offset, pifd->length, pifd->actual_length,
242 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800243 }
244 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700245 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800246#endif
247}
248
249/* read/set modem control bits etc. (m10x only) */
250static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700251 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800252{
253 return -EINVAL;
254}
255
256static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
257{
258 return -EINVAL;
259}
260
261static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
262{
263 return -EINVAL;
264}
265
266/* error_hangup
267 * hang up any existing connection because of an unrecoverable error
268 * This function may be called from any context and takes care of scheduling
269 * the necessary actions for execution outside of interrupt context.
270 * argument:
271 * B channel control structure
272 */
273static inline void error_hangup(struct bc_state *bcs)
274{
275 struct cardstate *cs = bcs->cs;
276
Tilman Schmidt784d5852006-04-10 22:55:04 -0700277 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
278 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800279
280 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
281 //FIXME what should we do?
282 return;
283 }
284
285 gigaset_schedule_event(cs);
286}
287
288/* error_reset
289 * reset Gigaset device because of an unrecoverable error
290 * This function may be called from any context and takes care of scheduling
291 * the necessary actions for execution outside of interrupt context.
292 * argument:
293 * controller state structure
294 */
295static inline void error_reset(struct cardstate *cs)
296{
297 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700298 dev_err(cs->dev,
299 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800300}
301
302/* check_pending
303 * check for completion of pending control request
304 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700305 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800306 */
307static void check_pending(struct bas_cardstate *ucs)
308{
309 unsigned long flags;
310
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800311 spin_lock_irqsave(&ucs->lock, flags);
312 switch (ucs->pending) {
313 case 0:
314 break;
315 case HD_OPEN_ATCHANNEL:
316 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
317 ucs->pending = 0;
318 break;
319 case HD_OPEN_B1CHANNEL:
320 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
321 ucs->pending = 0;
322 break;
323 case HD_OPEN_B2CHANNEL:
324 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
325 ucs->pending = 0;
326 break;
327 case HD_CLOSE_ATCHANNEL:
328 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
329 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800330 break;
331 case HD_CLOSE_B1CHANNEL:
332 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
333 ucs->pending = 0;
334 break;
335 case HD_CLOSE_B2CHANNEL:
336 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
337 ucs->pending = 0;
338 break;
339 case HD_DEVICE_INIT_ACK: /* no reply expected */
340 ucs->pending = 0;
341 break;
342 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
343 * are handled separately and should never end up here
344 */
345 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700346 dev_warn(&ucs->interface->dev,
347 "unknown pending request 0x%02x cleared\n",
348 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800349 ucs->pending = 0;
350 }
351
352 if (!ucs->pending)
353 del_timer(&ucs->timer_ctrl);
354
355 spin_unlock_irqrestore(&ucs->lock, flags);
356}
357
358/* cmd_in_timeout
359 * timeout routine for command input request
360 * argument:
361 * controller state structure
362 */
363static void cmd_in_timeout(unsigned long data)
364{
365 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700366 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800367 unsigned long flags;
368
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800369 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700370 if (unlikely(!atomic_read(&cs->connected))) {
371 gig_dbg(DEBUG_USBREQ, "%s: disconnected", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800372 spin_unlock_irqrestore(&cs->lock, flags);
373 return;
374 }
375 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700376 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800377 spin_unlock_irqrestore(&cs->lock, flags);
378 return;
379 }
380 spin_unlock_irqrestore(&cs->lock, flags);
381
Tilman Schmidt784d5852006-04-10 22:55:04 -0700382 dev_err(cs->dev, "timeout reading AT response\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800383 error_reset(cs); //FIXME retry?
384}
385
386
387static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs);
388
389/* atread_submit
390 * submit an HD_READ_ATMESSAGE command URB
391 * parameters:
392 * cs controller state structure
393 * timeout timeout in 1/10 sec., 0: none
394 * return value:
395 * 0 on success
396 * -EINVAL if a NULL pointer is encountered somewhere
397 * -EBUSY if another request is pending
398 * any URB submission error code
399 */
400static int atread_submit(struct cardstate *cs, int timeout)
401{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700402 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800403 int ret;
404
Tilman Schmidt784d5852006-04-10 22:55:04 -0700405 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
406 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800407
408 if (ucs->urb_cmd_in->status == -EINPROGRESS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700409 dev_err(cs->dev,
410 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800411 return -EBUSY;
412 }
413
414 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
415 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
416 ucs->dr_cmd_in.wValue = 0;
417 ucs->dr_cmd_in.wIndex = 0;
418 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
419 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700420 usb_rcvctrlpipe(ucs->udev, 0),
421 (unsigned char*) & ucs->dr_cmd_in,
422 ucs->rcvbuf, ucs->rcvbuf_size,
423 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800424
425 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700426 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
427 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800428 return ret;
429 }
430
431 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700432 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800433 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
434 ucs->timer_cmd_in.data = (unsigned long) cs;
435 ucs->timer_cmd_in.function = cmd_in_timeout;
436 add_timer(&ucs->timer_cmd_in);
437 }
438 return 0;
439}
440
441static void stopurbs(struct bas_bc_state *);
442static int start_cbsend(struct cardstate *);
443
444/* set/clear bits in base connection state
445 */
446inline static void update_basstate(struct bas_cardstate *ucs,
447 int set, int clear)
448{
449 unsigned long flags;
450 int state;
451
452 spin_lock_irqsave(&ucs->lock, flags);
453 state = atomic_read(&ucs->basstate);
454 state &= ~clear;
455 state |= set;
456 atomic_set(&ucs->basstate, state);
457 spin_unlock_irqrestore(&ucs->lock, flags);
458}
459
460
461/* read_int_callback
462 * USB completion handler for interrupt pipe input
463 * called by the USB subsystem in interrupt context
464 * parameter:
465 * urb USB request block
466 * urb->context = controller state structure
467 */
468static void read_int_callback(struct urb *urb, struct pt_regs *regs)
469{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700470 struct cardstate *cs = urb->context;
471 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800472 struct bc_state *bcs;
473 unsigned long flags;
474 int status;
475 unsigned l;
476 int channel;
477
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800478 if (unlikely(!atomic_read(&cs->connected))) {
479 warn("%s: disconnected", __func__);
480 return;
481 }
482
483 switch (urb->status) {
484 case 0: /* success */
485 break;
486 case -ENOENT: /* canceled */
487 case -ECONNRESET: /* canceled (async) */
488 case -EINPROGRESS: /* pending */
489 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700490 gig_dbg(DEBUG_USBREQ, "%s: %s",
491 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800492 return;
493 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700494 dev_warn(cs->dev, "interrupt read: %s\n",
495 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800496 //FIXME corrective action? resubmission always ok?
497 goto resubmit;
498 }
499
500 l = (unsigned) ucs->int_in_buf[1] +
501 (((unsigned) ucs->int_in_buf[2]) << 8);
502
Tilman Schmidt784d5852006-04-10 22:55:04 -0700503 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
504 urb->actual_length, (int)ucs->int_in_buf[0], l,
505 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800506
507 channel = 0;
508
509 switch (ucs->int_in_buf[0]) {
510 case HD_DEVICE_INIT_OK:
511 update_basstate(ucs, BS_INIT, 0);
512 break;
513
514 case HD_READY_SEND_ATDATA:
515 del_timer(&ucs->timer_atrdy);
516 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
517 start_cbsend(cs);
518 break;
519
520 case HD_OPEN_B2CHANNEL_ACK:
521 ++channel;
522 case HD_OPEN_B1CHANNEL_ACK:
523 bcs = cs->bcs + channel;
524 update_basstate(ucs, BS_B1OPEN << channel, 0);
525 gigaset_bchannel_up(bcs);
526 break;
527
528 case HD_OPEN_ATCHANNEL_ACK:
529 update_basstate(ucs, BS_ATOPEN, 0);
530 start_cbsend(cs);
531 break;
532
533 case HD_CLOSE_B2CHANNEL_ACK:
534 ++channel;
535 case HD_CLOSE_B1CHANNEL_ACK:
536 bcs = cs->bcs + channel;
537 update_basstate(ucs, 0, BS_B1OPEN << channel);
538 stopurbs(bcs->hw.bas);
539 gigaset_bchannel_down(bcs);
540 break;
541
542 case HD_CLOSE_ATCHANNEL_ACK:
543 update_basstate(ucs, 0, BS_ATOPEN);
544 break;
545
546 case HD_B2_FLOW_CONTROL:
547 ++channel;
548 case HD_B1_FLOW_CONTROL:
549 bcs = cs->bcs + channel;
550 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700551 &bcs->hw.bas->corrbytes);
552 gig_dbg(DEBUG_ISO,
553 "Flow control (channel %d, sub %d): 0x%02x => %d",
554 channel, bcs->hw.bas->numsub, l,
555 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800556 break;
557
558 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
559 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700560 dev_warn(cs->dev,
561 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800562 break;
563 }
564 spin_lock_irqsave(&cs->lock, flags);
565 if (ucs->rcvbuf_size) {
566 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700567 dev_err(cs->dev,
568 "receive AT data overrun, %d bytes lost\n", l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800569 error_reset(cs); //FIXME reschedule
570 break;
571 }
572 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
573 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700574 dev_err(cs->dev, "out of memory, %d bytes lost\n", l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800575 error_reset(cs); //FIXME reschedule
576 break;
577 }
578 ucs->rcvbuf_size = l;
579 ucs->retry_cmd_in = 0;
580 if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
581 kfree(ucs->rcvbuf);
582 ucs->rcvbuf = NULL;
583 ucs->rcvbuf_size = 0;
584 error_reset(cs); //FIXME reschedule
585 }
586 spin_unlock_irqrestore(&cs->lock, flags);
587 break;
588
589 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700590 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800591 break;
592
593 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700594 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800595 break;
596
597 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700598 dev_warn(cs->dev,
599 "unknown Gigaset signal 0x%02x (%u) ignored\n",
600 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800601 }
602
603 check_pending(ucs);
604
605resubmit:
606 status = usb_submit_urb(urb, SLAB_ATOMIC);
607 if (unlikely(status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700608 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
609 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800610 error_reset(cs);
611 }
612}
613
614/* read_ctrl_callback
615 * USB completion handler for control pipe input
616 * called by the USB subsystem in interrupt context
617 * parameter:
618 * urb USB request block
619 * urb->context = inbuf structure for controller state
620 */
621static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
622{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700623 struct inbuf_t *inbuf = urb->context;
624 struct cardstate *cs = inbuf->cs;
625 struct bas_cardstate *ucs = cs->hw.bas;
626 int have_data = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800627 unsigned numbytes;
628 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800629
630 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700631 if (unlikely(!atomic_read(&cs->connected))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800632 warn("%s: disconnected", __func__);
633 spin_unlock_irqrestore(&cs->lock, flags);
634 return;
635 }
636
637 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700638 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800639 spin_unlock_irqrestore(&cs->lock, flags);
640 return;
641 }
642
643 del_timer(&ucs->timer_cmd_in);
644
645 switch (urb->status) {
646 case 0: /* normal completion */
647 numbytes = urb->actual_length;
648 if (unlikely(numbytes == 0)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700649 dev_warn(cs->dev,
650 "control read: empty block received\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800651 goto retry;
652 }
653 if (unlikely(numbytes != ucs->rcvbuf_size)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700654 dev_warn(cs->dev,
655 "control read: received %d chars, expected %d\n",
656 numbytes, ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800657 if (numbytes > ucs->rcvbuf_size)
658 numbytes = ucs->rcvbuf_size;
659 }
660
661 /* copy received bytes to inbuf */
662 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
663
664 if (unlikely(numbytes < ucs->rcvbuf_size)) {
665 /* incomplete - resubmit for remaining bytes */
666 ucs->rcvbuf_size -= numbytes;
667 ucs->retry_cmd_in = 0;
668 goto retry;
669 }
670 break;
671
672 case -ENOENT: /* canceled */
673 case -ECONNRESET: /* canceled (async) */
674 case -EINPROGRESS: /* pending */
675 /* no action necessary */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700676 gig_dbg(DEBUG_USBREQ, "%s: %s",
677 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800678 break;
679
680 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700681 dev_warn(cs->dev, "control read: %s\n",
682 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800683 retry:
684 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700685 dev_notice(cs->dev, "control read: retry %d\n",
686 ucs->retry_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800687 if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
688 /* resubmitted - bypass regular exit block */
689 spin_unlock_irqrestore(&cs->lock, flags);
690 return;
691 }
692 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700693 dev_err(cs->dev,
694 "control read: giving up after %d tries\n",
695 ucs->retry_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800696 }
697 error_reset(cs);
698 }
699
700 kfree(ucs->rcvbuf);
701 ucs->rcvbuf = NULL;
702 ucs->rcvbuf_size = 0;
703 spin_unlock_irqrestore(&cs->lock, flags);
704 if (have_data) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700705 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800706 gigaset_schedule_event(cs);
707 }
708}
709
710/* read_iso_callback
711 * USB completion handler for B channel isochronous input
712 * called by the USB subsystem in interrupt context
713 * parameter:
714 * urb USB request block of completed request
715 * urb->context = bc_state structure
716 */
717static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
718{
719 struct bc_state *bcs;
720 struct bas_bc_state *ubc;
721 unsigned long flags;
722 int i, rc;
723
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800724 /* status codes not worth bothering the tasklet with */
725 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
Tilman Schmidt784d5852006-04-10 22:55:04 -0700726 urb->status == -EINPROGRESS)) {
727 gig_dbg(DEBUG_ISO, "%s: %s",
728 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800729 return;
730 }
731
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700732 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800733 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800734
735 spin_lock_irqsave(&ubc->isoinlock, flags);
736 if (likely(ubc->isoindone == NULL)) {
737 /* pass URB to tasklet */
738 ubc->isoindone = urb;
739 tasklet_schedule(&ubc->rcvd_tasklet);
740 } else {
741 /* tasklet still busy, drop data and resubmit URB */
742 ubc->loststatus = urb->status;
743 for (i = 0; i < BAS_NUMFRAMES; i++) {
744 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
745 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
746 urb->iso_frame_desc[i].status != -EINPROGRESS)) {
747 ubc->loststatus = urb->iso_frame_desc[i].status;
748 }
749 urb->iso_frame_desc[i].status = 0;
750 urb->iso_frame_desc[i].actual_length = 0;
751 }
752 if (likely(atomic_read(&ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700753 /* urb->dev is clobbered by USB subsystem */
754 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800755 urb->transfer_flags = URB_ISO_ASAP;
756 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700757 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
758 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800759 rc = usb_submit_urb(urb, SLAB_ATOMIC);
760 if (unlikely(rc != 0)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700761 dev_err(bcs->cs->dev,
762 "could not resubmit isochronous read "
763 "URB: %s\n", get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800764 dump_urb(DEBUG_ISO, "isoc read", urb);
765 error_hangup(bcs);
766 }
767 }
768 }
769 spin_unlock_irqrestore(&ubc->isoinlock, flags);
770}
771
772/* write_iso_callback
773 * USB completion handler for B channel isochronous output
774 * called by the USB subsystem in interrupt context
775 * parameter:
776 * urb USB request block of completed request
777 * urb->context = isow_urbctx_t structure
778 */
779static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
780{
781 struct isow_urbctx_t *ucx;
782 struct bas_bc_state *ubc;
783 unsigned long flags;
784
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800785 /* status codes not worth bothering the tasklet with */
786 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
Tilman Schmidt784d5852006-04-10 22:55:04 -0700787 urb->status == -EINPROGRESS)) {
788 gig_dbg(DEBUG_ISO, "%s: %s",
789 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800790 return;
791 }
792
793 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700794 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800795 ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800796
797 spin_lock_irqsave(&ubc->isooutlock, flags);
798 ubc->isooutovfl = ubc->isooutdone;
799 ubc->isooutdone = ucx;
800 spin_unlock_irqrestore(&ubc->isooutlock, flags);
801 tasklet_schedule(&ubc->sent_tasklet);
802}
803
804/* starturbs
805 * prepare and submit USB request blocks for isochronous input and output
806 * argument:
807 * B channel control structure
808 * return value:
809 * 0 on success
810 * < 0 on error (no URBs submitted)
811 */
812static int starturbs(struct bc_state *bcs)
813{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700814 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800815 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800816 int j, k;
817 int rc;
818
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800819 /* initialize L2 reception */
820 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
821 bcs->inputstate |= INS_flag_hunt;
822
823 /* submit all isochronous input URBs */
824 atomic_set(&ubc->running, 1);
825 for (k = 0; k < BAS_INURBS; k++) {
826 urb = ubc->isoinurbs[k];
827 if (!urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700828 dev_err(bcs->cs->dev, "isoinurbs[%d]==NULL\n", k);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800829 rc = -EFAULT;
830 goto error;
831 }
832
833 urb->dev = bcs->cs->hw.bas->udev;
834 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
835 urb->transfer_flags = URB_ISO_ASAP;
836 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
837 urb->transfer_buffer_length = BAS_INBUFSIZE;
838 urb->number_of_packets = BAS_NUMFRAMES;
839 urb->interval = BAS_FRAMETIME;
840 urb->complete = read_iso_callback;
841 urb->context = bcs;
842 for (j = 0; j < BAS_NUMFRAMES; j++) {
843 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
844 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
845 urb->iso_frame_desc[j].status = 0;
846 urb->iso_frame_desc[j].actual_length = 0;
847 }
848
849 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
850 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700851 dev_err(bcs->cs->dev,
852 "could not submit isochronous read URB %d: %s\n",
853 k, get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800854 goto error;
855 }
856 }
857
858 /* initialize L2 transmission */
859 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
860
861 /* set up isochronous output URBs for flag idling */
862 for (k = 0; k < BAS_OUTURBS; ++k) {
863 urb = ubc->isoouturbs[k].urb;
864 if (!urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700865 dev_err(bcs->cs->dev, "isoouturbs[%d].urb==NULL\n", k);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800866 rc = -EFAULT;
867 goto error;
868 }
869 urb->dev = bcs->cs->hw.bas->udev;
870 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
871 urb->transfer_flags = URB_ISO_ASAP;
872 urb->transfer_buffer = ubc->isooutbuf->data;
873 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
874 urb->number_of_packets = BAS_NUMFRAMES;
875 urb->interval = BAS_FRAMETIME;
876 urb->complete = write_iso_callback;
877 urb->context = &ubc->isoouturbs[k];
878 for (j = 0; j < BAS_NUMFRAMES; ++j) {
879 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
880 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
881 urb->iso_frame_desc[j].status = 0;
882 urb->iso_frame_desc[j].actual_length = 0;
883 }
884 ubc->isoouturbs[k].limit = -1;
885 }
886
887 /* submit two URBs, keep third one */
888 for (k = 0; k < 2; ++k) {
889 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
890 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
891 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700892 dev_err(bcs->cs->dev,
893 "could not submit isochronous write URB %d: %s\n",
894 k, get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800895 goto error;
896 }
897 }
898 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
899 ubc->isooutfree = &ubc->isoouturbs[2];
900 ubc->isooutdone = ubc->isooutovfl = NULL;
901 return 0;
902 error:
903 stopurbs(ubc);
904 return rc;
905}
906
907/* stopurbs
908 * cancel the USB request blocks for isochronous input and output
909 * errors are silently ignored
910 * argument:
911 * B channel control structure
912 */
913static void stopurbs(struct bas_bc_state *ubc)
914{
915 int k, rc;
916
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800917 atomic_set(&ubc->running, 0);
918
919 for (k = 0; k < BAS_INURBS; ++k) {
920 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700921 gig_dbg(DEBUG_ISO,
922 "%s: isoc input URB %d unlinked, result = %d",
923 __func__, k, rc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800924 }
925
926 for (k = 0; k < BAS_OUTURBS; ++k) {
927 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700928 gig_dbg(DEBUG_ISO,
929 "%s: isoc output URB %d unlinked, result = %d",
930 __func__, k, rc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800931 }
932}
933
934/* Isochronous Write - Bottom Half */
935/* =============================== */
936
937/* submit_iso_write_urb
938 * fill and submit the next isochronous write URB
939 * parameters:
940 * bcs B channel state structure
941 * return value:
942 * number of frames submitted in URB
943 * 0 if URB not submitted because no data available (isooutbuf busy)
944 * error code < 0 on error
945 */
946static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
947{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700948 struct urb *urb = ucx->urb;
949 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800950 struct usb_iso_packet_descriptor *ifd;
951 int corrbytes, nframe, rc;
952
Tilman Schmidt784d5852006-04-10 22:55:04 -0700953 /* urb->dev is clobbered by USB subsystem */
954 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800955 urb->transfer_flags = URB_ISO_ASAP;
956 urb->transfer_buffer = ubc->isooutbuf->data;
957 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
958
959 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
960 ifd = &urb->iso_frame_desc[nframe];
961
962 /* compute frame length according to flow control */
963 ifd->length = BAS_NORMFRAME;
964 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700965 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
966 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800967 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
968 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
969 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
970 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
971 ifd->length += corrbytes;
972 atomic_add(-corrbytes, &ubc->corrbytes);
973 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800974
975 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -0700976 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
977 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800978 if (ifd->offset < 0) {
979 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700980 gig_dbg(DEBUG_ISO,
981 "%s: buffer busy at frame %d",
982 __func__, nframe);
983 /* tasklet will be restarted from
984 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800985 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700986 dev_err(ucx->bcs->cs->dev,
987 "%s: buffer error %d at frame %d\n",
988 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800989 return ifd->offset;
990 }
991 break;
992 }
993 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
994 ifd->status = 0;
995 ifd->actual_length = 0;
996 }
997 if ((urb->number_of_packets = nframe) > 0) {
998 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700999 dev_err(ucx->bcs->cs->dev,
1000 "could not submit isochronous write URB: %s\n",
1001 get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001002 dump_urb(DEBUG_ISO, "isoc write", urb);
1003 return rc;
1004 }
1005 ++ubc->numsub;
1006 }
1007 return nframe;
1008}
1009
1010/* write_iso_tasklet
1011 * tasklet scheduled when an isochronous output URB from the Gigaset device
1012 * has completed
1013 * parameter:
1014 * data B channel state structure
1015 */
1016static void write_iso_tasklet(unsigned long data)
1017{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001018 struct bc_state *bcs = (struct bc_state *) data;
1019 struct bas_bc_state *ubc = bcs->hw.bas;
1020 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001021 struct isow_urbctx_t *done, *next, *ovfl;
1022 struct urb *urb;
1023 struct usb_iso_packet_descriptor *ifd;
1024 int offset;
1025 unsigned long flags;
1026 int i;
1027 struct sk_buff *skb;
1028 int len;
1029
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001030 /* loop while completed URBs arrive in time */
1031 for (;;) {
1032 if (unlikely(!atomic_read(&cs->connected))) {
1033 warn("%s: disconnected", __func__);
1034 return;
1035 }
1036
1037 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001038 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001039 return;
1040 }
1041
1042 /* retrieve completed URBs */
1043 spin_lock_irqsave(&ubc->isooutlock, flags);
1044 done = ubc->isooutdone;
1045 ubc->isooutdone = NULL;
1046 ovfl = ubc->isooutovfl;
1047 ubc->isooutovfl = NULL;
1048 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1049 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001050 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001051 error_hangup(bcs);
1052 break;
1053 }
1054 if (!done)
1055 break;
1056
1057 /* submit free URB if available */
1058 spin_lock_irqsave(&ubc->isooutlock, flags);
1059 next = ubc->isooutfree;
1060 ubc->isooutfree = NULL;
1061 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1062 if (next) {
1063 if (submit_iso_write_urb(next) <= 0) {
1064 /* could not submit URB, put it back */
1065 spin_lock_irqsave(&ubc->isooutlock, flags);
1066 if (ubc->isooutfree == NULL) {
1067 ubc->isooutfree = next;
1068 next = NULL;
1069 }
1070 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1071 if (next) {
1072 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001073 dev_err(cs->dev,
1074 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001075 error_hangup(bcs);
1076 }
1077 }
1078 }
1079
1080 /* process completed URB */
1081 urb = done->urb;
1082 switch (urb->status) {
1083 case 0: /* normal completion */
1084 break;
1085 case -EXDEV: /* inspect individual frames */
1086 /* assumptions (for lack of documentation):
Tilman Schmidt917f5082006-04-10 22:55:00 -07001087 * - actual_length bytes of the frame in error are
1088 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001089 * - all following frames are not sent at all
1090 */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001091 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1092 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001093 offset = done->limit; /* just in case */
1094 for (i = 0; i < BAS_NUMFRAMES; i++) {
1095 ifd = &urb->iso_frame_desc[i];
1096 if (ifd->status ||
1097 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001098 dev_warn(cs->dev,
1099 "isochronous write: frame %d: %s, "
1100 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001101 i, get_usb_statmsg(ifd->status),
1102 ifd->actual_length, ifd->length);
1103 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001104 ifd->actual_length)
1105 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001106 break;
1107 }
1108 }
1109#ifdef CONFIG_GIGASET_DEBUG
1110 /* check assumption on remaining frames */
1111 for (; i < BAS_NUMFRAMES; i++) {
1112 ifd = &urb->iso_frame_desc[i];
1113 if (ifd->status != -EINPROGRESS
1114 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001115 dev_warn(cs->dev,
1116 "isochronous write: frame %d: %s, "
1117 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001118 i, get_usb_statmsg(ifd->status),
1119 ifd->actual_length, ifd->length);
1120 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001121 ifd->actual_length)
1122 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001123 break;
1124 }
1125 }
1126#endif
1127 break;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001128 case -EPIPE: //FIXME is this the code for "underrun"?
1129 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001130 error_hangup(bcs);
1131 break;
1132 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001133 dev_warn(cs->dev, "isochronous write: %s\n",
1134 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001135 }
1136
1137 /* mark the write buffer area covered by this URB as free */
1138 if (done->limit >= 0)
1139 atomic_set(&ubc->isooutbuf->read, done->limit);
1140
1141 /* mark URB as free */
1142 spin_lock_irqsave(&ubc->isooutlock, flags);
1143 next = ubc->isooutfree;
1144 ubc->isooutfree = done;
1145 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1146 if (next) {
1147 /* only one URB still active - resubmit one */
1148 if (submit_iso_write_urb(next) <= 0) {
1149 /* couldn't submit */
1150 error_hangup(bcs);
1151 }
1152 }
1153 }
1154
1155 /* process queued SKBs */
1156 while ((skb = skb_dequeue(&bcs->squeue))) {
1157 /* copy to output buffer, doing L2 encapsulation */
1158 len = skb->len;
1159 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1160 /* insufficient buffer space, push back onto queue */
1161 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001162 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1163 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001164 break;
1165 }
1166 skb_pull(skb, len);
1167 gigaset_skb_sent(bcs, skb);
1168 dev_kfree_skb_any(skb);
1169 }
1170}
1171
1172/* Isochronous Read - Bottom Half */
1173/* ============================== */
1174
1175/* read_iso_tasklet
1176 * tasklet scheduled when an isochronous input URB from the Gigaset device
1177 * has completed
1178 * parameter:
1179 * data B channel state structure
1180 */
1181static void read_iso_tasklet(unsigned long data)
1182{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001183 struct bc_state *bcs = (struct bc_state *) data;
1184 struct bas_bc_state *ubc = bcs->hw.bas;
1185 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001186 struct urb *urb;
1187 char *rcvbuf;
1188 unsigned long flags;
1189 int totleft, numbytes, offset, frame, rc;
1190
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001191 /* loop while more completed URBs arrive in the meantime */
1192 for (;;) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001193 if (unlikely(!atomic_read(&cs->connected))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001194 warn("%s: disconnected", __func__);
1195 return;
1196 }
1197
1198 /* retrieve URB */
1199 spin_lock_irqsave(&ubc->isoinlock, flags);
1200 if (!(urb = ubc->isoindone)) {
1201 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1202 return;
1203 }
1204 ubc->isoindone = NULL;
1205 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001206 dev_warn(cs->dev,
1207 "isochronous read overrun, "
1208 "dropped URB with status: %s, %d bytes lost\n",
1209 get_usb_statmsg(ubc->loststatus),
1210 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001211 ubc->loststatus = -EINPROGRESS;
1212 }
1213 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1214
1215 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001216 gig_dbg(DEBUG_ISO,
1217 "%s: channel not running, "
1218 "dropped URB with status: %s",
1219 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001220 return;
1221 }
1222
1223 switch (urb->status) {
1224 case 0: /* normal completion */
1225 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001226 case -EXDEV: /* inspect individual frames
1227 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001228 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1229 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001230 break;
1231 case -ENOENT:
1232 case -ECONNRESET:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001233 gig_dbg(DEBUG_ISO, "%s: URB canceled", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001234 continue; /* -> skip */
1235 case -EINPROGRESS: /* huh? */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001236 gig_dbg(DEBUG_ISO, "%s: URB still pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001237 continue; /* -> skip */
1238 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001239 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001240 error_hangup(bcs);
1241 continue; /* -> skip */
1242 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001243 dev_warn(cs->dev, "isochronous read: %s\n",
1244 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001245 goto error;
1246 }
1247
1248 rcvbuf = urb->transfer_buffer;
1249 totleft = urb->actual_length;
1250 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1251 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001252 dev_warn(cs->dev,
1253 "isochronous read: frame %d: %s\n",
1254 frame,
1255 get_usb_statmsg(
1256 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001257 break;
1258 }
1259 numbytes = urb->iso_frame_desc[frame].actual_length;
1260 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001261 dev_warn(cs->dev,
1262 "isochronous read: frame %d: "
1263 "numbytes (%d) > BAS_MAXFRAME\n",
1264 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001265 break;
1266 }
1267 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001268 dev_warn(cs->dev,
1269 "isochronous read: frame %d: "
1270 "numbytes (%d) > totleft (%d)\n",
1271 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001272 break;
1273 }
1274 offset = urb->iso_frame_desc[frame].offset;
1275 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001276 dev_warn(cs->dev,
1277 "isochronous read: frame %d: "
1278 "offset (%d) + numbytes (%d) "
1279 "> BAS_INBUFSIZE\n",
1280 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001281 break;
1282 }
1283 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1284 totleft -= numbytes;
1285 }
1286 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001287 dev_warn(cs->dev,
1288 "isochronous read: %d data bytes missing\n",
1289 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001290
1291 error:
1292 /* URB processed, resubmit */
1293 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1294 urb->iso_frame_desc[frame].status = 0;
1295 urb->iso_frame_desc[frame].actual_length = 0;
1296 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001297 /* urb->dev is clobbered by USB subsystem */
1298 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001299 urb->transfer_flags = URB_ISO_ASAP;
1300 urb->number_of_packets = BAS_NUMFRAMES;
1301 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001302 dev_err(cs->dev,
1303 "could not resubmit isochronous read URB: %s\n",
1304 get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001305 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1306 error_hangup(bcs);
1307 }
1308 }
1309}
1310
1311/* Channel Operations */
1312/* ================== */
1313
1314/* req_timeout
1315 * timeout routine for control output request
1316 * argument:
1317 * B channel control structure
1318 */
1319static void req_timeout(unsigned long data)
1320{
1321 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001322 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001323 int pending;
1324 unsigned long flags;
1325
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001326 check_pending(ucs);
1327
1328 spin_lock_irqsave(&ucs->lock, flags);
1329 pending = ucs->pending;
1330 ucs->pending = 0;
1331 spin_unlock_irqrestore(&ucs->lock, flags);
1332
1333 switch (pending) {
1334 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001335 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001336 break;
1337
1338 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001339 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001340 error_reset(bcs->cs);
1341 break;
1342
1343 case HD_OPEN_B2CHANNEL:
1344 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001345 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1346 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001347 error_hangup(bcs);
1348 break;
1349
1350 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001351 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001352 break;
1353
1354 case HD_CLOSE_B2CHANNEL:
1355 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001356 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1357 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001358 break;
1359
1360 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001361 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1362 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001363 }
1364}
1365
1366/* write_ctrl_callback
1367 * USB completion handler for control pipe output
1368 * called by the USB subsystem in interrupt context
1369 * parameter:
1370 * urb USB request block of completed request
1371 * urb->context = hardware specific controller state structure
1372 */
1373static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1374{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001375 struct bas_cardstate *ucs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001376 unsigned long flags;
1377
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001378 spin_lock_irqsave(&ucs->lock, flags);
1379 if (urb->status && ucs->pending) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001380 dev_err(&ucs->interface->dev,
1381 "control request 0x%02x failed: %s\n",
1382 ucs->pending, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001383 del_timer(&ucs->timer_ctrl);
1384 ucs->pending = 0;
1385 }
1386 /* individual handling of specific request types */
1387 switch (ucs->pending) {
1388 case HD_DEVICE_INIT_ACK: /* no reply expected */
1389 ucs->pending = 0;
1390 break;
1391 }
1392 spin_unlock_irqrestore(&ucs->lock, flags);
1393}
1394
1395/* req_submit
1396 * submit a control output request without message buffer to the Gigaset base
1397 * and optionally start a timeout
1398 * parameters:
1399 * bcs B channel control structure
1400 * req control request code (HD_*)
1401 * val control request parameter value (set to 0 if unused)
1402 * timeout timeout in seconds (0: no timeout)
1403 * return value:
1404 * 0 on success
1405 * -EINVAL if a NULL pointer is encountered somewhere
1406 * -EBUSY if another request is pending
1407 * any URB submission error code
1408 */
1409static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1410{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001411 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001412 int ret;
1413 unsigned long flags;
1414
Tilman Schmidt784d5852006-04-10 22:55:04 -07001415 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001416
1417 spin_lock_irqsave(&ucs->lock, flags);
1418 if (ucs->pending) {
1419 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001420 dev_err(bcs->cs->dev,
1421 "submission of request 0x%02x failed: "
1422 "request 0x%02x still pending\n",
1423 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001424 return -EBUSY;
1425 }
1426 if (ucs->urb_ctrl->status == -EINPROGRESS) {
1427 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001428 dev_err(bcs->cs->dev,
1429 "could not submit request 0x%02x: URB busy\n", req);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001430 return -EBUSY;
1431 }
1432
1433 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1434 ucs->dr_ctrl.bRequest = req;
1435 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1436 ucs->dr_ctrl.wIndex = 0;
1437 ucs->dr_ctrl.wLength = 0;
1438 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001439 usb_sndctrlpipe(ucs->udev, 0),
1440 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1441 write_ctrl_callback, ucs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001442 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001443 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1444 req, get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001445 spin_unlock_irqrestore(&ucs->lock, flags);
1446 return ret;
1447 }
1448 ucs->pending = req;
1449
1450 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001451 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001452 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1453 ucs->timer_ctrl.data = (unsigned long) bcs;
1454 ucs->timer_ctrl.function = req_timeout;
1455 add_timer(&ucs->timer_ctrl);
1456 }
1457
1458 spin_unlock_irqrestore(&ucs->lock, flags);
1459 return 0;
1460}
1461
1462/* gigaset_init_bchannel
1463 * called by common.c to connect a B channel
1464 * initialize isochronous I/O and tell the Gigaset base to open the channel
1465 * argument:
1466 * B channel control structure
1467 * return value:
1468 * 0 on success, error code < 0 on error
1469 */
1470static int gigaset_init_bchannel(struct bc_state *bcs)
1471{
1472 int req, ret;
1473
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001474 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001475 dev_err(bcs->cs->dev,
1476 "could not start isochronous I/O for channel %d\n",
1477 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001478 error_hangup(bcs);
1479 return ret;
1480 }
1481
1482 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1483 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001484 dev_err(bcs->cs->dev, "could not open channel %d: %s\n",
1485 bcs->channel + 1, get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001486 stopurbs(bcs->hw.bas);
1487 error_hangup(bcs);
1488 }
1489 return ret;
1490}
1491
1492/* gigaset_close_bchannel
1493 * called by common.c to disconnect a B channel
1494 * tell the Gigaset base to close the channel
1495 * stopping isochronous I/O and LL notification will be done when the
1496 * acknowledgement for the close arrives
1497 * argument:
1498 * B channel control structure
1499 * return value:
1500 * 0 on success, error code < 0 on error
1501 */
1502static int gigaset_close_bchannel(struct bc_state *bcs)
1503{
1504 int req, ret;
1505
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001506 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1507 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1508 /* channel not running: just signal common.c */
1509 gigaset_bchannel_down(bcs);
1510 return 0;
1511 }
1512
1513 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1514 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidt784d5852006-04-10 22:55:04 -07001515 dev_err(bcs->cs->dev,
1516 "could not submit HD_CLOSE_BxCHANNEL request: %s\n",
1517 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001518 return ret;
1519}
1520
1521/* Device Operations */
1522/* ================= */
1523
1524/* complete_cb
1525 * unqueue first command buffer from queue, waking any sleepers
1526 * must be called with cs->cmdlock held
1527 * parameter:
1528 * cs controller state structure
1529 */
1530static void complete_cb(struct cardstate *cs)
1531{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001532 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001533
1534 /* unqueue completed buffer */
1535 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001536 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1537 "write_command: sent %u bytes, %u left",
1538 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001539 if ((cs->cmdbuf = cb->next) != NULL) {
1540 cs->cmdbuf->prev = NULL;
1541 cs->curlen = cs->cmdbuf->len;
1542 } else {
1543 cs->lastcmdbuf = NULL;
1544 cs->curlen = 0;
1545 }
1546
1547 if (cb->wake_tasklet)
1548 tasklet_schedule(cb->wake_tasklet);
1549
1550 kfree(cb);
1551}
1552
1553static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len);
1554
1555/* write_command_callback
1556 * USB completion handler for AT command transmission
1557 * called by the USB subsystem in interrupt context
1558 * parameter:
1559 * urb USB request block of completed request
1560 * urb->context = controller state structure
1561 */
1562static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1563{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001564 struct cardstate *cs = urb->context;
1565 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001566 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001567
1568 /* check status */
1569 switch (urb->status) {
1570 case 0: /* normal completion */
1571 break;
1572 case -ENOENT: /* canceled */
1573 case -ECONNRESET: /* canceled (async) */
1574 case -EINPROGRESS: /* pending */
1575 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001576 gig_dbg(DEBUG_USBREQ, "%s: %s",
1577 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001578 return;
1579 default: /* any failure */
1580 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001581 dev_warn(cs->dev,
1582 "command write: %s, "
1583 "giving up after %d retries\n",
1584 get_usb_statmsg(urb->status),
1585 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001586 break;
1587 }
1588 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001589 dev_warn(cs->dev,
1590 "command write: %s, "
1591 "cannot retry - cmdbuf gone\n",
1592 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001593 break;
1594 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001595 dev_notice(cs->dev, "command write: %s, retry %d\n",
1596 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001597 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1598 /* resubmitted - bypass regular exit block */
1599 return;
1600 /* command send failed, assume base still waiting */
1601 update_basstate(ucs, BS_ATREADY, 0);
1602 }
1603
1604 spin_lock_irqsave(&cs->cmdlock, flags);
1605 if (cs->cmdbuf != NULL)
1606 complete_cb(cs);
1607 spin_unlock_irqrestore(&cs->cmdlock, flags);
1608}
1609
1610/* atrdy_timeout
1611 * timeout routine for AT command transmission
1612 * argument:
1613 * controller state structure
1614 */
1615static void atrdy_timeout(unsigned long data)
1616{
1617 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001618 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001619
Tilman Schmidt784d5852006-04-10 22:55:04 -07001620 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001621
1622 /* fake the missing signal - what else can I do? */
1623 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1624 start_cbsend(cs);
1625}
1626
1627/* atwrite_submit
1628 * submit an HD_WRITE_ATMESSAGE command URB
1629 * parameters:
1630 * cs controller state structure
1631 * buf buffer containing command to send
1632 * len length of command to send
1633 * return value:
1634 * 0 on success
1635 * -EFAULT if a NULL pointer is encountered somewhere
1636 * -EBUSY if another request is pending
1637 * any URB submission error code
1638 */
1639static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1640{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001641 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001642 int ret;
1643
Tilman Schmidt784d5852006-04-10 22:55:04 -07001644 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001645
1646 if (ucs->urb_cmd_out->status == -EINPROGRESS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001647 dev_err(cs->dev,
1648 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001649 return -EBUSY;
1650 }
1651
1652 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1653 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1654 ucs->dr_cmd_out.wValue = 0;
1655 ucs->dr_cmd_out.wIndex = 0;
1656 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1657 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1658 usb_sndctrlpipe(ucs->udev, 0),
1659 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1660 write_command_callback, cs);
1661
1662 if ((ret = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001663 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1664 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001665 return ret;
1666 }
1667
1668 /* submitted successfully */
1669 update_basstate(ucs, 0, BS_ATREADY);
1670
1671 /* start timeout if necessary */
1672 if (!(atomic_read(&ucs->basstate) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001673 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1674 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001675 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1676 ucs->timer_atrdy.data = (unsigned long) cs;
1677 ucs->timer_atrdy.function = atrdy_timeout;
1678 add_timer(&ucs->timer_atrdy);
1679 update_basstate(ucs, BS_ATTIMER, 0);
1680 }
1681 return 0;
1682}
1683
1684/* start_cbsend
1685 * start transmission of AT command queue if necessary
1686 * parameter:
1687 * cs controller state structure
1688 * return value:
1689 * 0 on success
1690 * error code < 0 on error
1691 */
1692static int start_cbsend(struct cardstate *cs)
1693{
1694 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001695 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001696 unsigned long flags;
1697 int rc;
1698 int retval = 0;
1699
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001700 /* check if AT channel is open */
1701 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001702 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001703 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1704 if (rc < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001705 dev_err(cs->dev, "could not open AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001706 /* flush command queue */
1707 spin_lock_irqsave(&cs->cmdlock, flags);
1708 while (cs->cmdbuf != NULL)
1709 complete_cb(cs);
1710 spin_unlock_irqrestore(&cs->cmdlock, flags);
1711 }
1712 return rc;
1713 }
1714
1715 /* try to send first command in queue */
1716 spin_lock_irqsave(&cs->cmdlock, flags);
1717
1718 while ((cb = cs->cmdbuf) != NULL &&
1719 atomic_read(&ucs->basstate) & BS_ATREADY) {
1720 ucs->retry_cmd_out = 0;
1721 rc = atwrite_submit(cs, cb->buf, cb->len);
1722 if (unlikely(rc)) {
1723 retval = rc;
1724 complete_cb(cs);
1725 }
1726 }
1727
1728 spin_unlock_irqrestore(&cs->cmdlock, flags);
1729 return retval;
1730}
1731
1732/* gigaset_write_cmd
1733 * This function is called by the device independent part of the driver
1734 * to transmit an AT command string to the Gigaset device.
1735 * It encapsulates the device specific method for transmission over the
1736 * direct USB connection to the base.
1737 * The command string is added to the queue of commands to send, and
1738 * USB transmission is started if necessary.
1739 * parameters:
1740 * cs controller state structure
1741 * buf command string to send
1742 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001743 * wake_tasklet tasklet to run when transmission is completed
1744 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001745 * return value:
1746 * number of bytes queued on success
1747 * error code < 0 on error
1748 */
1749static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001750 const unsigned char *buf, int len,
1751 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001752{
1753 struct cmdbuf_t *cb;
1754 unsigned long flags;
1755 int status;
1756
1757 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001758 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001759 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001760
Tilman Schmidt784d5852006-04-10 22:55:04 -07001761 if (unlikely(!atomic_read(&cs->connected))) {
1762 err("%s: disconnected", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001763 return -ENODEV;
1764 }
1765
1766 if (len <= 0)
1767 return 0; /* nothing to do */
1768
1769 if (len > IF_WRITEBUF)
1770 len = IF_WRITEBUF;
1771 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001772 dev_err(cs->dev, "%s: out of memory\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001773 return -ENOMEM;
1774 }
1775
1776 memcpy(cb->buf, buf, len);
1777 cb->len = len;
1778 cb->offset = 0;
1779 cb->next = NULL;
1780 cb->wake_tasklet = wake_tasklet;
1781
1782 spin_lock_irqsave(&cs->cmdlock, flags);
1783 cb->prev = cs->lastcmdbuf;
1784 if (cs->lastcmdbuf)
1785 cs->lastcmdbuf->next = cb;
1786 else {
1787 cs->cmdbuf = cb;
1788 cs->curlen = len;
1789 }
1790 cs->cmdbytes += len;
1791 cs->lastcmdbuf = cb;
1792 spin_unlock_irqrestore(&cs->cmdlock, flags);
1793
1794 status = start_cbsend(cs);
1795
1796 return status < 0 ? status : len;
1797}
1798
1799/* gigaset_write_room
1800 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001801 * return number of characters the driver will accept to be written via
1802 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001803 * parameter:
1804 * controller state structure
1805 * return value:
1806 * number of characters
1807 */
1808static int gigaset_write_room(struct cardstate *cs)
1809{
1810 return IF_WRITEBUF;
1811}
1812
1813/* gigaset_chars_in_buffer
1814 * tty_driver.chars_in_buffer interface routine
1815 * return number of characters waiting to be sent
1816 * parameter:
1817 * controller state structure
1818 * return value:
1819 * number of characters
1820 */
1821static int gigaset_chars_in_buffer(struct cardstate *cs)
1822{
1823 unsigned long flags;
1824 unsigned bytes;
1825
1826 spin_lock_irqsave(&cs->cmdlock, flags);
1827 bytes = cs->cmdbytes;
1828 spin_unlock_irqrestore(&cs->cmdlock, flags);
1829
1830 return bytes;
1831}
1832
1833/* gigaset_brkchars
1834 * implementation of ioctl(GIGASET_BRKCHARS)
1835 * parameter:
1836 * controller state structure
1837 * return value:
1838 * -EINVAL (unimplemented function)
1839 */
1840static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1841{
1842 return -EINVAL;
1843}
1844
1845
1846/* Device Initialization/Shutdown */
1847/* ============================== */
1848
1849/* Free hardware dependent part of the B channel structure
1850 * parameter:
1851 * bcs B channel structure
1852 * return value:
1853 * !=0 on success
1854 */
1855static int gigaset_freebcshw(struct bc_state *bcs)
1856{
1857 if (!bcs->hw.bas)
1858 return 0;
1859
1860 if (bcs->hw.bas->isooutbuf)
1861 kfree(bcs->hw.bas->isooutbuf);
1862 kfree(bcs->hw.bas);
1863 bcs->hw.bas = NULL;
1864 return 1;
1865}
1866
1867/* Initialize hardware dependent part of the B channel structure
1868 * parameter:
1869 * bcs B channel structure
1870 * return value:
1871 * !=0 on success
1872 */
1873static int gigaset_initbcshw(struct bc_state *bcs)
1874{
1875 int i;
1876 struct bas_bc_state *ubc;
1877
1878 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1879 if (!ubc) {
1880 err("could not allocate bas_bc_state");
1881 return 0;
1882 }
1883
1884 atomic_set(&ubc->running, 0);
1885 atomic_set(&ubc->corrbytes, 0);
1886 spin_lock_init(&ubc->isooutlock);
1887 for (i = 0; i < BAS_OUTURBS; ++i) {
1888 ubc->isoouturbs[i].urb = NULL;
1889 ubc->isoouturbs[i].bcs = bcs;
1890 }
1891 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1892 ubc->numsub = 0;
1893 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
1894 err("could not allocate isochronous output buffer");
1895 kfree(ubc);
1896 bcs->hw.bas = NULL;
1897 return 0;
1898 }
1899 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001900 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001901
1902 spin_lock_init(&ubc->isoinlock);
1903 for (i = 0; i < BAS_INURBS; ++i)
1904 ubc->isoinurbs[i] = NULL;
1905 ubc->isoindone = NULL;
1906 ubc->loststatus = -EINPROGRESS;
1907 ubc->isoinlost = 0;
1908 ubc->seqlen = 0;
1909 ubc->inbyte = 0;
1910 ubc->inbits = 0;
1911 ubc->goodbytes = 0;
1912 ubc->alignerrs = 0;
1913 ubc->fcserrs = 0;
1914 ubc->frameerrs = 0;
1915 ubc->giants = 0;
1916 ubc->runts = 0;
1917 ubc->aborts = 0;
1918 ubc->shared0s = 0;
1919 ubc->stolen0s = 0;
1920 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001921 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001922 return 1;
1923}
1924
1925static void gigaset_reinitbcshw(struct bc_state *bcs)
1926{
1927 struct bas_bc_state *ubc = bcs->hw.bas;
1928
1929 atomic_set(&bcs->hw.bas->running, 0);
1930 atomic_set(&bcs->hw.bas->corrbytes, 0);
1931 bcs->hw.bas->numsub = 0;
1932 spin_lock_init(&ubc->isooutlock);
1933 spin_lock_init(&ubc->isoinlock);
1934 ubc->loststatus = -EINPROGRESS;
1935}
1936
1937static void gigaset_freecshw(struct cardstate *cs)
1938{
1939 struct bas_cardstate *ucs = cs->hw.bas;
1940
1941 del_timer(&ucs->timer_ctrl);
1942 del_timer(&ucs->timer_atrdy);
1943 del_timer(&ucs->timer_cmd_in);
1944
1945 kfree(cs->hw.bas);
1946}
1947
1948static int gigaset_initcshw(struct cardstate *cs)
1949{
1950 struct bas_cardstate *ucs;
1951
1952 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
1953 if (!ucs)
1954 return 0;
1955
1956 ucs->urb_cmd_in = NULL;
1957 ucs->urb_cmd_out = NULL;
1958 ucs->rcvbuf = NULL;
1959 ucs->rcvbuf_size = 0;
1960
1961 spin_lock_init(&ucs->lock);
1962 ucs->pending = 0;
1963
1964 atomic_set(&ucs->basstate, 0);
1965 init_timer(&ucs->timer_ctrl);
1966 init_timer(&ucs->timer_atrdy);
1967 init_timer(&ucs->timer_cmd_in);
1968
1969 return 1;
1970}
1971
1972/* freeurbs
1973 * unlink and deallocate all URBs unconditionally
1974 * caller must make sure that no commands are still in progress
1975 * parameter:
1976 * cs controller state structure
1977 */
1978static void freeurbs(struct cardstate *cs)
1979{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001980 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001981 struct bas_bc_state *ubc;
1982 int i, j;
1983
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001984 for (j = 0; j < 2; ++j) {
1985 ubc = cs->bcs[j].hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001986 for (i = 0; i < BAS_OUTURBS; ++i)
1987 if (ubc->isoouturbs[i].urb) {
1988 usb_kill_urb(ubc->isoouturbs[i].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001989 gig_dbg(DEBUG_INIT,
1990 "%s: isoc output URB %d/%d unlinked",
1991 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001992 usb_free_urb(ubc->isoouturbs[i].urb);
1993 ubc->isoouturbs[i].urb = NULL;
1994 }
1995 for (i = 0; i < BAS_INURBS; ++i)
1996 if (ubc->isoinurbs[i]) {
1997 usb_kill_urb(ubc->isoinurbs[i]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001998 gig_dbg(DEBUG_INIT,
1999 "%s: isoc input URB %d/%d unlinked",
2000 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002001 usb_free_urb(ubc->isoinurbs[i]);
2002 ubc->isoinurbs[i] = NULL;
2003 }
2004 }
2005 if (ucs->urb_int_in) {
2006 usb_kill_urb(ucs->urb_int_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002007 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2008 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002009 usb_free_urb(ucs->urb_int_in);
2010 ucs->urb_int_in = NULL;
2011 }
2012 if (ucs->urb_cmd_out) {
2013 usb_kill_urb(ucs->urb_cmd_out);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002014 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2015 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002016 usb_free_urb(ucs->urb_cmd_out);
2017 ucs->urb_cmd_out = NULL;
2018 }
2019 if (ucs->urb_cmd_in) {
2020 usb_kill_urb(ucs->urb_cmd_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002021 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2022 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002023 usb_free_urb(ucs->urb_cmd_in);
2024 ucs->urb_cmd_in = NULL;
2025 }
2026 if (ucs->urb_ctrl) {
2027 usb_kill_urb(ucs->urb_ctrl);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002028 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2029 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002030 usb_free_urb(ucs->urb_ctrl);
2031 ucs->urb_ctrl = NULL;
2032 }
2033}
2034
2035/* gigaset_probe
2036 * This function is called when a new USB device is connected.
2037 * It checks whether the new device is handled by this driver.
2038 */
2039static int gigaset_probe(struct usb_interface *interface,
2040 const struct usb_device_id *id)
2041{
2042 struct usb_host_interface *hostif;
2043 struct usb_device *udev = interface_to_usbdev(interface);
2044 struct cardstate *cs = NULL;
2045 struct bas_cardstate *ucs = NULL;
2046 struct bas_bc_state *ubc;
2047 struct usb_endpoint_descriptor *endpoint;
2048 int i, j;
2049 int ret;
2050
Tilman Schmidt784d5852006-04-10 22:55:04 -07002051 gig_dbg(DEBUG_ANY,
2052 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2053 __func__, le16_to_cpu(udev->descriptor.idVendor),
2054 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002055
2056 /* See if the device offered us matches what we can accept */
2057 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_GIGA_VENDOR_ID) ||
2058 (le16_to_cpu(udev->descriptor.idProduct) != USB_GIGA_PRODUCT_ID &&
2059 le16_to_cpu(udev->descriptor.idProduct) != USB_4175_PRODUCT_ID &&
2060 le16_to_cpu(udev->descriptor.idProduct) != USB_SX303_PRODUCT_ID &&
2061 le16_to_cpu(udev->descriptor.idProduct) != USB_SX353_PRODUCT_ID)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002062 gig_dbg(DEBUG_ANY, "%s: unmatched ID - exiting", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002063 return -ENODEV;
2064 }
2065
2066 /* set required alternate setting */
2067 hostif = interface->cur_altsetting;
2068 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002069 gig_dbg(DEBUG_ANY,
2070 "%s: wrong alternate setting %d - trying to switch",
2071 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002072 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002073 dev_warn(&udev->dev, "usb_set_interface failed, "
2074 "device %d interface %d altsetting %d\n",
2075 udev->devnum, hostif->desc.bInterfaceNumber,
2076 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002077 return -ENODEV;
2078 }
2079 hostif = interface->cur_altsetting;
2080 }
2081
2082 /* Reject application specific interfaces
2083 */
2084 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002085 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2086 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002087 return -ENODEV;
2088 }
2089
Tilman Schmidt784d5852006-04-10 22:55:04 -07002090 dev_info(&udev->dev,
2091 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2092 __func__, le16_to_cpu(udev->descriptor.idVendor),
2093 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002094
2095 cs = gigaset_getunassignedcs(driver);
2096 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002097 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002098 return -ENODEV;
2099 }
2100 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002101
2102 /* save off device structure ptrs for later use */
2103 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002104 ucs->udev = udev;
2105 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002106 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002107
2108 /* allocate URBs:
2109 * - one for the interrupt pipe
2110 * - three for the different uses of the default control pipe
2111 * - three for each isochronous pipe
2112 */
2113 ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL);
2114 if (!ucs->urb_int_in) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002115 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002116 goto error;
2117 }
2118 ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL);
2119 if (!ucs->urb_cmd_in) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002120 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002121 goto error;
2122 }
2123 ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL);
2124 if (!ucs->urb_cmd_out) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002125 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002126 goto error;
2127 }
2128 ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL);
2129 if (!ucs->urb_ctrl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002130 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002131 goto error;
2132 }
2133
2134 for (j = 0; j < 2; ++j) {
2135 ubc = cs->bcs[j].hw.bas;
2136 for (i = 0; i < BAS_OUTURBS; ++i) {
2137 ubc->isoouturbs[i].urb =
2138 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2139 if (!ubc->isoouturbs[i].urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002140 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002141 goto error;
2142 }
2143 }
2144 for (i = 0; i < BAS_INURBS; ++i) {
2145 ubc->isoinurbs[i] =
2146 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2147 if (!ubc->isoinurbs[i]) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002148 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002149 goto error;
2150 }
2151 }
2152 }
2153
2154 ucs->rcvbuf = NULL;
2155 ucs->rcvbuf_size = 0;
2156
2157 /* Fill the interrupt urb and send it to the core */
2158 endpoint = &hostif->endpoint[0].desc;
2159 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002160 usb_rcvintpipe(udev,
2161 (endpoint->bEndpointAddress) & 0x0f),
2162 ucs->int_in_buf, 3, read_int_callback, cs,
2163 endpoint->bInterval);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002164 ret = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL);
2165 if (ret) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002166 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2167 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002168 goto error;
2169 }
2170
2171 /* tell the device that the driver is ready */
2172 if ((ret = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2173 goto error;
2174
2175 /* tell common part that the device is ready */
2176 if (startmode == SM_LOCKED)
2177 atomic_set(&cs->mstate, MS_LOCKED);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002178
2179 /* save address of controller structure */
2180 usb_set_intfdata(interface, cs);
2181
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002182 if (!gigaset_start(cs))
2183 goto error;
2184
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002185 return 0;
2186
2187error:
2188 freeurbs(cs);
2189 gigaset_unassign(cs);
2190 return -ENODEV;
2191}
2192
2193/* gigaset_disconnect
2194 * This function is called when the Gigaset base is unplugged.
2195 */
2196static void gigaset_disconnect(struct usb_interface *interface)
2197{
2198 struct cardstate *cs;
2199 struct bas_cardstate *ucs;
2200
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002201 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002202
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002203 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002204
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002205 dev_info(cs->dev, "disconnecting Gigaset base\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002206 gigaset_stop(cs);
2207 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002208 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002209 kfree(ucs->rcvbuf);
2210 ucs->rcvbuf = NULL;
2211 ucs->rcvbuf_size = 0;
2212 atomic_set(&ucs->basstate, 0);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002213 usb_put_dev(ucs->udev);
2214 ucs->interface = NULL;
2215 ucs->udev = NULL;
2216 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002217 gigaset_unassign(cs);
2218}
2219
2220static struct gigaset_ops gigops = {
2221 gigaset_write_cmd,
2222 gigaset_write_room,
2223 gigaset_chars_in_buffer,
2224 gigaset_brkchars,
2225 gigaset_init_bchannel,
2226 gigaset_close_bchannel,
2227 gigaset_initbcshw,
2228 gigaset_freebcshw,
2229 gigaset_reinitbcshw,
2230 gigaset_initcshw,
2231 gigaset_freecshw,
2232 gigaset_set_modem_ctrl,
2233 gigaset_baud_rate,
2234 gigaset_set_line_ctrl,
2235 gigaset_isoc_send_skb,
2236 gigaset_isoc_input,
2237};
2238
2239/* bas_gigaset_init
2240 * This function is called after the kernel module is loaded.
2241 */
2242static int __init bas_gigaset_init(void)
2243{
2244 int result;
2245
2246 /* allocate memory for our driver state and intialize it */
2247 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002248 GIGASET_MODULENAME, GIGASET_DEVNAME,
2249 GIGASET_DEVFSNAME, &gigops,
2250 THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002251 goto error;
2252
2253 /* allocate memory for our device state and intialize it */
Tilman Schmidt917f5082006-04-10 22:55:00 -07002254 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2255 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002256 if (!cardstate)
2257 goto error;
2258
2259 /* register this driver with the USB subsystem */
2260 result = usb_register(&gigaset_usb_driver);
2261 if (result < 0) {
2262 err("usb_register failed (error %d)", -result);
2263 goto error;
2264 }
2265
2266 info(DRIVER_AUTHOR);
2267 info(DRIVER_DESC);
2268 return 0;
2269
2270error: if (cardstate)
2271 gigaset_freecs(cardstate);
2272 cardstate = NULL;
2273 if (driver)
2274 gigaset_freedriver(driver);
2275 driver = NULL;
2276 return -1;
2277}
2278
2279/* bas_gigaset_exit
2280 * This function is called before the kernel module is unloaded.
2281 */
2282static void __exit bas_gigaset_exit(void)
2283{
2284 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002285 * => no gigaset_start any more
2286 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002287
2288 gigaset_shutdown(cardstate);
2289 /* from now on, no isdn callback should be possible */
2290
2291 if (atomic_read(&cardstate->hw.bas->basstate) & BS_ATOPEN) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002292 gig_dbg(DEBUG_ANY, "closing AT channel");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002293 if (req_submit(cardstate->bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002294 HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT) >= 0) {
2295 /* successfully submitted */
2296 //FIXME wait for completion?
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002297 }
2298 }
2299
2300 /* deregister this driver with the USB subsystem */
2301 usb_deregister(&gigaset_usb_driver);
2302 /* this will call the disconnect-callback */
2303 /* from now on, no disconnect/probe callback should be running */
2304
2305 gigaset_freecs(cardstate);
2306 cardstate = NULL;
2307 gigaset_freedriver(driver);
2308 driver = NULL;
2309}
2310
2311
2312module_init(bas_gigaset_init);
2313module_exit(bas_gigaset_exit);
2314
2315MODULE_AUTHOR(DRIVER_AUTHOR);
2316MODULE_DESCRIPTION(DRIVER_DESC);
2317MODULE_LICENSE("GPL");