blob: f86ed6af3aa2d5fb68575cd4a78c2fb8082034ba [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 Schmidt69049cc2006-04-10 22:55:16 -0700370 if (unlikely(!cs->connected)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700371 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 switch (urb->status) {
479 case 0: /* success */
480 break;
481 case -ENOENT: /* canceled */
482 case -ECONNRESET: /* canceled (async) */
483 case -EINPROGRESS: /* pending */
484 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700485 gig_dbg(DEBUG_USBREQ, "%s: %s",
486 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800487 return;
488 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700489 dev_warn(cs->dev, "interrupt read: %s\n",
490 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800491 //FIXME corrective action? resubmission always ok?
492 goto resubmit;
493 }
494
495 l = (unsigned) ucs->int_in_buf[1] +
496 (((unsigned) ucs->int_in_buf[2]) << 8);
497
Tilman Schmidt784d5852006-04-10 22:55:04 -0700498 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
499 urb->actual_length, (int)ucs->int_in_buf[0], l,
500 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800501
502 channel = 0;
503
504 switch (ucs->int_in_buf[0]) {
505 case HD_DEVICE_INIT_OK:
506 update_basstate(ucs, BS_INIT, 0);
507 break;
508
509 case HD_READY_SEND_ATDATA:
510 del_timer(&ucs->timer_atrdy);
511 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
512 start_cbsend(cs);
513 break;
514
515 case HD_OPEN_B2CHANNEL_ACK:
516 ++channel;
517 case HD_OPEN_B1CHANNEL_ACK:
518 bcs = cs->bcs + channel;
519 update_basstate(ucs, BS_B1OPEN << channel, 0);
520 gigaset_bchannel_up(bcs);
521 break;
522
523 case HD_OPEN_ATCHANNEL_ACK:
524 update_basstate(ucs, BS_ATOPEN, 0);
525 start_cbsend(cs);
526 break;
527
528 case HD_CLOSE_B2CHANNEL_ACK:
529 ++channel;
530 case HD_CLOSE_B1CHANNEL_ACK:
531 bcs = cs->bcs + channel;
532 update_basstate(ucs, 0, BS_B1OPEN << channel);
533 stopurbs(bcs->hw.bas);
534 gigaset_bchannel_down(bcs);
535 break;
536
537 case HD_CLOSE_ATCHANNEL_ACK:
538 update_basstate(ucs, 0, BS_ATOPEN);
539 break;
540
541 case HD_B2_FLOW_CONTROL:
542 ++channel;
543 case HD_B1_FLOW_CONTROL:
544 bcs = cs->bcs + channel;
545 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700546 &bcs->hw.bas->corrbytes);
547 gig_dbg(DEBUG_ISO,
548 "Flow control (channel %d, sub %d): 0x%02x => %d",
549 channel, bcs->hw.bas->numsub, l,
550 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800551 break;
552
553 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
554 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700555 dev_warn(cs->dev,
556 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800557 break;
558 }
559 spin_lock_irqsave(&cs->lock, flags);
560 if (ucs->rcvbuf_size) {
561 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700562 dev_err(cs->dev,
563 "receive AT data overrun, %d bytes lost\n", l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800564 error_reset(cs); //FIXME reschedule
565 break;
566 }
567 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
568 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700569 dev_err(cs->dev, "out of memory, %d bytes lost\n", l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800570 error_reset(cs); //FIXME reschedule
571 break;
572 }
573 ucs->rcvbuf_size = l;
574 ucs->retry_cmd_in = 0;
575 if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
576 kfree(ucs->rcvbuf);
577 ucs->rcvbuf = NULL;
578 ucs->rcvbuf_size = 0;
579 error_reset(cs); //FIXME reschedule
580 }
581 spin_unlock_irqrestore(&cs->lock, flags);
582 break;
583
584 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700585 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800586 break;
587
588 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700589 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800590 break;
591
592 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700593 dev_warn(cs->dev,
594 "unknown Gigaset signal 0x%02x (%u) ignored\n",
595 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800596 }
597
598 check_pending(ucs);
599
600resubmit:
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700601 spin_lock_irqsave(&cs->lock, flags);
602 status = cs->connected ? usb_submit_urb(urb, SLAB_ATOMIC) : -ENODEV;
603 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800604 if (unlikely(status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700605 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
606 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800607 error_reset(cs);
608 }
609}
610
611/* read_ctrl_callback
612 * USB completion handler for control pipe input
613 * called by the USB subsystem in interrupt context
614 * parameter:
615 * urb USB request block
616 * urb->context = inbuf structure for controller state
617 */
618static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
619{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700620 struct inbuf_t *inbuf = urb->context;
621 struct cardstate *cs = inbuf->cs;
622 struct bas_cardstate *ucs = cs->hw.bas;
623 int have_data = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800624 unsigned numbytes;
625 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800626
627 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700628 if (unlikely(!cs->connected)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800629 warn("%s: disconnected", __func__);
630 spin_unlock_irqrestore(&cs->lock, flags);
631 return;
632 }
633
634 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700635 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800636 spin_unlock_irqrestore(&cs->lock, flags);
637 return;
638 }
639
640 del_timer(&ucs->timer_cmd_in);
641
642 switch (urb->status) {
643 case 0: /* normal completion */
644 numbytes = urb->actual_length;
645 if (unlikely(numbytes == 0)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700646 dev_warn(cs->dev,
647 "control read: empty block received\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800648 goto retry;
649 }
650 if (unlikely(numbytes != ucs->rcvbuf_size)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700651 dev_warn(cs->dev,
652 "control read: received %d chars, expected %d\n",
653 numbytes, ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800654 if (numbytes > ucs->rcvbuf_size)
655 numbytes = ucs->rcvbuf_size;
656 }
657
658 /* copy received bytes to inbuf */
659 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
660
661 if (unlikely(numbytes < ucs->rcvbuf_size)) {
662 /* incomplete - resubmit for remaining bytes */
663 ucs->rcvbuf_size -= numbytes;
664 ucs->retry_cmd_in = 0;
665 goto retry;
666 }
667 break;
668
669 case -ENOENT: /* canceled */
670 case -ECONNRESET: /* canceled (async) */
671 case -EINPROGRESS: /* pending */
672 /* no action necessary */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700673 gig_dbg(DEBUG_USBREQ, "%s: %s",
674 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800675 break;
676
677 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700678 dev_warn(cs->dev, "control read: %s\n",
679 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800680 retry:
681 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700682 dev_notice(cs->dev, "control read: retry %d\n",
683 ucs->retry_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800684 if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
685 /* resubmitted - bypass regular exit block */
686 spin_unlock_irqrestore(&cs->lock, flags);
687 return;
688 }
689 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700690 dev_err(cs->dev,
691 "control read: giving up after %d tries\n",
692 ucs->retry_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800693 }
694 error_reset(cs);
695 }
696
697 kfree(ucs->rcvbuf);
698 ucs->rcvbuf = NULL;
699 ucs->rcvbuf_size = 0;
700 spin_unlock_irqrestore(&cs->lock, flags);
701 if (have_data) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700702 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800703 gigaset_schedule_event(cs);
704 }
705}
706
707/* read_iso_callback
708 * USB completion handler for B channel isochronous input
709 * called by the USB subsystem in interrupt context
710 * parameter:
711 * urb USB request block of completed request
712 * urb->context = bc_state structure
713 */
714static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
715{
716 struct bc_state *bcs;
717 struct bas_bc_state *ubc;
718 unsigned long flags;
719 int i, rc;
720
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800721 /* status codes not worth bothering the tasklet with */
722 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
Tilman Schmidt784d5852006-04-10 22:55:04 -0700723 urb->status == -EINPROGRESS)) {
724 gig_dbg(DEBUG_ISO, "%s: %s",
725 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800726 return;
727 }
728
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700729 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800730 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800731
732 spin_lock_irqsave(&ubc->isoinlock, flags);
733 if (likely(ubc->isoindone == NULL)) {
734 /* pass URB to tasklet */
735 ubc->isoindone = urb;
736 tasklet_schedule(&ubc->rcvd_tasklet);
737 } else {
738 /* tasklet still busy, drop data and resubmit URB */
739 ubc->loststatus = urb->status;
740 for (i = 0; i < BAS_NUMFRAMES; i++) {
741 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
742 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
743 urb->iso_frame_desc[i].status != -EINPROGRESS)) {
744 ubc->loststatus = urb->iso_frame_desc[i].status;
745 }
746 urb->iso_frame_desc[i].status = 0;
747 urb->iso_frame_desc[i].actual_length = 0;
748 }
749 if (likely(atomic_read(&ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700750 /* urb->dev is clobbered by USB subsystem */
751 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800752 urb->transfer_flags = URB_ISO_ASAP;
753 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700754 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
755 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800756 rc = usb_submit_urb(urb, SLAB_ATOMIC);
757 if (unlikely(rc != 0)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700758 dev_err(bcs->cs->dev,
759 "could not resubmit isochronous read "
760 "URB: %s\n", get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800761 dump_urb(DEBUG_ISO, "isoc read", urb);
762 error_hangup(bcs);
763 }
764 }
765 }
766 spin_unlock_irqrestore(&ubc->isoinlock, flags);
767}
768
769/* write_iso_callback
770 * USB completion handler for B channel isochronous output
771 * called by the USB subsystem in interrupt context
772 * parameter:
773 * urb USB request block of completed request
774 * urb->context = isow_urbctx_t structure
775 */
776static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
777{
778 struct isow_urbctx_t *ucx;
779 struct bas_bc_state *ubc;
780 unsigned long flags;
781
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800782 /* status codes not worth bothering the tasklet with */
783 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
Tilman Schmidt784d5852006-04-10 22:55:04 -0700784 urb->status == -EINPROGRESS)) {
785 gig_dbg(DEBUG_ISO, "%s: %s",
786 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800787 return;
788 }
789
790 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700791 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800792 ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800793
794 spin_lock_irqsave(&ubc->isooutlock, flags);
795 ubc->isooutovfl = ubc->isooutdone;
796 ubc->isooutdone = ucx;
797 spin_unlock_irqrestore(&ubc->isooutlock, flags);
798 tasklet_schedule(&ubc->sent_tasklet);
799}
800
801/* starturbs
802 * prepare and submit USB request blocks for isochronous input and output
803 * argument:
804 * B channel control structure
805 * return value:
806 * 0 on success
807 * < 0 on error (no URBs submitted)
808 */
809static int starturbs(struct bc_state *bcs)
810{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700811 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800812 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800813 int j, k;
814 int rc;
815
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800816 /* initialize L2 reception */
817 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
818 bcs->inputstate |= INS_flag_hunt;
819
820 /* submit all isochronous input URBs */
821 atomic_set(&ubc->running, 1);
822 for (k = 0; k < BAS_INURBS; k++) {
823 urb = ubc->isoinurbs[k];
824 if (!urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700825 dev_err(bcs->cs->dev, "isoinurbs[%d]==NULL\n", k);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800826 rc = -EFAULT;
827 goto error;
828 }
829
830 urb->dev = bcs->cs->hw.bas->udev;
831 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
832 urb->transfer_flags = URB_ISO_ASAP;
833 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
834 urb->transfer_buffer_length = BAS_INBUFSIZE;
835 urb->number_of_packets = BAS_NUMFRAMES;
836 urb->interval = BAS_FRAMETIME;
837 urb->complete = read_iso_callback;
838 urb->context = bcs;
839 for (j = 0; j < BAS_NUMFRAMES; j++) {
840 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
841 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
842 urb->iso_frame_desc[j].status = 0;
843 urb->iso_frame_desc[j].actual_length = 0;
844 }
845
846 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
847 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700848 dev_err(bcs->cs->dev,
849 "could not submit isochronous read URB %d: %s\n",
850 k, get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800851 goto error;
852 }
853 }
854
855 /* initialize L2 transmission */
856 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
857
858 /* set up isochronous output URBs for flag idling */
859 for (k = 0; k < BAS_OUTURBS; ++k) {
860 urb = ubc->isoouturbs[k].urb;
861 if (!urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700862 dev_err(bcs->cs->dev, "isoouturbs[%d].urb==NULL\n", k);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800863 rc = -EFAULT;
864 goto error;
865 }
866 urb->dev = bcs->cs->hw.bas->udev;
867 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
868 urb->transfer_flags = URB_ISO_ASAP;
869 urb->transfer_buffer = ubc->isooutbuf->data;
870 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
871 urb->number_of_packets = BAS_NUMFRAMES;
872 urb->interval = BAS_FRAMETIME;
873 urb->complete = write_iso_callback;
874 urb->context = &ubc->isoouturbs[k];
875 for (j = 0; j < BAS_NUMFRAMES; ++j) {
876 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
877 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
878 urb->iso_frame_desc[j].status = 0;
879 urb->iso_frame_desc[j].actual_length = 0;
880 }
881 ubc->isoouturbs[k].limit = -1;
882 }
883
884 /* submit two URBs, keep third one */
885 for (k = 0; k < 2; ++k) {
886 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
887 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
888 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700889 dev_err(bcs->cs->dev,
890 "could not submit isochronous write URB %d: %s\n",
891 k, get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800892 goto error;
893 }
894 }
895 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
896 ubc->isooutfree = &ubc->isoouturbs[2];
897 ubc->isooutdone = ubc->isooutovfl = NULL;
898 return 0;
899 error:
900 stopurbs(ubc);
901 return rc;
902}
903
904/* stopurbs
905 * cancel the USB request blocks for isochronous input and output
906 * errors are silently ignored
907 * argument:
908 * B channel control structure
909 */
910static void stopurbs(struct bas_bc_state *ubc)
911{
912 int k, rc;
913
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800914 atomic_set(&ubc->running, 0);
915
916 for (k = 0; k < BAS_INURBS; ++k) {
917 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700918 gig_dbg(DEBUG_ISO,
919 "%s: isoc input URB %d unlinked, result = %d",
920 __func__, k, rc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800921 }
922
923 for (k = 0; k < BAS_OUTURBS; ++k) {
924 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700925 gig_dbg(DEBUG_ISO,
926 "%s: isoc output URB %d unlinked, result = %d",
927 __func__, k, rc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800928 }
929}
930
931/* Isochronous Write - Bottom Half */
932/* =============================== */
933
934/* submit_iso_write_urb
935 * fill and submit the next isochronous write URB
936 * parameters:
937 * bcs B channel state structure
938 * return value:
939 * number of frames submitted in URB
940 * 0 if URB not submitted because no data available (isooutbuf busy)
941 * error code < 0 on error
942 */
943static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
944{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700945 struct urb *urb = ucx->urb;
946 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800947 struct usb_iso_packet_descriptor *ifd;
948 int corrbytes, nframe, rc;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700949 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800950
Tilman Schmidt784d5852006-04-10 22:55:04 -0700951 /* urb->dev is clobbered by USB subsystem */
952 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800953 urb->transfer_flags = URB_ISO_ASAP;
954 urb->transfer_buffer = ubc->isooutbuf->data;
955 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
956
957 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
958 ifd = &urb->iso_frame_desc[nframe];
959
960 /* compute frame length according to flow control */
961 ifd->length = BAS_NORMFRAME;
962 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700963 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
964 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800965 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
966 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
967 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
968 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
969 ifd->length += corrbytes;
970 atomic_add(-corrbytes, &ubc->corrbytes);
971 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800972
973 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -0700974 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
975 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800976 if (ifd->offset < 0) {
977 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700978 gig_dbg(DEBUG_ISO,
979 "%s: buffer busy at frame %d",
980 __func__, nframe);
981 /* tasklet will be restarted from
982 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800983 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700984 dev_err(ucx->bcs->cs->dev,
985 "%s: buffer error %d at frame %d\n",
986 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800987 return ifd->offset;
988 }
989 break;
990 }
991 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
992 ifd->status = 0;
993 ifd->actual_length = 0;
994 }
995 if ((urb->number_of_packets = nframe) > 0) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700996 spin_lock_irqsave(&ucx->bcs->cs->lock, flags);
997 rc = ucx->bcs->cs->connected ? usb_submit_urb(urb, SLAB_ATOMIC) : -ENODEV;
998 spin_unlock_irqrestore(&ucx->bcs->cs->lock, flags);
999
1000 if (rc) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001001 dev_err(ucx->bcs->cs->dev,
1002 "could not submit isochronous write URB: %s\n",
1003 get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001004 dump_urb(DEBUG_ISO, "isoc write", urb);
1005 return rc;
1006 }
1007 ++ubc->numsub;
1008 }
1009 return nframe;
1010}
1011
1012/* write_iso_tasklet
1013 * tasklet scheduled when an isochronous output URB from the Gigaset device
1014 * has completed
1015 * parameter:
1016 * data B channel state structure
1017 */
1018static void write_iso_tasklet(unsigned long data)
1019{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001020 struct bc_state *bcs = (struct bc_state *) data;
1021 struct bas_bc_state *ubc = bcs->hw.bas;
1022 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001023 struct isow_urbctx_t *done, *next, *ovfl;
1024 struct urb *urb;
1025 struct usb_iso_packet_descriptor *ifd;
1026 int offset;
1027 unsigned long flags;
1028 int i;
1029 struct sk_buff *skb;
1030 int len;
1031
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001032 /* loop while completed URBs arrive in time */
1033 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001034 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001035 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001036 return;
1037 }
1038
1039 /* retrieve completed URBs */
1040 spin_lock_irqsave(&ubc->isooutlock, flags);
1041 done = ubc->isooutdone;
1042 ubc->isooutdone = NULL;
1043 ovfl = ubc->isooutovfl;
1044 ubc->isooutovfl = NULL;
1045 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1046 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001047 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001048 error_hangup(bcs);
1049 break;
1050 }
1051 if (!done)
1052 break;
1053
1054 /* submit free URB if available */
1055 spin_lock_irqsave(&ubc->isooutlock, flags);
1056 next = ubc->isooutfree;
1057 ubc->isooutfree = NULL;
1058 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1059 if (next) {
1060 if (submit_iso_write_urb(next) <= 0) {
1061 /* could not submit URB, put it back */
1062 spin_lock_irqsave(&ubc->isooutlock, flags);
1063 if (ubc->isooutfree == NULL) {
1064 ubc->isooutfree = next;
1065 next = NULL;
1066 }
1067 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1068 if (next) {
1069 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001070 dev_err(cs->dev,
1071 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001072 error_hangup(bcs);
1073 }
1074 }
1075 }
1076
1077 /* process completed URB */
1078 urb = done->urb;
1079 switch (urb->status) {
1080 case 0: /* normal completion */
1081 break;
1082 case -EXDEV: /* inspect individual frames */
1083 /* assumptions (for lack of documentation):
Tilman Schmidt917f5082006-04-10 22:55:00 -07001084 * - actual_length bytes of the frame in error are
1085 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001086 * - all following frames are not sent at all
1087 */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001088 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1089 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001090 offset = done->limit; /* just in case */
1091 for (i = 0; i < BAS_NUMFRAMES; i++) {
1092 ifd = &urb->iso_frame_desc[i];
1093 if (ifd->status ||
1094 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001095 dev_warn(cs->dev,
1096 "isochronous write: frame %d: %s, "
1097 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001098 i, get_usb_statmsg(ifd->status),
1099 ifd->actual_length, ifd->length);
1100 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001101 ifd->actual_length)
1102 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001103 break;
1104 }
1105 }
1106#ifdef CONFIG_GIGASET_DEBUG
1107 /* check assumption on remaining frames */
1108 for (; i < BAS_NUMFRAMES; i++) {
1109 ifd = &urb->iso_frame_desc[i];
1110 if (ifd->status != -EINPROGRESS
1111 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001112 dev_warn(cs->dev,
1113 "isochronous write: frame %d: %s, "
1114 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001115 i, get_usb_statmsg(ifd->status),
1116 ifd->actual_length, ifd->length);
1117 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001118 ifd->actual_length)
1119 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001120 break;
1121 }
1122 }
1123#endif
1124 break;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001125 case -EPIPE: //FIXME is this the code for "underrun"?
1126 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001127 error_hangup(bcs);
1128 break;
1129 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001130 dev_warn(cs->dev, "isochronous write: %s\n",
1131 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001132 }
1133
1134 /* mark the write buffer area covered by this URB as free */
1135 if (done->limit >= 0)
1136 atomic_set(&ubc->isooutbuf->read, done->limit);
1137
1138 /* mark URB as free */
1139 spin_lock_irqsave(&ubc->isooutlock, flags);
1140 next = ubc->isooutfree;
1141 ubc->isooutfree = done;
1142 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1143 if (next) {
1144 /* only one URB still active - resubmit one */
1145 if (submit_iso_write_urb(next) <= 0) {
1146 /* couldn't submit */
1147 error_hangup(bcs);
1148 }
1149 }
1150 }
1151
1152 /* process queued SKBs */
1153 while ((skb = skb_dequeue(&bcs->squeue))) {
1154 /* copy to output buffer, doing L2 encapsulation */
1155 len = skb->len;
1156 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1157 /* insufficient buffer space, push back onto queue */
1158 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001159 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1160 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001161 break;
1162 }
1163 skb_pull(skb, len);
1164 gigaset_skb_sent(bcs, skb);
1165 dev_kfree_skb_any(skb);
1166 }
1167}
1168
1169/* Isochronous Read - Bottom Half */
1170/* ============================== */
1171
1172/* read_iso_tasklet
1173 * tasklet scheduled when an isochronous input URB from the Gigaset device
1174 * has completed
1175 * parameter:
1176 * data B channel state structure
1177 */
1178static void read_iso_tasklet(unsigned long data)
1179{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001180 struct bc_state *bcs = (struct bc_state *) data;
1181 struct bas_bc_state *ubc = bcs->hw.bas;
1182 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001183 struct urb *urb;
1184 char *rcvbuf;
1185 unsigned long flags;
1186 int totleft, numbytes, offset, frame, rc;
1187
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001188 /* loop while more completed URBs arrive in the meantime */
1189 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001190 /* retrieve URB */
1191 spin_lock_irqsave(&ubc->isoinlock, flags);
1192 if (!(urb = ubc->isoindone)) {
1193 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1194 return;
1195 }
1196 ubc->isoindone = NULL;
1197 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001198 dev_warn(cs->dev,
1199 "isochronous read overrun, "
1200 "dropped URB with status: %s, %d bytes lost\n",
1201 get_usb_statmsg(ubc->loststatus),
1202 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001203 ubc->loststatus = -EINPROGRESS;
1204 }
1205 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1206
1207 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001208 gig_dbg(DEBUG_ISO,
1209 "%s: channel not running, "
1210 "dropped URB with status: %s",
1211 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001212 return;
1213 }
1214
1215 switch (urb->status) {
1216 case 0: /* normal completion */
1217 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001218 case -EXDEV: /* inspect individual frames
1219 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001220 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1221 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001222 break;
1223 case -ENOENT:
1224 case -ECONNRESET:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001225 gig_dbg(DEBUG_ISO, "%s: URB canceled", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001226 continue; /* -> skip */
1227 case -EINPROGRESS: /* huh? */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001228 gig_dbg(DEBUG_ISO, "%s: URB still pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001229 continue; /* -> skip */
1230 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001231 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001232 error_hangup(bcs);
1233 continue; /* -> skip */
1234 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001235 dev_warn(cs->dev, "isochronous read: %s\n",
1236 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001237 goto error;
1238 }
1239
1240 rcvbuf = urb->transfer_buffer;
1241 totleft = urb->actual_length;
1242 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1243 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001244 dev_warn(cs->dev,
1245 "isochronous read: frame %d: %s\n",
1246 frame,
1247 get_usb_statmsg(
1248 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001249 break;
1250 }
1251 numbytes = urb->iso_frame_desc[frame].actual_length;
1252 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001253 dev_warn(cs->dev,
1254 "isochronous read: frame %d: "
1255 "numbytes (%d) > BAS_MAXFRAME\n",
1256 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001257 break;
1258 }
1259 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001260 dev_warn(cs->dev,
1261 "isochronous read: frame %d: "
1262 "numbytes (%d) > totleft (%d)\n",
1263 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001264 break;
1265 }
1266 offset = urb->iso_frame_desc[frame].offset;
1267 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001268 dev_warn(cs->dev,
1269 "isochronous read: frame %d: "
1270 "offset (%d) + numbytes (%d) "
1271 "> BAS_INBUFSIZE\n",
1272 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001273 break;
1274 }
1275 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1276 totleft -= numbytes;
1277 }
1278 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001279 dev_warn(cs->dev,
1280 "isochronous read: %d data bytes missing\n",
1281 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001282
1283 error:
1284 /* URB processed, resubmit */
1285 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1286 urb->iso_frame_desc[frame].status = 0;
1287 urb->iso_frame_desc[frame].actual_length = 0;
1288 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001289 /* urb->dev is clobbered by USB subsystem */
1290 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001291 urb->transfer_flags = URB_ISO_ASAP;
1292 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001293 spin_lock_irqsave(&cs->lock, flags);
1294 rc = cs->connected ? usb_submit_urb(urb, SLAB_ATOMIC) : -ENODEV;
1295 spin_unlock_irqrestore(&cs->lock, flags);
1296 if (rc) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001297 dev_err(cs->dev,
1298 "could not resubmit isochronous read URB: %s\n",
1299 get_usb_statmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001300 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1301 error_hangup(bcs);
1302 }
1303 }
1304}
1305
1306/* Channel Operations */
1307/* ================== */
1308
1309/* req_timeout
1310 * timeout routine for control output request
1311 * argument:
1312 * B channel control structure
1313 */
1314static void req_timeout(unsigned long data)
1315{
1316 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001317 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001318 int pending;
1319 unsigned long flags;
1320
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001321 check_pending(ucs);
1322
1323 spin_lock_irqsave(&ucs->lock, flags);
1324 pending = ucs->pending;
1325 ucs->pending = 0;
1326 spin_unlock_irqrestore(&ucs->lock, flags);
1327
1328 switch (pending) {
1329 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001330 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001331 break;
1332
1333 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001334 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001335 error_reset(bcs->cs);
1336 break;
1337
1338 case HD_OPEN_B2CHANNEL:
1339 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001340 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1341 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001342 error_hangup(bcs);
1343 break;
1344
1345 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001346 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001347 break;
1348
1349 case HD_CLOSE_B2CHANNEL:
1350 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001351 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1352 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001353 break;
1354
1355 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001356 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1357 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001358 }
1359}
1360
1361/* write_ctrl_callback
1362 * USB completion handler for control pipe output
1363 * called by the USB subsystem in interrupt context
1364 * parameter:
1365 * urb USB request block of completed request
1366 * urb->context = hardware specific controller state structure
1367 */
1368static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1369{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001370 struct bas_cardstate *ucs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001371 unsigned long flags;
1372
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001373 spin_lock_irqsave(&ucs->lock, flags);
1374 if (urb->status && ucs->pending) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001375 dev_err(&ucs->interface->dev,
1376 "control request 0x%02x failed: %s\n",
1377 ucs->pending, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001378 del_timer(&ucs->timer_ctrl);
1379 ucs->pending = 0;
1380 }
1381 /* individual handling of specific request types */
1382 switch (ucs->pending) {
1383 case HD_DEVICE_INIT_ACK: /* no reply expected */
1384 ucs->pending = 0;
1385 break;
1386 }
1387 spin_unlock_irqrestore(&ucs->lock, flags);
1388}
1389
1390/* req_submit
1391 * submit a control output request without message buffer to the Gigaset base
1392 * and optionally start a timeout
1393 * parameters:
1394 * bcs B channel control structure
1395 * req control request code (HD_*)
1396 * val control request parameter value (set to 0 if unused)
1397 * timeout timeout in seconds (0: no timeout)
1398 * return value:
1399 * 0 on success
1400 * -EINVAL if a NULL pointer is encountered somewhere
1401 * -EBUSY if another request is pending
1402 * any URB submission error code
1403 */
1404static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1405{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001406 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001407 int ret;
1408 unsigned long flags;
1409
Tilman Schmidt784d5852006-04-10 22:55:04 -07001410 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001411
1412 spin_lock_irqsave(&ucs->lock, flags);
1413 if (ucs->pending) {
1414 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001415 dev_err(bcs->cs->dev,
1416 "submission of request 0x%02x failed: "
1417 "request 0x%02x still pending\n",
1418 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001419 return -EBUSY;
1420 }
1421 if (ucs->urb_ctrl->status == -EINPROGRESS) {
1422 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001423 dev_err(bcs->cs->dev,
1424 "could not submit request 0x%02x: URB busy\n", req);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001425 return -EBUSY;
1426 }
1427
1428 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1429 ucs->dr_ctrl.bRequest = req;
1430 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1431 ucs->dr_ctrl.wIndex = 0;
1432 ucs->dr_ctrl.wLength = 0;
1433 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001434 usb_sndctrlpipe(ucs->udev, 0),
1435 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1436 write_ctrl_callback, ucs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001437 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001438 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1439 req, get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001440 spin_unlock_irqrestore(&ucs->lock, flags);
1441 return ret;
1442 }
1443 ucs->pending = req;
1444
1445 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001446 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001447 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1448 ucs->timer_ctrl.data = (unsigned long) bcs;
1449 ucs->timer_ctrl.function = req_timeout;
1450 add_timer(&ucs->timer_ctrl);
1451 }
1452
1453 spin_unlock_irqrestore(&ucs->lock, flags);
1454 return 0;
1455}
1456
1457/* gigaset_init_bchannel
1458 * called by common.c to connect a B channel
1459 * initialize isochronous I/O and tell the Gigaset base to open the channel
1460 * argument:
1461 * B channel control structure
1462 * return value:
1463 * 0 on success, error code < 0 on error
1464 */
1465static int gigaset_init_bchannel(struct bc_state *bcs)
1466{
1467 int req, ret;
1468
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001469 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001470 dev_err(bcs->cs->dev,
1471 "could not start isochronous I/O for channel %d\n",
1472 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001473 error_hangup(bcs);
1474 return ret;
1475 }
1476
1477 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1478 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001479 dev_err(bcs->cs->dev, "could not open channel %d: %s\n",
1480 bcs->channel + 1, get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001481 stopurbs(bcs->hw.bas);
1482 error_hangup(bcs);
1483 }
1484 return ret;
1485}
1486
1487/* gigaset_close_bchannel
1488 * called by common.c to disconnect a B channel
1489 * tell the Gigaset base to close the channel
1490 * stopping isochronous I/O and LL notification will be done when the
1491 * acknowledgement for the close arrives
1492 * argument:
1493 * B channel control structure
1494 * return value:
1495 * 0 on success, error code < 0 on error
1496 */
1497static int gigaset_close_bchannel(struct bc_state *bcs)
1498{
1499 int req, ret;
1500
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001501 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1502 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1503 /* channel not running: just signal common.c */
1504 gigaset_bchannel_down(bcs);
1505 return 0;
1506 }
1507
1508 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1509 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidt784d5852006-04-10 22:55:04 -07001510 dev_err(bcs->cs->dev,
1511 "could not submit HD_CLOSE_BxCHANNEL request: %s\n",
1512 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001513 return ret;
1514}
1515
1516/* Device Operations */
1517/* ================= */
1518
1519/* complete_cb
1520 * unqueue first command buffer from queue, waking any sleepers
1521 * must be called with cs->cmdlock held
1522 * parameter:
1523 * cs controller state structure
1524 */
1525static void complete_cb(struct cardstate *cs)
1526{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001527 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001528
1529 /* unqueue completed buffer */
1530 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001531 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1532 "write_command: sent %u bytes, %u left",
1533 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001534 if ((cs->cmdbuf = cb->next) != NULL) {
1535 cs->cmdbuf->prev = NULL;
1536 cs->curlen = cs->cmdbuf->len;
1537 } else {
1538 cs->lastcmdbuf = NULL;
1539 cs->curlen = 0;
1540 }
1541
1542 if (cb->wake_tasklet)
1543 tasklet_schedule(cb->wake_tasklet);
1544
1545 kfree(cb);
1546}
1547
1548static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len);
1549
1550/* write_command_callback
1551 * USB completion handler for AT command transmission
1552 * called by the USB subsystem in interrupt context
1553 * parameter:
1554 * urb USB request block of completed request
1555 * urb->context = controller state structure
1556 */
1557static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1558{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001559 struct cardstate *cs = urb->context;
1560 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001561 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001562
1563 /* check status */
1564 switch (urb->status) {
1565 case 0: /* normal completion */
1566 break;
1567 case -ENOENT: /* canceled */
1568 case -ECONNRESET: /* canceled (async) */
1569 case -EINPROGRESS: /* pending */
1570 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001571 gig_dbg(DEBUG_USBREQ, "%s: %s",
1572 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001573 return;
1574 default: /* any failure */
1575 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001576 dev_warn(cs->dev,
1577 "command write: %s, "
1578 "giving up after %d retries\n",
1579 get_usb_statmsg(urb->status),
1580 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001581 break;
1582 }
1583 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001584 dev_warn(cs->dev,
1585 "command write: %s, "
1586 "cannot retry - cmdbuf gone\n",
1587 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001588 break;
1589 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001590 dev_notice(cs->dev, "command write: %s, retry %d\n",
1591 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001592 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1593 /* resubmitted - bypass regular exit block */
1594 return;
1595 /* command send failed, assume base still waiting */
1596 update_basstate(ucs, BS_ATREADY, 0);
1597 }
1598
1599 spin_lock_irqsave(&cs->cmdlock, flags);
1600 if (cs->cmdbuf != NULL)
1601 complete_cb(cs);
1602 spin_unlock_irqrestore(&cs->cmdlock, flags);
1603}
1604
1605/* atrdy_timeout
1606 * timeout routine for AT command transmission
1607 * argument:
1608 * controller state structure
1609 */
1610static void atrdy_timeout(unsigned long data)
1611{
1612 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001613 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001614
Tilman Schmidt784d5852006-04-10 22:55:04 -07001615 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001616
1617 /* fake the missing signal - what else can I do? */
1618 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1619 start_cbsend(cs);
1620}
1621
1622/* atwrite_submit
1623 * submit an HD_WRITE_ATMESSAGE command URB
1624 * parameters:
1625 * cs controller state structure
1626 * buf buffer containing command to send
1627 * len length of command to send
1628 * return value:
1629 * 0 on success
1630 * -EFAULT if a NULL pointer is encountered somewhere
1631 * -EBUSY if another request is pending
1632 * any URB submission error code
1633 */
1634static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1635{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001636 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001637 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001638 int ret;
1639
Tilman Schmidt784d5852006-04-10 22:55:04 -07001640 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001641
1642 if (ucs->urb_cmd_out->status == -EINPROGRESS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001643 dev_err(cs->dev,
1644 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001645 return -EBUSY;
1646 }
1647
1648 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1649 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1650 ucs->dr_cmd_out.wValue = 0;
1651 ucs->dr_cmd_out.wIndex = 0;
1652 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1653 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1654 usb_sndctrlpipe(ucs->udev, 0),
1655 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1656 write_command_callback, cs);
1657
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001658 spin_lock_irqsave(&cs->lock, flags);
1659 ret = cs->connected ? usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC) : -ENODEV;
1660 spin_unlock_irqrestore(&cs->lock, flags);
1661
1662 if (ret) {
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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001761 if (len <= 0)
1762 return 0; /* nothing to do */
1763
1764 if (len > IF_WRITEBUF)
1765 len = IF_WRITEBUF;
1766 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001767 dev_err(cs->dev, "%s: out of memory\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001768 return -ENOMEM;
1769 }
1770
1771 memcpy(cb->buf, buf, len);
1772 cb->len = len;
1773 cb->offset = 0;
1774 cb->next = NULL;
1775 cb->wake_tasklet = wake_tasklet;
1776
1777 spin_lock_irqsave(&cs->cmdlock, flags);
1778 cb->prev = cs->lastcmdbuf;
1779 if (cs->lastcmdbuf)
1780 cs->lastcmdbuf->next = cb;
1781 else {
1782 cs->cmdbuf = cb;
1783 cs->curlen = len;
1784 }
1785 cs->cmdbytes += len;
1786 cs->lastcmdbuf = cb;
1787 spin_unlock_irqrestore(&cs->cmdlock, flags);
1788
1789 status = start_cbsend(cs);
1790
1791 return status < 0 ? status : len;
1792}
1793
1794/* gigaset_write_room
1795 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001796 * return number of characters the driver will accept to be written via
1797 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001798 * parameter:
1799 * controller state structure
1800 * return value:
1801 * number of characters
1802 */
1803static int gigaset_write_room(struct cardstate *cs)
1804{
1805 return IF_WRITEBUF;
1806}
1807
1808/* gigaset_chars_in_buffer
1809 * tty_driver.chars_in_buffer interface routine
1810 * return number of characters waiting to be sent
1811 * parameter:
1812 * controller state structure
1813 * return value:
1814 * number of characters
1815 */
1816static int gigaset_chars_in_buffer(struct cardstate *cs)
1817{
1818 unsigned long flags;
1819 unsigned bytes;
1820
1821 spin_lock_irqsave(&cs->cmdlock, flags);
1822 bytes = cs->cmdbytes;
1823 spin_unlock_irqrestore(&cs->cmdlock, flags);
1824
1825 return bytes;
1826}
1827
1828/* gigaset_brkchars
1829 * implementation of ioctl(GIGASET_BRKCHARS)
1830 * parameter:
1831 * controller state structure
1832 * return value:
1833 * -EINVAL (unimplemented function)
1834 */
1835static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1836{
1837 return -EINVAL;
1838}
1839
1840
1841/* Device Initialization/Shutdown */
1842/* ============================== */
1843
1844/* Free hardware dependent part of the B channel structure
1845 * parameter:
1846 * bcs B channel structure
1847 * return value:
1848 * !=0 on success
1849 */
1850static int gigaset_freebcshw(struct bc_state *bcs)
1851{
1852 if (!bcs->hw.bas)
1853 return 0;
1854
1855 if (bcs->hw.bas->isooutbuf)
1856 kfree(bcs->hw.bas->isooutbuf);
1857 kfree(bcs->hw.bas);
1858 bcs->hw.bas = NULL;
1859 return 1;
1860}
1861
1862/* Initialize hardware dependent part of the B channel structure
1863 * parameter:
1864 * bcs B channel structure
1865 * return value:
1866 * !=0 on success
1867 */
1868static int gigaset_initbcshw(struct bc_state *bcs)
1869{
1870 int i;
1871 struct bas_bc_state *ubc;
1872
1873 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1874 if (!ubc) {
1875 err("could not allocate bas_bc_state");
1876 return 0;
1877 }
1878
1879 atomic_set(&ubc->running, 0);
1880 atomic_set(&ubc->corrbytes, 0);
1881 spin_lock_init(&ubc->isooutlock);
1882 for (i = 0; i < BAS_OUTURBS; ++i) {
1883 ubc->isoouturbs[i].urb = NULL;
1884 ubc->isoouturbs[i].bcs = bcs;
1885 }
1886 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1887 ubc->numsub = 0;
1888 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
1889 err("could not allocate isochronous output buffer");
1890 kfree(ubc);
1891 bcs->hw.bas = NULL;
1892 return 0;
1893 }
1894 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001895 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001896
1897 spin_lock_init(&ubc->isoinlock);
1898 for (i = 0; i < BAS_INURBS; ++i)
1899 ubc->isoinurbs[i] = NULL;
1900 ubc->isoindone = NULL;
1901 ubc->loststatus = -EINPROGRESS;
1902 ubc->isoinlost = 0;
1903 ubc->seqlen = 0;
1904 ubc->inbyte = 0;
1905 ubc->inbits = 0;
1906 ubc->goodbytes = 0;
1907 ubc->alignerrs = 0;
1908 ubc->fcserrs = 0;
1909 ubc->frameerrs = 0;
1910 ubc->giants = 0;
1911 ubc->runts = 0;
1912 ubc->aborts = 0;
1913 ubc->shared0s = 0;
1914 ubc->stolen0s = 0;
1915 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001916 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001917 return 1;
1918}
1919
1920static void gigaset_reinitbcshw(struct bc_state *bcs)
1921{
1922 struct bas_bc_state *ubc = bcs->hw.bas;
1923
1924 atomic_set(&bcs->hw.bas->running, 0);
1925 atomic_set(&bcs->hw.bas->corrbytes, 0);
1926 bcs->hw.bas->numsub = 0;
1927 spin_lock_init(&ubc->isooutlock);
1928 spin_lock_init(&ubc->isoinlock);
1929 ubc->loststatus = -EINPROGRESS;
1930}
1931
1932static void gigaset_freecshw(struct cardstate *cs)
1933{
1934 struct bas_cardstate *ucs = cs->hw.bas;
1935
1936 del_timer(&ucs->timer_ctrl);
1937 del_timer(&ucs->timer_atrdy);
1938 del_timer(&ucs->timer_cmd_in);
1939
1940 kfree(cs->hw.bas);
1941}
1942
1943static int gigaset_initcshw(struct cardstate *cs)
1944{
1945 struct bas_cardstate *ucs;
1946
1947 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
1948 if (!ucs)
1949 return 0;
1950
1951 ucs->urb_cmd_in = NULL;
1952 ucs->urb_cmd_out = NULL;
1953 ucs->rcvbuf = NULL;
1954 ucs->rcvbuf_size = 0;
1955
1956 spin_lock_init(&ucs->lock);
1957 ucs->pending = 0;
1958
1959 atomic_set(&ucs->basstate, 0);
1960 init_timer(&ucs->timer_ctrl);
1961 init_timer(&ucs->timer_atrdy);
1962 init_timer(&ucs->timer_cmd_in);
1963
1964 return 1;
1965}
1966
1967/* freeurbs
1968 * unlink and deallocate all URBs unconditionally
1969 * caller must make sure that no commands are still in progress
1970 * parameter:
1971 * cs controller state structure
1972 */
1973static void freeurbs(struct cardstate *cs)
1974{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001975 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001976 struct bas_bc_state *ubc;
1977 int i, j;
1978
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001979 for (j = 0; j < 2; ++j) {
1980 ubc = cs->bcs[j].hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001981 for (i = 0; i < BAS_OUTURBS; ++i)
1982 if (ubc->isoouturbs[i].urb) {
1983 usb_kill_urb(ubc->isoouturbs[i].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001984 gig_dbg(DEBUG_INIT,
1985 "%s: isoc output URB %d/%d unlinked",
1986 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001987 usb_free_urb(ubc->isoouturbs[i].urb);
1988 ubc->isoouturbs[i].urb = NULL;
1989 }
1990 for (i = 0; i < BAS_INURBS; ++i)
1991 if (ubc->isoinurbs[i]) {
1992 usb_kill_urb(ubc->isoinurbs[i]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001993 gig_dbg(DEBUG_INIT,
1994 "%s: isoc input URB %d/%d unlinked",
1995 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001996 usb_free_urb(ubc->isoinurbs[i]);
1997 ubc->isoinurbs[i] = NULL;
1998 }
1999 }
2000 if (ucs->urb_int_in) {
2001 usb_kill_urb(ucs->urb_int_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002002 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2003 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002004 usb_free_urb(ucs->urb_int_in);
2005 ucs->urb_int_in = NULL;
2006 }
2007 if (ucs->urb_cmd_out) {
2008 usb_kill_urb(ucs->urb_cmd_out);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002009 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2010 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002011 usb_free_urb(ucs->urb_cmd_out);
2012 ucs->urb_cmd_out = NULL;
2013 }
2014 if (ucs->urb_cmd_in) {
2015 usb_kill_urb(ucs->urb_cmd_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002016 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2017 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002018 usb_free_urb(ucs->urb_cmd_in);
2019 ucs->urb_cmd_in = NULL;
2020 }
2021 if (ucs->urb_ctrl) {
2022 usb_kill_urb(ucs->urb_ctrl);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002023 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2024 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002025 usb_free_urb(ucs->urb_ctrl);
2026 ucs->urb_ctrl = NULL;
2027 }
2028}
2029
2030/* gigaset_probe
2031 * This function is called when a new USB device is connected.
2032 * It checks whether the new device is handled by this driver.
2033 */
2034static int gigaset_probe(struct usb_interface *interface,
2035 const struct usb_device_id *id)
2036{
2037 struct usb_host_interface *hostif;
2038 struct usb_device *udev = interface_to_usbdev(interface);
2039 struct cardstate *cs = NULL;
2040 struct bas_cardstate *ucs = NULL;
2041 struct bas_bc_state *ubc;
2042 struct usb_endpoint_descriptor *endpoint;
2043 int i, j;
2044 int ret;
2045
Tilman Schmidt784d5852006-04-10 22:55:04 -07002046 gig_dbg(DEBUG_ANY,
2047 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2048 __func__, le16_to_cpu(udev->descriptor.idVendor),
2049 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002050
2051 /* See if the device offered us matches what we can accept */
2052 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_GIGA_VENDOR_ID) ||
2053 (le16_to_cpu(udev->descriptor.idProduct) != USB_GIGA_PRODUCT_ID &&
2054 le16_to_cpu(udev->descriptor.idProduct) != USB_4175_PRODUCT_ID &&
2055 le16_to_cpu(udev->descriptor.idProduct) != USB_SX303_PRODUCT_ID &&
2056 le16_to_cpu(udev->descriptor.idProduct) != USB_SX353_PRODUCT_ID)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002057 gig_dbg(DEBUG_ANY, "%s: unmatched ID - exiting", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002058 return -ENODEV;
2059 }
2060
2061 /* set required alternate setting */
2062 hostif = interface->cur_altsetting;
2063 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002064 gig_dbg(DEBUG_ANY,
2065 "%s: wrong alternate setting %d - trying to switch",
2066 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002067 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002068 dev_warn(&udev->dev, "usb_set_interface failed, "
2069 "device %d interface %d altsetting %d\n",
2070 udev->devnum, hostif->desc.bInterfaceNumber,
2071 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002072 return -ENODEV;
2073 }
2074 hostif = interface->cur_altsetting;
2075 }
2076
2077 /* Reject application specific interfaces
2078 */
2079 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002080 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2081 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002082 return -ENODEV;
2083 }
2084
Tilman Schmidt784d5852006-04-10 22:55:04 -07002085 dev_info(&udev->dev,
2086 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2087 __func__, le16_to_cpu(udev->descriptor.idVendor),
2088 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002089
2090 cs = gigaset_getunassignedcs(driver);
2091 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002092 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002093 return -ENODEV;
2094 }
2095 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002096
2097 /* save off device structure ptrs for later use */
2098 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002099 ucs->udev = udev;
2100 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002101 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002102
2103 /* allocate URBs:
2104 * - one for the interrupt pipe
2105 * - three for the different uses of the default control pipe
2106 * - three for each isochronous pipe
2107 */
2108 ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL);
2109 if (!ucs->urb_int_in) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002110 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002111 goto error;
2112 }
2113 ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL);
2114 if (!ucs->urb_cmd_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_out = usb_alloc_urb(0, SLAB_KERNEL);
2119 if (!ucs->urb_cmd_out) {
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_ctrl = usb_alloc_urb(0, SLAB_KERNEL);
2124 if (!ucs->urb_ctrl) {
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
2129 for (j = 0; j < 2; ++j) {
2130 ubc = cs->bcs[j].hw.bas;
2131 for (i = 0; i < BAS_OUTURBS; ++i) {
2132 ubc->isoouturbs[i].urb =
2133 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2134 if (!ubc->isoouturbs[i].urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002135 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002136 goto error;
2137 }
2138 }
2139 for (i = 0; i < BAS_INURBS; ++i) {
2140 ubc->isoinurbs[i] =
2141 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2142 if (!ubc->isoinurbs[i]) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002143 dev_err(cs->dev, "no free urbs available\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002144 goto error;
2145 }
2146 }
2147 }
2148
2149 ucs->rcvbuf = NULL;
2150 ucs->rcvbuf_size = 0;
2151
2152 /* Fill the interrupt urb and send it to the core */
2153 endpoint = &hostif->endpoint[0].desc;
2154 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002155 usb_rcvintpipe(udev,
2156 (endpoint->bEndpointAddress) & 0x0f),
2157 ucs->int_in_buf, 3, read_int_callback, cs,
2158 endpoint->bInterval);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002159 ret = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL);
2160 if (ret) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002161 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2162 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002163 goto error;
2164 }
2165
2166 /* tell the device that the driver is ready */
2167 if ((ret = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2168 goto error;
2169
2170 /* tell common part that the device is ready */
2171 if (startmode == SM_LOCKED)
2172 atomic_set(&cs->mstate, MS_LOCKED);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002173
2174 /* save address of controller structure */
2175 usb_set_intfdata(interface, cs);
2176
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002177 if (!gigaset_start(cs))
2178 goto error;
2179
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002180 return 0;
2181
2182error:
2183 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002184 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002185 gigaset_unassign(cs);
2186 return -ENODEV;
2187}
2188
2189/* gigaset_disconnect
2190 * This function is called when the Gigaset base is unplugged.
2191 */
2192static void gigaset_disconnect(struct usb_interface *interface)
2193{
2194 struct cardstate *cs;
2195 struct bas_cardstate *ucs;
2196
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002197 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002198
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002199 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002200
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002201 dev_info(cs->dev, "disconnecting Gigaset base\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002202 gigaset_stop(cs);
2203 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002204 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002205 kfree(ucs->rcvbuf);
2206 ucs->rcvbuf = NULL;
2207 ucs->rcvbuf_size = 0;
2208 atomic_set(&ucs->basstate, 0);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002209 usb_put_dev(ucs->udev);
2210 ucs->interface = NULL;
2211 ucs->udev = NULL;
2212 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002213 gigaset_unassign(cs);
2214}
2215
2216static struct gigaset_ops gigops = {
2217 gigaset_write_cmd,
2218 gigaset_write_room,
2219 gigaset_chars_in_buffer,
2220 gigaset_brkchars,
2221 gigaset_init_bchannel,
2222 gigaset_close_bchannel,
2223 gigaset_initbcshw,
2224 gigaset_freebcshw,
2225 gigaset_reinitbcshw,
2226 gigaset_initcshw,
2227 gigaset_freecshw,
2228 gigaset_set_modem_ctrl,
2229 gigaset_baud_rate,
2230 gigaset_set_line_ctrl,
2231 gigaset_isoc_send_skb,
2232 gigaset_isoc_input,
2233};
2234
2235/* bas_gigaset_init
2236 * This function is called after the kernel module is loaded.
2237 */
2238static int __init bas_gigaset_init(void)
2239{
2240 int result;
2241
2242 /* allocate memory for our driver state and intialize it */
2243 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002244 GIGASET_MODULENAME, GIGASET_DEVNAME,
2245 GIGASET_DEVFSNAME, &gigops,
2246 THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002247 goto error;
2248
2249 /* allocate memory for our device state and intialize it */
Tilman Schmidt917f5082006-04-10 22:55:00 -07002250 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2251 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002252 if (!cardstate)
2253 goto error;
2254
2255 /* register this driver with the USB subsystem */
2256 result = usb_register(&gigaset_usb_driver);
2257 if (result < 0) {
2258 err("usb_register failed (error %d)", -result);
2259 goto error;
2260 }
2261
2262 info(DRIVER_AUTHOR);
2263 info(DRIVER_DESC);
2264 return 0;
2265
2266error: if (cardstate)
2267 gigaset_freecs(cardstate);
2268 cardstate = NULL;
2269 if (driver)
2270 gigaset_freedriver(driver);
2271 driver = NULL;
2272 return -1;
2273}
2274
2275/* bas_gigaset_exit
2276 * This function is called before the kernel module is unloaded.
2277 */
2278static void __exit bas_gigaset_exit(void)
2279{
2280 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002281 * => no gigaset_start any more
2282 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002283
2284 gigaset_shutdown(cardstate);
2285 /* from now on, no isdn callback should be possible */
2286
2287 if (atomic_read(&cardstate->hw.bas->basstate) & BS_ATOPEN) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002288 gig_dbg(DEBUG_ANY, "closing AT channel");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002289 if (req_submit(cardstate->bcs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002290 HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT) >= 0) {
2291 /* successfully submitted */
2292 //FIXME wait for completion?
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002293 }
2294 }
2295
2296 /* deregister this driver with the USB subsystem */
2297 usb_deregister(&gigaset_usb_driver);
2298 /* this will call the disconnect-callback */
2299 /* from now on, no disconnect/probe callback should be running */
2300
2301 gigaset_freecs(cardstate);
2302 cardstate = NULL;
2303 gigaset_freedriver(driver);
2304 driver = NULL;
2305}
2306
2307
2308module_init(bas_gigaset_init);
2309module_exit(bas_gigaset_exit);
2310
2311MODULE_AUTHOR(DRIVER_AUTHOR);
2312MODULE_DESCRIPTION(DRIVER_DESC);
2313MODULE_LICENSE("GPL");