blob: 63b629b1cdb2be0c1e79ac5d7d56f100914a5c11 [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 *
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08008 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080014 */
15
16#include "gigaset.h"
17
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/timer.h>
22#include <linux/usb.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25
26/* Version Information */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070027#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080028#define DRIVER_DESC "USB Driver for Gigaset 307x"
29
30
31/* Module parameters */
32
33static int startmode = SM_ISDN;
34static int cidmode = 1;
35
36module_param(startmode, int, S_IRUGO);
37module_param(cidmode, int, S_IRUGO);
38MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
39MODULE_PARM_DESC(cidmode, "Call-ID mode");
40
41#define GIGASET_MINORS 1
42#define GIGASET_MINOR 16
43#define GIGASET_MODULENAME "bas_gigaset"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080044#define GIGASET_DEVNAME "ttyGB"
45
Tilman Schmidt73a88812006-04-22 02:35:30 -070046/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
47#define IF_WRITEBUF 264
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080048
49/* Values for the Gigaset 307x */
50#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070051#define USB_3070_PRODUCT_ID 0x0001
52#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080053#define USB_SX303_PRODUCT_ID 0x0021
54#define USB_SX353_PRODUCT_ID 0x0022
55
56/* table of devices that work with this driver */
57static struct usb_device_id gigaset_table [] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070058 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080060 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
62 { } /* Terminating entry */
63};
64
65MODULE_DEVICE_TABLE(usb, gigaset_table);
66
Tilman Schmidt06163f82006-06-26 00:25:33 -070067/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080068
Tilman Schmidt06163f82006-06-26 00:25:33 -070069/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080070static int gigaset_probe(struct usb_interface *interface,
71 const struct usb_device_id *id);
72
73/* Function will be called if the device is unplugged */
74static void gigaset_disconnect(struct usb_interface *interface);
75
Tilman Schmidt06163f82006-06-26 00:25:33 -070076static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070077static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070078static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070079static int atwrite_submit(struct cardstate *, unsigned char *, int);
80static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080081
Tilman Schmidt06163f82006-06-26 00:25:33 -070082/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080083
84struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070085 struct usb_device *udev; /* USB device pointer */
86 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080087 unsigned char minor; /* starting minor number */
88
Tilman Schmidt784d5852006-04-10 22:55:04 -070089 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080090 struct usb_ctrlrequest dr_ctrl;
91 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -070092 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080093
94 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -070095 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080096 struct usb_ctrlrequest dr_cmd_out;
97 int retry_cmd_out;
98
Tilman Schmidt784d5852006-04-10 22:55:04 -070099 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800100 struct usb_ctrlrequest dr_cmd_in;
101 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700102 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800103
Tilman Schmidt784d5852006-04-10 22:55:04 -0700104 struct urb *urb_int_in; /* URB for interrupt pipe */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800105 unsigned char int_in_buf[3];
106
107 spinlock_t lock; /* locks all following */
108 atomic_t basstate; /* bitmap (BS_*) */
109 int pending; /* uncompleted base request */
110 int rcvbuf_size; /* size of AT receive buffer */
111 /* 0: no receive in progress */
112 int retry_cmd_in; /* receive req retry count */
113};
114
115/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700116#define BS_ATOPEN 0x001 /* AT channel open */
117#define BS_B1OPEN 0x002 /* B channel 1 open */
118#define BS_B2OPEN 0x004 /* B channel 2 open */
119#define BS_ATREADY 0x008 /* base ready for AT command */
120#define BS_INIT 0x010 /* base has signalled INIT_OK */
121#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
122#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
123#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800124
125
126static struct gigaset_driver *driver = NULL;
127static struct cardstate *cardstate = NULL;
128
129/* usb specific object needed to register this driver with the usb subsystem */
130static struct usb_driver gigaset_usb_driver = {
131 .name = GIGASET_MODULENAME,
132 .probe = gigaset_probe,
133 .disconnect = gigaset_disconnect,
134 .id_table = gigaset_table,
135};
136
Tilman Schmidt73a88812006-04-22 02:35:30 -0700137/* get message text for usb_submit_urb return code
138 */
139static char *get_usb_rcmsg(int rc)
140{
141 static char unkmsg[28];
142
143 switch (rc) {
144 case 0:
145 return "success";
146 case -ENOMEM:
147 return "out of memory";
148 case -ENODEV:
149 return "device not present";
150 case -ENOENT:
151 return "endpoint not present";
152 case -ENXIO:
153 return "URB type not supported";
154 case -EINVAL:
155 return "invalid argument";
156 case -EAGAIN:
157 return "start frame too early or too much scheduled";
158 case -EFBIG:
159 return "too many isochronous frames requested";
160 case -EPIPE:
161 return "endpoint stalled";
162 case -EMSGSIZE:
163 return "invalid packet size";
164 case -ENOSPC:
165 return "would overcommit USB bandwidth";
166 case -ESHUTDOWN:
167 return "device shut down";
168 case -EPERM:
169 return "reject flag set";
170 case -EHOSTUNREACH:
171 return "device suspended";
172 default:
173 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
174 return unkmsg;
175 }
176}
177
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800178/* get message text for USB status code
179 */
180static char *get_usb_statmsg(int status)
181{
182 static char unkmsg[28];
183
184 switch (status) {
185 case 0:
186 return "success";
187 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700188 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800189 case -EINPROGRESS:
190 return "pending";
191 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700192 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800193 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700194 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700195 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800196 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700197 case -EPIPE:
198 return "endpoint stalled";
199 case -ECOMM:
200 return "IN buffer overrun";
201 case -ENOSR:
202 return "OUT buffer underrun";
203 case -EOVERFLOW:
204 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800205 case -EREMOTEIO:
206 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700207 case -ENODEV:
208 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800209 case -EXDEV:
210 return "partial isochronous transfer";
211 case -EINVAL:
212 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700213 case -ECONNRESET:
214 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800215 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700216 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800217 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700218 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800219 return unkmsg;
220 }
221}
222
223/* usb_pipetype_str
224 * retrieve string representation of USB pipe type
225 */
226static inline char *usb_pipetype_str(int pipe)
227{
228 if (usb_pipeisoc(pipe))
229 return "Isoc";
230 if (usb_pipeint(pipe))
231 return "Int";
232 if (usb_pipecontrol(pipe))
233 return "Ctrl";
234 if (usb_pipebulk(pipe))
235 return "Bulk";
236 return "?";
237}
238
239/* dump_urb
240 * write content of URB to syslog for debugging
241 */
242static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700243 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800244{
245#ifdef CONFIG_GIGASET_DEBUG
246 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700247 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800248 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700249 gig_dbg(level,
250 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
251 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
252 (unsigned long) urb->dev,
253 usb_pipetype_str(urb->pipe),
254 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
255 usb_pipein(urb->pipe) ? "in" : "out",
256 urb->status, (unsigned long) urb->hcpriv,
257 urb->transfer_flags);
258 gig_dbg(level,
259 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
260 "bandwidth=%d, setup_packet=0x%08lx,",
261 (unsigned long) urb->transfer_buffer,
262 urb->transfer_buffer_length, urb->actual_length,
263 urb->bandwidth, (unsigned long) urb->setup_packet);
264 gig_dbg(level,
265 " start_frame=%d, number_of_packets=%d, interval=%d, "
266 "error_count=%d,",
267 urb->start_frame, urb->number_of_packets, urb->interval,
268 urb->error_count);
269 gig_dbg(level,
270 " context=0x%08lx, complete=0x%08lx, "
271 "iso_frame_desc[]={",
272 (unsigned long) urb->context,
273 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800274 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700275 struct usb_iso_packet_descriptor *pifd
276 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700277 gig_dbg(level,
278 " {offset=%u, length=%u, actual_length=%u, "
279 "status=%u}",
280 pifd->offset, pifd->length, pifd->actual_length,
281 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800282 }
283 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700284 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800285#endif
286}
287
288/* read/set modem control bits etc. (m10x only) */
289static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700290 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800291{
292 return -EINVAL;
293}
294
295static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
296{
297 return -EINVAL;
298}
299
300static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
301{
302 return -EINVAL;
303}
304
305/* error_hangup
306 * hang up any existing connection because of an unrecoverable error
307 * This function may be called from any context and takes care of scheduling
308 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700309 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800310 * argument:
311 * B channel control structure
312 */
313static inline void error_hangup(struct bc_state *bcs)
314{
315 struct cardstate *cs = bcs->cs;
316
Tilman Schmidt784d5852006-04-10 22:55:04 -0700317 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
318 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800319
Tilman Schmidt73a88812006-04-22 02:35:30 -0700320 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
321 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800322
323 gigaset_schedule_event(cs);
324}
325
326/* error_reset
327 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700328 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700329 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700330 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800331 * argument:
332 * controller state structure
333 */
334static inline void error_reset(struct cardstate *cs)
335{
Tilman Schmidt06163f82006-06-26 00:25:33 -0700336 /* close AT command channel to recover (ignore errors) */
337 req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
338
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800339 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700340 dev_err(cs->dev,
341 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800342}
343
344/* check_pending
345 * check for completion of pending control request
346 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700347 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800348 */
349static void check_pending(struct bas_cardstate *ucs)
350{
351 unsigned long flags;
352
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800353 spin_lock_irqsave(&ucs->lock, flags);
354 switch (ucs->pending) {
355 case 0:
356 break;
357 case HD_OPEN_ATCHANNEL:
358 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
359 ucs->pending = 0;
360 break;
361 case HD_OPEN_B1CHANNEL:
362 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
363 ucs->pending = 0;
364 break;
365 case HD_OPEN_B2CHANNEL:
366 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
367 ucs->pending = 0;
368 break;
369 case HD_CLOSE_ATCHANNEL:
370 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
371 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800372 break;
373 case HD_CLOSE_B1CHANNEL:
374 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
375 ucs->pending = 0;
376 break;
377 case HD_CLOSE_B2CHANNEL:
378 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
379 ucs->pending = 0;
380 break;
381 case HD_DEVICE_INIT_ACK: /* no reply expected */
382 ucs->pending = 0;
383 break;
384 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
385 * are handled separately and should never end up here
386 */
387 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700388 dev_warn(&ucs->interface->dev,
389 "unknown pending request 0x%02x cleared\n",
390 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800391 ucs->pending = 0;
392 }
393
394 if (!ucs->pending)
395 del_timer(&ucs->timer_ctrl);
396
397 spin_unlock_irqrestore(&ucs->lock, flags);
398}
399
400/* cmd_in_timeout
401 * timeout routine for command input request
402 * argument:
403 * controller state structure
404 */
405static void cmd_in_timeout(unsigned long data)
406{
407 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700408 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700409 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800410
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800411 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700412 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800413 return;
414 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800415
Tilman Schmidt06163f82006-06-26 00:25:33 -0700416 if (ucs->retry_cmd_in++ < BAS_RETRY) {
417 dev_notice(cs->dev, "control read: timeout, retry %d\n",
418 ucs->retry_cmd_in);
419 rc = atread_submit(cs, BAS_TIMEOUT);
420 if (rc >= 0 || rc == -ENODEV)
421 /* resubmitted or disconnected */
422 /* - bypass regular exit block */
423 return;
424 } else {
425 dev_err(cs->dev,
426 "control read: timeout, giving up after %d tries\n",
427 ucs->retry_cmd_in);
428 }
429 kfree(ucs->rcvbuf);
430 ucs->rcvbuf = NULL;
431 ucs->rcvbuf_size = 0;
432 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800433}
434
Tilman Schmidt73a88812006-04-22 02:35:30 -0700435/* set/clear bits in base connection state, return previous state
436 */
437inline static int update_basstate(struct bas_cardstate *ucs,
438 int set, int clear)
439{
440 unsigned long flags;
441 int state;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800442
Tilman Schmidt73a88812006-04-22 02:35:30 -0700443 spin_lock_irqsave(&ucs->lock, flags);
444 state = atomic_read(&ucs->basstate);
445 atomic_set(&ucs->basstate, (state & ~clear) | set);
446 spin_unlock_irqrestore(&ucs->lock, flags);
447 return state;
448}
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800449
Tilman Schmidt06163f82006-06-26 00:25:33 -0700450/* read_ctrl_callback
451 * USB completion handler for control pipe input
452 * called by the USB subsystem in interrupt context
453 * parameter:
454 * urb USB request block
455 * urb->context = inbuf structure for controller state
456 */
David Howells7d12e782006-10-05 14:55:46 +0100457static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700458{
459 struct inbuf_t *inbuf = urb->context;
460 struct cardstate *cs = inbuf->cs;
461 struct bas_cardstate *ucs = cs->hw.bas;
462 int have_data = 0;
463 unsigned numbytes;
464 int rc;
465
466 update_basstate(ucs, 0, BS_ATRDPEND);
467
468 if (!ucs->rcvbuf_size) {
469 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
470 return;
471 }
472
473 del_timer(&ucs->timer_cmd_in);
474
475 switch (urb->status) {
476 case 0: /* normal completion */
477 numbytes = urb->actual_length;
478 if (unlikely(numbytes != ucs->rcvbuf_size)) {
479 dev_warn(cs->dev,
480 "control read: received %d chars, expected %d\n",
481 numbytes, ucs->rcvbuf_size);
482 if (numbytes > ucs->rcvbuf_size)
483 numbytes = ucs->rcvbuf_size;
484 }
485
486 /* copy received bytes to inbuf */
487 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
488
489 if (unlikely(numbytes < ucs->rcvbuf_size)) {
490 /* incomplete - resubmit for remaining bytes */
491 ucs->rcvbuf_size -= numbytes;
492 ucs->retry_cmd_in = 0;
493 rc = atread_submit(cs, BAS_TIMEOUT);
494 if (rc >= 0 || rc == -ENODEV)
495 /* resubmitted or disconnected */
496 /* - bypass regular exit block */
497 return;
498 error_reset(cs);
499 }
500 break;
501
502 case -ENOENT: /* cancelled */
503 case -ECONNRESET: /* cancelled (async) */
504 case -EINPROGRESS: /* pending */
505 case -ENODEV: /* device removed */
506 case -ESHUTDOWN: /* device shut down */
507 /* no action necessary */
508 gig_dbg(DEBUG_USBREQ, "%s: %s",
509 __func__, get_usb_statmsg(urb->status));
510 break;
511
512 default: /* severe trouble */
513 dev_warn(cs->dev, "control read: %s\n",
514 get_usb_statmsg(urb->status));
515 if (ucs->retry_cmd_in++ < BAS_RETRY) {
516 dev_notice(cs->dev, "control read: retry %d\n",
517 ucs->retry_cmd_in);
518 rc = atread_submit(cs, BAS_TIMEOUT);
519 if (rc >= 0 || rc == -ENODEV)
520 /* resubmitted or disconnected */
521 /* - bypass regular exit block */
522 return;
523 } else {
524 dev_err(cs->dev,
525 "control read: giving up after %d tries\n",
526 ucs->retry_cmd_in);
527 }
528 error_reset(cs);
529 }
530
531 kfree(ucs->rcvbuf);
532 ucs->rcvbuf = NULL;
533 ucs->rcvbuf_size = 0;
534 if (have_data) {
535 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
536 gigaset_schedule_event(cs);
537 }
538}
539
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800540/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700541 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800542 * parameters:
543 * cs controller state structure
544 * timeout timeout in 1/10 sec., 0: none
545 * return value:
546 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800547 * -EBUSY if another request is pending
548 * any URB submission error code
549 */
550static int atread_submit(struct cardstate *cs, int timeout)
551{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700552 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800553 int ret;
554
Tilman Schmidt784d5852006-04-10 22:55:04 -0700555 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
556 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800557
Tilman Schmidt73a88812006-04-22 02:35:30 -0700558 if (update_basstate(ucs, BS_ATRDPEND, 0) & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700559 dev_err(cs->dev,
560 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800561 return -EBUSY;
562 }
563
564 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
565 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
566 ucs->dr_cmd_in.wValue = 0;
567 ucs->dr_cmd_in.wIndex = 0;
568 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
569 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700570 usb_rcvctrlpipe(ucs->udev, 0),
571 (unsigned char*) & ucs->dr_cmd_in,
572 ucs->rcvbuf, ucs->rcvbuf_size,
573 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800574
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800575 if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700576 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700577 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700578 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800579 return ret;
580 }
581
582 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700583 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800584 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
585 ucs->timer_cmd_in.data = (unsigned long) cs;
586 ucs->timer_cmd_in.function = cmd_in_timeout;
587 add_timer(&ucs->timer_cmd_in);
588 }
589 return 0;
590}
591
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800592/* read_int_callback
593 * USB completion handler for interrupt pipe input
594 * called by the USB subsystem in interrupt context
595 * parameter:
596 * urb USB request block
597 * urb->context = controller state structure
598 */
David Howells7d12e782006-10-05 14:55:46 +0100599static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800600{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700601 struct cardstate *cs = urb->context;
602 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800603 struct bc_state *bcs;
604 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700605 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800606 unsigned l;
607 int channel;
608
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800609 switch (urb->status) {
610 case 0: /* success */
611 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700612 case -ENOENT: /* cancelled */
613 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800614 case -EINPROGRESS: /* pending */
615 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700616 gig_dbg(DEBUG_USBREQ, "%s: %s",
617 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800618 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700619 case -ENODEV: /* device removed */
620 case -ESHUTDOWN: /* device shut down */
621 //FIXME use this as disconnect indicator?
622 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
623 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800624 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700625 dev_warn(cs->dev, "interrupt read: %s\n",
626 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800627 //FIXME corrective action? resubmission always ok?
628 goto resubmit;
629 }
630
Tilman Schmidt73a88812006-04-22 02:35:30 -0700631 /* drop incomplete packets even if the missing bytes wouldn't matter */
632 if (unlikely(urb->actual_length < 3)) {
633 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
634 urb->actual_length);
635 goto resubmit;
636 }
637
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800638 l = (unsigned) ucs->int_in_buf[1] +
639 (((unsigned) ucs->int_in_buf[2]) << 8);
640
Tilman Schmidt784d5852006-04-10 22:55:04 -0700641 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
642 urb->actual_length, (int)ucs->int_in_buf[0], l,
643 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800644
645 channel = 0;
646
647 switch (ucs->int_in_buf[0]) {
648 case HD_DEVICE_INIT_OK:
649 update_basstate(ucs, BS_INIT, 0);
650 break;
651
652 case HD_READY_SEND_ATDATA:
653 del_timer(&ucs->timer_atrdy);
654 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
655 start_cbsend(cs);
656 break;
657
658 case HD_OPEN_B2CHANNEL_ACK:
659 ++channel;
660 case HD_OPEN_B1CHANNEL_ACK:
661 bcs = cs->bcs + channel;
662 update_basstate(ucs, BS_B1OPEN << channel, 0);
663 gigaset_bchannel_up(bcs);
664 break;
665
666 case HD_OPEN_ATCHANNEL_ACK:
667 update_basstate(ucs, BS_ATOPEN, 0);
668 start_cbsend(cs);
669 break;
670
671 case HD_CLOSE_B2CHANNEL_ACK:
672 ++channel;
673 case HD_CLOSE_B1CHANNEL_ACK:
674 bcs = cs->bcs + channel;
675 update_basstate(ucs, 0, BS_B1OPEN << channel);
676 stopurbs(bcs->hw.bas);
677 gigaset_bchannel_down(bcs);
678 break;
679
680 case HD_CLOSE_ATCHANNEL_ACK:
681 update_basstate(ucs, 0, BS_ATOPEN);
682 break;
683
684 case HD_B2_FLOW_CONTROL:
685 ++channel;
686 case HD_B1_FLOW_CONTROL:
687 bcs = cs->bcs + channel;
688 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700689 &bcs->hw.bas->corrbytes);
690 gig_dbg(DEBUG_ISO,
691 "Flow control (channel %d, sub %d): 0x%02x => %d",
692 channel, bcs->hw.bas->numsub, l,
693 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800694 break;
695
696 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
697 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700698 dev_warn(cs->dev,
699 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800700 break;
701 }
702 spin_lock_irqsave(&cs->lock, flags);
703 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700704 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700705 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700706 "receive AT data overrun, %d bytes lost\n",
707 ucs->rcvbuf_size);
708 kfree(ucs->rcvbuf);
709 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800710 }
711 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
712 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700713 dev_err(cs->dev, "out of memory receiving AT data\n");
714 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800715 break;
716 }
717 ucs->rcvbuf_size = l;
718 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700719 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800720 kfree(ucs->rcvbuf);
721 ucs->rcvbuf = NULL;
722 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700723 if (rc != -ENODEV) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700724 //FIXME corrective action?
Tilman Schmidt06163f82006-06-26 00:25:33 -0700725 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700726 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700727 break;
728 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800729 }
730 spin_unlock_irqrestore(&cs->lock, flags);
731 break;
732
733 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700734 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800735 break;
736
737 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700738 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800739 break;
740
741 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700742 dev_warn(cs->dev,
743 "unknown Gigaset signal 0x%02x (%u) ignored\n",
744 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800745 }
746
747 check_pending(ucs);
748
749resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800750 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700751 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700752 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700753 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800754 error_reset(cs);
755 }
756}
757
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800758/* read_iso_callback
759 * USB completion handler for B channel isochronous input
760 * called by the USB subsystem in interrupt context
761 * parameter:
762 * urb USB request block of completed request
763 * urb->context = bc_state structure
764 */
David Howells7d12e782006-10-05 14:55:46 +0100765static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800766{
767 struct bc_state *bcs;
768 struct bas_bc_state *ubc;
769 unsigned long flags;
770 int i, rc;
771
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800772 /* status codes not worth bothering the tasklet with */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700773 if (unlikely(urb->status == -ENOENT ||
774 urb->status == -ECONNRESET ||
775 urb->status == -EINPROGRESS ||
776 urb->status == -ENODEV ||
777 urb->status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700778 gig_dbg(DEBUG_ISO, "%s: %s",
779 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800780 return;
781 }
782
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700783 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800784 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800785
786 spin_lock_irqsave(&ubc->isoinlock, flags);
787 if (likely(ubc->isoindone == NULL)) {
788 /* pass URB to tasklet */
789 ubc->isoindone = urb;
790 tasklet_schedule(&ubc->rcvd_tasklet);
791 } else {
792 /* tasklet still busy, drop data and resubmit URB */
793 ubc->loststatus = urb->status;
794 for (i = 0; i < BAS_NUMFRAMES; i++) {
795 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
796 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700797 urb->iso_frame_desc[i].status !=
798 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800799 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800800 urb->iso_frame_desc[i].status = 0;
801 urb->iso_frame_desc[i].actual_length = 0;
802 }
803 if (likely(atomic_read(&ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700804 /* urb->dev is clobbered by USB subsystem */
805 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800806 urb->transfer_flags = URB_ISO_ASAP;
807 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700808 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
809 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800810 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700811 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700812 dev_err(bcs->cs->dev,
813 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700814 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800815 dump_urb(DEBUG_ISO, "isoc read", urb);
816 error_hangup(bcs);
817 }
818 }
819 }
820 spin_unlock_irqrestore(&ubc->isoinlock, flags);
821}
822
823/* write_iso_callback
824 * USB completion handler for B channel isochronous output
825 * called by the USB subsystem in interrupt context
826 * parameter:
827 * urb USB request block of completed request
828 * urb->context = isow_urbctx_t structure
829 */
David Howells7d12e782006-10-05 14:55:46 +0100830static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800831{
832 struct isow_urbctx_t *ucx;
833 struct bas_bc_state *ubc;
834 unsigned long flags;
835
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800836 /* status codes not worth bothering the tasklet with */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700837 if (unlikely(urb->status == -ENOENT ||
838 urb->status == -ECONNRESET ||
839 urb->status == -EINPROGRESS ||
840 urb->status == -ENODEV ||
841 urb->status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700842 gig_dbg(DEBUG_ISO, "%s: %s",
843 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800844 return;
845 }
846
847 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700848 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800849 ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800850
851 spin_lock_irqsave(&ubc->isooutlock, flags);
852 ubc->isooutovfl = ubc->isooutdone;
853 ubc->isooutdone = ucx;
854 spin_unlock_irqrestore(&ubc->isooutlock, flags);
855 tasklet_schedule(&ubc->sent_tasklet);
856}
857
858/* starturbs
859 * prepare and submit USB request blocks for isochronous input and output
860 * argument:
861 * B channel control structure
862 * return value:
863 * 0 on success
864 * < 0 on error (no URBs submitted)
865 */
866static int starturbs(struct bc_state *bcs)
867{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700868 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800869 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800870 int j, k;
871 int rc;
872
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800873 /* initialize L2 reception */
874 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
875 bcs->inputstate |= INS_flag_hunt;
876
877 /* submit all isochronous input URBs */
878 atomic_set(&ubc->running, 1);
879 for (k = 0; k < BAS_INURBS; k++) {
880 urb = ubc->isoinurbs[k];
881 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800882 rc = -EFAULT;
883 goto error;
884 }
885
886 urb->dev = bcs->cs->hw.bas->udev;
887 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
888 urb->transfer_flags = URB_ISO_ASAP;
889 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
890 urb->transfer_buffer_length = BAS_INBUFSIZE;
891 urb->number_of_packets = BAS_NUMFRAMES;
892 urb->interval = BAS_FRAMETIME;
893 urb->complete = read_iso_callback;
894 urb->context = bcs;
895 for (j = 0; j < BAS_NUMFRAMES; j++) {
896 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
897 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
898 urb->iso_frame_desc[j].status = 0;
899 urb->iso_frame_desc[j].actual_length = 0;
900 }
901
902 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800903 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800904 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800905 }
906
907 /* initialize L2 transmission */
908 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
909
910 /* set up isochronous output URBs for flag idling */
911 for (k = 0; k < BAS_OUTURBS; ++k) {
912 urb = ubc->isoouturbs[k].urb;
913 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800914 rc = -EFAULT;
915 goto error;
916 }
917 urb->dev = bcs->cs->hw.bas->udev;
918 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
919 urb->transfer_flags = URB_ISO_ASAP;
920 urb->transfer_buffer = ubc->isooutbuf->data;
921 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
922 urb->number_of_packets = BAS_NUMFRAMES;
923 urb->interval = BAS_FRAMETIME;
924 urb->complete = write_iso_callback;
925 urb->context = &ubc->isoouturbs[k];
926 for (j = 0; j < BAS_NUMFRAMES; ++j) {
927 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
928 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
929 urb->iso_frame_desc[j].status = 0;
930 urb->iso_frame_desc[j].actual_length = 0;
931 }
932 ubc->isoouturbs[k].limit = -1;
933 }
934
935 /* submit two URBs, keep third one */
936 for (k = 0; k < 2; ++k) {
937 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800938 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700939 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800940 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800941 }
942 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
943 ubc->isooutfree = &ubc->isoouturbs[2];
944 ubc->isooutdone = ubc->isooutovfl = NULL;
945 return 0;
946 error:
947 stopurbs(ubc);
948 return rc;
949}
950
951/* stopurbs
952 * cancel the USB request blocks for isochronous input and output
953 * errors are silently ignored
954 * argument:
955 * B channel control structure
956 */
957static void stopurbs(struct bas_bc_state *ubc)
958{
959 int k, rc;
960
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800961 atomic_set(&ubc->running, 0);
962
963 for (k = 0; k < BAS_INURBS; ++k) {
964 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700965 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700966 "%s: isoc input URB %d unlinked, result = %s",
967 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800968 }
969
970 for (k = 0; k < BAS_OUTURBS; ++k) {
971 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700972 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700973 "%s: isoc output URB %d unlinked, result = %s",
974 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800975 }
976}
977
978/* Isochronous Write - Bottom Half */
979/* =============================== */
980
981/* submit_iso_write_urb
982 * fill and submit the next isochronous write URB
983 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700984 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800985 * return value:
986 * number of frames submitted in URB
987 * 0 if URB not submitted because no data available (isooutbuf busy)
988 * error code < 0 on error
989 */
990static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
991{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700992 struct urb *urb = ucx->urb;
993 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800994 struct usb_iso_packet_descriptor *ifd;
995 int corrbytes, nframe, rc;
996
Tilman Schmidt784d5852006-04-10 22:55:04 -0700997 /* urb->dev is clobbered by USB subsystem */
998 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800999 urb->transfer_flags = URB_ISO_ASAP;
1000 urb->transfer_buffer = ubc->isooutbuf->data;
1001 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1002
1003 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1004 ifd = &urb->iso_frame_desc[nframe];
1005
1006 /* compute frame length according to flow control */
1007 ifd->length = BAS_NORMFRAME;
1008 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001009 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1010 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001011 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1012 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1013 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1014 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1015 ifd->length += corrbytes;
1016 atomic_add(-corrbytes, &ubc->corrbytes);
1017 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001018
1019 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -07001020 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1021 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001022 if (ifd->offset < 0) {
1023 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001024 gig_dbg(DEBUG_ISO,
1025 "%s: buffer busy at frame %d",
1026 __func__, nframe);
1027 /* tasklet will be restarted from
1028 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001029 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001030 dev_err(ucx->bcs->cs->dev,
1031 "%s: buffer error %d at frame %d\n",
1032 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001033 return ifd->offset;
1034 }
1035 break;
1036 }
1037 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1038 ifd->status = 0;
1039 ifd->actual_length = 0;
1040 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001041 if (unlikely(nframe == 0))
1042 return 0; /* no data to send */
1043 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001044
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001045 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001046 if (unlikely(rc)) {
1047 if (rc == -ENODEV)
1048 /* device removed - give up silently */
1049 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1050 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001051 dev_err(ucx->bcs->cs->dev,
1052 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001053 get_usb_rcmsg(rc));
1054 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001055 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001056 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001057 return nframe;
1058}
1059
1060/* write_iso_tasklet
1061 * tasklet scheduled when an isochronous output URB from the Gigaset device
1062 * has completed
1063 * parameter:
1064 * data B channel state structure
1065 */
1066static void write_iso_tasklet(unsigned long data)
1067{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001068 struct bc_state *bcs = (struct bc_state *) data;
1069 struct bas_bc_state *ubc = bcs->hw.bas;
1070 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001071 struct isow_urbctx_t *done, *next, *ovfl;
1072 struct urb *urb;
1073 struct usb_iso_packet_descriptor *ifd;
1074 int offset;
1075 unsigned long flags;
1076 int i;
1077 struct sk_buff *skb;
1078 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001079 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001080
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001081 /* loop while completed URBs arrive in time */
1082 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001083 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001084 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001085 return;
1086 }
1087
1088 /* retrieve completed URBs */
1089 spin_lock_irqsave(&ubc->isooutlock, flags);
1090 done = ubc->isooutdone;
1091 ubc->isooutdone = NULL;
1092 ovfl = ubc->isooutovfl;
1093 ubc->isooutovfl = NULL;
1094 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1095 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001096 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001097 error_hangup(bcs);
1098 break;
1099 }
1100 if (!done)
1101 break;
1102
1103 /* submit free URB if available */
1104 spin_lock_irqsave(&ubc->isooutlock, flags);
1105 next = ubc->isooutfree;
1106 ubc->isooutfree = NULL;
1107 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1108 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001109 rc = submit_iso_write_urb(next);
1110 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001111 /* could not submit URB, put it back */
1112 spin_lock_irqsave(&ubc->isooutlock, flags);
1113 if (ubc->isooutfree == NULL) {
1114 ubc->isooutfree = next;
1115 next = NULL;
1116 }
1117 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1118 if (next) {
1119 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001120 dev_err(cs->dev,
1121 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001122 error_hangup(bcs);
1123 }
1124 }
1125 }
1126
1127 /* process completed URB */
1128 urb = done->urb;
1129 switch (urb->status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001130 case -EXDEV: /* partial completion */
1131 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1132 __func__);
1133 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001134 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001135 /* inspect individual frames
1136 * assumptions (for lack of documentation):
1137 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001138 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001139 * - all following frames are not sent at all
1140 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001141 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001142 for (i = 0; i < BAS_NUMFRAMES; i++) {
1143 ifd = &urb->iso_frame_desc[i];
1144 if (ifd->status ||
1145 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001146 dev_warn(cs->dev,
1147 "isochronous write: frame %d: %s, "
1148 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001149 i, get_usb_statmsg(ifd->status),
1150 ifd->actual_length, ifd->length);
1151 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001152 ifd->actual_length)
1153 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001154 break;
1155 }
1156 }
1157#ifdef CONFIG_GIGASET_DEBUG
1158 /* check assumption on remaining frames */
1159 for (; i < BAS_NUMFRAMES; i++) {
1160 ifd = &urb->iso_frame_desc[i];
1161 if (ifd->status != -EINPROGRESS
1162 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001163 dev_warn(cs->dev,
1164 "isochronous write: frame %d: %s, "
1165 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001166 i, get_usb_statmsg(ifd->status),
1167 ifd->actual_length, ifd->length);
1168 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001169 ifd->actual_length)
1170 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001171 break;
1172 }
1173 }
1174#endif
1175 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001176 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001177 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001178 error_hangup(bcs);
1179 break;
1180 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001181 dev_warn(cs->dev, "isochronous write: %s\n",
1182 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001183 }
1184
1185 /* mark the write buffer area covered by this URB as free */
1186 if (done->limit >= 0)
1187 atomic_set(&ubc->isooutbuf->read, done->limit);
1188
1189 /* mark URB as free */
1190 spin_lock_irqsave(&ubc->isooutlock, flags);
1191 next = ubc->isooutfree;
1192 ubc->isooutfree = done;
1193 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1194 if (next) {
1195 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001196 rc = submit_iso_write_urb(next);
1197 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001198 /* couldn't submit */
1199 error_hangup(bcs);
1200 }
1201 }
1202 }
1203
1204 /* process queued SKBs */
1205 while ((skb = skb_dequeue(&bcs->squeue))) {
1206 /* copy to output buffer, doing L2 encapsulation */
1207 len = skb->len;
1208 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1209 /* insufficient buffer space, push back onto queue */
1210 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001211 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1212 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001213 break;
1214 }
1215 skb_pull(skb, len);
1216 gigaset_skb_sent(bcs, skb);
1217 dev_kfree_skb_any(skb);
1218 }
1219}
1220
1221/* Isochronous Read - Bottom Half */
1222/* ============================== */
1223
1224/* read_iso_tasklet
1225 * tasklet scheduled when an isochronous input URB from the Gigaset device
1226 * has completed
1227 * parameter:
1228 * data B channel state structure
1229 */
1230static void read_iso_tasklet(unsigned long data)
1231{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001232 struct bc_state *bcs = (struct bc_state *) data;
1233 struct bas_bc_state *ubc = bcs->hw.bas;
1234 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001235 struct urb *urb;
1236 char *rcvbuf;
1237 unsigned long flags;
1238 int totleft, numbytes, offset, frame, rc;
1239
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001240 /* loop while more completed URBs arrive in the meantime */
1241 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001242 /* retrieve URB */
1243 spin_lock_irqsave(&ubc->isoinlock, flags);
1244 if (!(urb = ubc->isoindone)) {
1245 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1246 return;
1247 }
1248 ubc->isoindone = NULL;
1249 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001250 dev_warn(cs->dev,
1251 "isochronous read overrun, "
1252 "dropped URB with status: %s, %d bytes lost\n",
1253 get_usb_statmsg(ubc->loststatus),
1254 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001255 ubc->loststatus = -EINPROGRESS;
1256 }
1257 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1258
1259 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001260 gig_dbg(DEBUG_ISO,
1261 "%s: channel not running, "
1262 "dropped URB with status: %s",
1263 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001264 return;
1265 }
1266
1267 switch (urb->status) {
1268 case 0: /* normal completion */
1269 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001270 case -EXDEV: /* inspect individual frames
1271 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001272 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1273 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001274 break;
1275 case -ENOENT:
1276 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001277 case -EINPROGRESS:
1278 gig_dbg(DEBUG_ISO, "%s: %s",
1279 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001280 continue; /* -> skip */
1281 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001282 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001283 error_hangup(bcs);
1284 continue; /* -> skip */
1285 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001286 dev_warn(cs->dev, "isochronous read: %s\n",
1287 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001288 goto error;
1289 }
1290
1291 rcvbuf = urb->transfer_buffer;
1292 totleft = urb->actual_length;
1293 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1294 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001295 dev_warn(cs->dev,
1296 "isochronous read: frame %d: %s\n",
1297 frame,
1298 get_usb_statmsg(
1299 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001300 break;
1301 }
1302 numbytes = urb->iso_frame_desc[frame].actual_length;
1303 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001304 dev_warn(cs->dev,
1305 "isochronous read: frame %d: "
1306 "numbytes (%d) > BAS_MAXFRAME\n",
1307 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001308 break;
1309 }
1310 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001311 dev_warn(cs->dev,
1312 "isochronous read: frame %d: "
1313 "numbytes (%d) > totleft (%d)\n",
1314 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001315 break;
1316 }
1317 offset = urb->iso_frame_desc[frame].offset;
1318 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001319 dev_warn(cs->dev,
1320 "isochronous read: frame %d: "
1321 "offset (%d) + numbytes (%d) "
1322 "> BAS_INBUFSIZE\n",
1323 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001324 break;
1325 }
1326 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1327 totleft -= numbytes;
1328 }
1329 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001330 dev_warn(cs->dev,
1331 "isochronous read: %d data bytes missing\n",
1332 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001333
1334 error:
1335 /* URB processed, resubmit */
1336 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1337 urb->iso_frame_desc[frame].status = 0;
1338 urb->iso_frame_desc[frame].actual_length = 0;
1339 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001340 /* urb->dev is clobbered by USB subsystem */
1341 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001342 urb->transfer_flags = URB_ISO_ASAP;
1343 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001344 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001345 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001346 dev_err(cs->dev,
1347 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001348 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001349 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1350 error_hangup(bcs);
1351 }
1352 }
1353}
1354
1355/* Channel Operations */
1356/* ================== */
1357
1358/* req_timeout
1359 * timeout routine for control output request
1360 * argument:
1361 * B channel control structure
1362 */
1363static void req_timeout(unsigned long data)
1364{
1365 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001366 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001367 int pending;
1368 unsigned long flags;
1369
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001370 check_pending(ucs);
1371
1372 spin_lock_irqsave(&ucs->lock, flags);
1373 pending = ucs->pending;
1374 ucs->pending = 0;
1375 spin_unlock_irqrestore(&ucs->lock, flags);
1376
1377 switch (pending) {
1378 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001379 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001380 break;
1381
1382 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001383 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001384 error_reset(bcs->cs);
1385 break;
1386
1387 case HD_OPEN_B2CHANNEL:
1388 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001389 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1390 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001391 error_hangup(bcs);
1392 break;
1393
1394 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001395 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001396 break;
1397
1398 case HD_CLOSE_B2CHANNEL:
1399 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001400 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1401 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001402 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001403 break;
1404
1405 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001406 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1407 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001408 }
1409}
1410
1411/* write_ctrl_callback
1412 * USB completion handler for control pipe output
1413 * called by the USB subsystem in interrupt context
1414 * parameter:
1415 * urb USB request block of completed request
1416 * urb->context = hardware specific controller state structure
1417 */
David Howells7d12e782006-10-05 14:55:46 +01001418static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001419{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001420 struct bas_cardstate *ucs = urb->context;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001421 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001422 unsigned long flags;
1423
Tilman Schmidt06163f82006-06-26 00:25:33 -07001424 /* check status */
1425 switch (urb->status) {
1426 case 0: /* normal completion */
1427 spin_lock_irqsave(&ucs->lock, flags);
1428 switch (ucs->pending) {
1429 case HD_DEVICE_INIT_ACK: /* no reply expected */
1430 del_timer(&ucs->timer_ctrl);
1431 ucs->pending = 0;
1432 break;
1433 }
1434 spin_unlock_irqrestore(&ucs->lock, flags);
1435 return;
1436
1437 case -ENOENT: /* cancelled */
1438 case -ECONNRESET: /* cancelled (async) */
1439 case -EINPROGRESS: /* pending */
1440 case -ENODEV: /* device removed */
1441 case -ESHUTDOWN: /* device shut down */
1442 /* ignore silently */
1443 gig_dbg(DEBUG_USBREQ, "%s: %s",
1444 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001445 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001446
1447 default: /* any failure */
1448 if (++ucs->retry_ctrl > BAS_RETRY) {
1449 dev_err(&ucs->interface->dev,
1450 "control request 0x%02x failed: %s\n",
1451 ucs->dr_ctrl.bRequest,
1452 get_usb_statmsg(urb->status));
1453 break; /* give up */
1454 }
1455 dev_notice(&ucs->interface->dev,
1456 "control request 0x%02x: %s, retry %d\n",
1457 ucs->dr_ctrl.bRequest, get_usb_statmsg(urb->status),
1458 ucs->retry_ctrl);
1459 /* urb->dev is clobbered by USB subsystem */
1460 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001461 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001462 if (unlikely(rc)) {
1463 dev_err(&ucs->interface->dev,
1464 "could not resubmit request 0x%02x: %s\n",
1465 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1466 break;
1467 }
1468 /* resubmitted */
1469 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001470 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001471
1472 /* failed, clear pending request */
1473 spin_lock_irqsave(&ucs->lock, flags);
1474 del_timer(&ucs->timer_ctrl);
1475 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001476 spin_unlock_irqrestore(&ucs->lock, flags);
1477}
1478
1479/* req_submit
1480 * submit a control output request without message buffer to the Gigaset base
1481 * and optionally start a timeout
1482 * parameters:
1483 * bcs B channel control structure
1484 * req control request code (HD_*)
1485 * val control request parameter value (set to 0 if unused)
1486 * timeout timeout in seconds (0: no timeout)
1487 * return value:
1488 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001489 * -EBUSY if another request is pending
1490 * any URB submission error code
1491 */
1492static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1493{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001494 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001495 int ret;
1496 unsigned long flags;
1497
Tilman Schmidt784d5852006-04-10 22:55:04 -07001498 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001499
1500 spin_lock_irqsave(&ucs->lock, flags);
1501 if (ucs->pending) {
1502 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001503 dev_err(bcs->cs->dev,
1504 "submission of request 0x%02x failed: "
1505 "request 0x%02x still pending\n",
1506 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001507 return -EBUSY;
1508 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001509
1510 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1511 ucs->dr_ctrl.bRequest = req;
1512 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1513 ucs->dr_ctrl.wIndex = 0;
1514 ucs->dr_ctrl.wLength = 0;
1515 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001516 usb_sndctrlpipe(ucs->udev, 0),
1517 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1518 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001519 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001520 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001521 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001522 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001523 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001524 spin_unlock_irqrestore(&ucs->lock, flags);
1525 return ret;
1526 }
1527 ucs->pending = req;
1528
1529 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001530 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001531 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1532 ucs->timer_ctrl.data = (unsigned long) bcs;
1533 ucs->timer_ctrl.function = req_timeout;
1534 add_timer(&ucs->timer_ctrl);
1535 }
1536
1537 spin_unlock_irqrestore(&ucs->lock, flags);
1538 return 0;
1539}
1540
1541/* gigaset_init_bchannel
1542 * called by common.c to connect a B channel
1543 * initialize isochronous I/O and tell the Gigaset base to open the channel
1544 * argument:
1545 * B channel control structure
1546 * return value:
1547 * 0 on success, error code < 0 on error
1548 */
1549static int gigaset_init_bchannel(struct bc_state *bcs)
1550{
1551 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001552 unsigned long flags;
1553
1554 spin_lock_irqsave(&bcs->cs->lock, flags);
1555 if (unlikely(!bcs->cs->connected)) {
1556 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1557 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1558 return -ENODEV;
1559 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001560
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001561 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001562 dev_err(bcs->cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001563 "could not start isochronous I/O for channel B%d: %s\n",
1564 bcs->channel + 1,
1565 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1566 if (ret != -ENODEV)
1567 error_hangup(bcs);
1568 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001569 return ret;
1570 }
1571
1572 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1573 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001574 dev_err(bcs->cs->dev, "could not open channel B%d\n",
1575 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001576 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001577 if (ret != -ENODEV)
1578 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001579 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001580
1581 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001582 return ret;
1583}
1584
1585/* gigaset_close_bchannel
1586 * called by common.c to disconnect a B channel
1587 * tell the Gigaset base to close the channel
1588 * stopping isochronous I/O and LL notification will be done when the
1589 * acknowledgement for the close arrives
1590 * argument:
1591 * B channel control structure
1592 * return value:
1593 * 0 on success, error code < 0 on error
1594 */
1595static int gigaset_close_bchannel(struct bc_state *bcs)
1596{
1597 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001598 unsigned long flags;
1599
1600 spin_lock_irqsave(&bcs->cs->lock, flags);
1601 if (unlikely(!bcs->cs->connected)) {
1602 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1603 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1604 return -ENODEV;
1605 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001606
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001607 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1608 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1609 /* channel not running: just signal common.c */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001610 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001611 gigaset_bchannel_down(bcs);
1612 return 0;
1613 }
1614
Tilman Schmidt73a88812006-04-22 02:35:30 -07001615 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001616 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1617 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidt73a88812006-04-22 02:35:30 -07001618 dev_err(bcs->cs->dev, "closing channel B%d failed\n",
1619 bcs->channel + 1);
1620
1621 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001622 return ret;
1623}
1624
1625/* Device Operations */
1626/* ================= */
1627
1628/* complete_cb
1629 * unqueue first command buffer from queue, waking any sleepers
1630 * must be called with cs->cmdlock held
1631 * parameter:
1632 * cs controller state structure
1633 */
1634static void complete_cb(struct cardstate *cs)
1635{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001636 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001637
1638 /* unqueue completed buffer */
1639 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001640 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1641 "write_command: sent %u bytes, %u left",
1642 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001643 if ((cs->cmdbuf = cb->next) != NULL) {
1644 cs->cmdbuf->prev = NULL;
1645 cs->curlen = cs->cmdbuf->len;
1646 } else {
1647 cs->lastcmdbuf = NULL;
1648 cs->curlen = 0;
1649 }
1650
1651 if (cb->wake_tasklet)
1652 tasklet_schedule(cb->wake_tasklet);
1653
1654 kfree(cb);
1655}
1656
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001657/* write_command_callback
1658 * USB completion handler for AT command transmission
1659 * called by the USB subsystem in interrupt context
1660 * parameter:
1661 * urb USB request block of completed request
1662 * urb->context = controller state structure
1663 */
David Howells7d12e782006-10-05 14:55:46 +01001664static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001665{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001666 struct cardstate *cs = urb->context;
1667 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001668 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001669
Tilman Schmidt73a88812006-04-22 02:35:30 -07001670 update_basstate(ucs, 0, BS_ATWRPEND);
1671
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001672 /* check status */
1673 switch (urb->status) {
1674 case 0: /* normal completion */
1675 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001676 case -ENOENT: /* cancelled */
1677 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001678 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001679 case -ENODEV: /* device removed */
1680 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001681 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001682 gig_dbg(DEBUG_USBREQ, "%s: %s",
1683 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001684 return;
1685 default: /* any failure */
1686 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001687 dev_warn(cs->dev,
1688 "command write: %s, "
1689 "giving up after %d retries\n",
1690 get_usb_statmsg(urb->status),
1691 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001692 break;
1693 }
1694 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001695 dev_warn(cs->dev,
1696 "command write: %s, "
1697 "cannot retry - cmdbuf gone\n",
1698 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001699 break;
1700 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001701 dev_notice(cs->dev, "command write: %s, retry %d\n",
1702 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001703 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1704 /* resubmitted - bypass regular exit block */
1705 return;
1706 /* command send failed, assume base still waiting */
1707 update_basstate(ucs, BS_ATREADY, 0);
1708 }
1709
1710 spin_lock_irqsave(&cs->cmdlock, flags);
1711 if (cs->cmdbuf != NULL)
1712 complete_cb(cs);
1713 spin_unlock_irqrestore(&cs->cmdlock, flags);
1714}
1715
1716/* atrdy_timeout
1717 * timeout routine for AT command transmission
1718 * argument:
1719 * controller state structure
1720 */
1721static void atrdy_timeout(unsigned long data)
1722{
1723 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001724 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001725
Tilman Schmidt784d5852006-04-10 22:55:04 -07001726 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001727
1728 /* fake the missing signal - what else can I do? */
1729 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1730 start_cbsend(cs);
1731}
1732
1733/* atwrite_submit
1734 * submit an HD_WRITE_ATMESSAGE command URB
1735 * parameters:
1736 * cs controller state structure
1737 * buf buffer containing command to send
1738 * len length of command to send
1739 * return value:
1740 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001741 * -EBUSY if another request is pending
1742 * any URB submission error code
1743 */
1744static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1745{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001746 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001747 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001748
Tilman Schmidt784d5852006-04-10 22:55:04 -07001749 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001750
Tilman Schmidt73a88812006-04-22 02:35:30 -07001751 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001752 dev_err(cs->dev,
1753 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001754 return -EBUSY;
1755 }
1756
1757 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1758 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1759 ucs->dr_cmd_out.wValue = 0;
1760 ucs->dr_cmd_out.wIndex = 0;
1761 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1762 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1763 usb_sndctrlpipe(ucs->udev, 0),
1764 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1765 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001766 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001767 if (unlikely(rc)) {
1768 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001769 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001770 get_usb_rcmsg(rc));
1771 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001772 }
1773
Tilman Schmidt73a88812006-04-22 02:35:30 -07001774 /* submitted successfully, start timeout if necessary */
1775 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001776 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1777 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001778 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1779 ucs->timer_atrdy.data = (unsigned long) cs;
1780 ucs->timer_atrdy.function = atrdy_timeout;
1781 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001782 }
1783 return 0;
1784}
1785
1786/* start_cbsend
1787 * start transmission of AT command queue if necessary
1788 * parameter:
1789 * cs controller state structure
1790 * return value:
1791 * 0 on success
1792 * error code < 0 on error
1793 */
1794static int start_cbsend(struct cardstate *cs)
1795{
1796 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001797 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001798 unsigned long flags;
1799 int rc;
1800 int retval = 0;
1801
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001802 /* check if AT channel is open */
1803 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001804 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001805 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1806 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001807 /* flush command queue */
1808 spin_lock_irqsave(&cs->cmdlock, flags);
1809 while (cs->cmdbuf != NULL)
1810 complete_cb(cs);
1811 spin_unlock_irqrestore(&cs->cmdlock, flags);
1812 }
1813 return rc;
1814 }
1815
1816 /* try to send first command in queue */
1817 spin_lock_irqsave(&cs->cmdlock, flags);
1818
1819 while ((cb = cs->cmdbuf) != NULL &&
1820 atomic_read(&ucs->basstate) & BS_ATREADY) {
1821 ucs->retry_cmd_out = 0;
1822 rc = atwrite_submit(cs, cb->buf, cb->len);
1823 if (unlikely(rc)) {
1824 retval = rc;
1825 complete_cb(cs);
1826 }
1827 }
1828
1829 spin_unlock_irqrestore(&cs->cmdlock, flags);
1830 return retval;
1831}
1832
1833/* gigaset_write_cmd
1834 * This function is called by the device independent part of the driver
1835 * to transmit an AT command string to the Gigaset device.
1836 * It encapsulates the device specific method for transmission over the
1837 * direct USB connection to the base.
1838 * The command string is added to the queue of commands to send, and
1839 * USB transmission is started if necessary.
1840 * parameters:
1841 * cs controller state structure
1842 * buf command string to send
1843 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001844 * wake_tasklet tasklet to run when transmission is completed
1845 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001846 * return value:
1847 * number of bytes queued on success
1848 * error code < 0 on error
1849 */
1850static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001851 const unsigned char *buf, int len,
1852 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001853{
1854 struct cmdbuf_t *cb;
1855 unsigned long flags;
1856 int status;
1857
1858 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001859 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001860 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001861
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001862 if (len <= 0)
1863 return 0; /* nothing to do */
1864
1865 if (len > IF_WRITEBUF)
1866 len = IF_WRITEBUF;
1867 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001868 dev_err(cs->dev, "%s: out of memory\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001869 return -ENOMEM;
1870 }
1871
1872 memcpy(cb->buf, buf, len);
1873 cb->len = len;
1874 cb->offset = 0;
1875 cb->next = NULL;
1876 cb->wake_tasklet = wake_tasklet;
1877
1878 spin_lock_irqsave(&cs->cmdlock, flags);
1879 cb->prev = cs->lastcmdbuf;
1880 if (cs->lastcmdbuf)
1881 cs->lastcmdbuf->next = cb;
1882 else {
1883 cs->cmdbuf = cb;
1884 cs->curlen = len;
1885 }
1886 cs->cmdbytes += len;
1887 cs->lastcmdbuf = cb;
1888 spin_unlock_irqrestore(&cs->cmdlock, flags);
1889
Tilman Schmidt73a88812006-04-22 02:35:30 -07001890 spin_lock_irqsave(&cs->lock, flags);
1891 if (unlikely(!cs->connected)) {
1892 spin_unlock_irqrestore(&cs->lock, flags);
1893 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1894 return -ENODEV;
1895 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001896 status = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001897 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001898 return status < 0 ? status : len;
1899}
1900
1901/* gigaset_write_room
1902 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001903 * return number of characters the driver will accept to be written via
1904 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001905 * parameter:
1906 * controller state structure
1907 * return value:
1908 * number of characters
1909 */
1910static int gigaset_write_room(struct cardstate *cs)
1911{
1912 return IF_WRITEBUF;
1913}
1914
1915/* gigaset_chars_in_buffer
1916 * tty_driver.chars_in_buffer interface routine
1917 * return number of characters waiting to be sent
1918 * parameter:
1919 * controller state structure
1920 * return value:
1921 * number of characters
1922 */
1923static int gigaset_chars_in_buffer(struct cardstate *cs)
1924{
1925 unsigned long flags;
1926 unsigned bytes;
1927
1928 spin_lock_irqsave(&cs->cmdlock, flags);
1929 bytes = cs->cmdbytes;
1930 spin_unlock_irqrestore(&cs->cmdlock, flags);
1931
1932 return bytes;
1933}
1934
1935/* gigaset_brkchars
1936 * implementation of ioctl(GIGASET_BRKCHARS)
1937 * parameter:
1938 * controller state structure
1939 * return value:
1940 * -EINVAL (unimplemented function)
1941 */
1942static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1943{
1944 return -EINVAL;
1945}
1946
1947
1948/* Device Initialization/Shutdown */
1949/* ============================== */
1950
1951/* Free hardware dependent part of the B channel structure
1952 * parameter:
1953 * bcs B channel structure
1954 * return value:
1955 * !=0 on success
1956 */
1957static int gigaset_freebcshw(struct bc_state *bcs)
1958{
Tilman Schmidt73a88812006-04-22 02:35:30 -07001959 struct bas_bc_state *ubc = bcs->hw.bas;
1960 int i;
1961
1962 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001963 return 0;
1964
Tilman Schmidt73a88812006-04-22 02:35:30 -07001965 /* kill URBs and tasklets before freeing - better safe than sorry */
1966 atomic_set(&ubc->running, 0);
1967 for (i = 0; i < BAS_OUTURBS; ++i)
1968 if (ubc->isoouturbs[i].urb) {
1969 gig_dbg(DEBUG_INIT, "%s: killing iso out URB %d",
1970 __func__, i);
1971 usb_kill_urb(ubc->isoouturbs[i].urb);
1972 usb_free_urb(ubc->isoouturbs[i].urb);
1973 }
1974 for (i = 0; i < BAS_INURBS; ++i)
1975 if (ubc->isoinurbs[i]) {
1976 gig_dbg(DEBUG_INIT, "%s: killing iso in URB %d",
1977 __func__, i);
1978 usb_kill_urb(ubc->isoinurbs[i]);
1979 usb_free_urb(ubc->isoinurbs[i]);
1980 }
1981 tasklet_kill(&ubc->sent_tasklet);
1982 tasklet_kill(&ubc->rcvd_tasklet);
1983 kfree(ubc->isooutbuf);
1984 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001985 bcs->hw.bas = NULL;
1986 return 1;
1987}
1988
1989/* Initialize hardware dependent part of the B channel structure
1990 * parameter:
1991 * bcs B channel structure
1992 * return value:
1993 * !=0 on success
1994 */
1995static int gigaset_initbcshw(struct bc_state *bcs)
1996{
1997 int i;
1998 struct bas_bc_state *ubc;
1999
2000 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2001 if (!ubc) {
2002 err("could not allocate bas_bc_state");
2003 return 0;
2004 }
2005
2006 atomic_set(&ubc->running, 0);
2007 atomic_set(&ubc->corrbytes, 0);
2008 spin_lock_init(&ubc->isooutlock);
2009 for (i = 0; i < BAS_OUTURBS; ++i) {
2010 ubc->isoouturbs[i].urb = NULL;
2011 ubc->isoouturbs[i].bcs = bcs;
2012 }
2013 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2014 ubc->numsub = 0;
2015 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2016 err("could not allocate isochronous output buffer");
2017 kfree(ubc);
2018 bcs->hw.bas = NULL;
2019 return 0;
2020 }
2021 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002022 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002023
2024 spin_lock_init(&ubc->isoinlock);
2025 for (i = 0; i < BAS_INURBS; ++i)
2026 ubc->isoinurbs[i] = NULL;
2027 ubc->isoindone = NULL;
2028 ubc->loststatus = -EINPROGRESS;
2029 ubc->isoinlost = 0;
2030 ubc->seqlen = 0;
2031 ubc->inbyte = 0;
2032 ubc->inbits = 0;
2033 ubc->goodbytes = 0;
2034 ubc->alignerrs = 0;
2035 ubc->fcserrs = 0;
2036 ubc->frameerrs = 0;
2037 ubc->giants = 0;
2038 ubc->runts = 0;
2039 ubc->aborts = 0;
2040 ubc->shared0s = 0;
2041 ubc->stolen0s = 0;
2042 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002043 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002044 return 1;
2045}
2046
2047static void gigaset_reinitbcshw(struct bc_state *bcs)
2048{
2049 struct bas_bc_state *ubc = bcs->hw.bas;
2050
2051 atomic_set(&bcs->hw.bas->running, 0);
2052 atomic_set(&bcs->hw.bas->corrbytes, 0);
2053 bcs->hw.bas->numsub = 0;
2054 spin_lock_init(&ubc->isooutlock);
2055 spin_lock_init(&ubc->isoinlock);
2056 ubc->loststatus = -EINPROGRESS;
2057}
2058
2059static void gigaset_freecshw(struct cardstate *cs)
2060{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002061 /* timers, URBs and rcvbuf are disposed of in disconnect */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002062 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002063 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002064}
2065
2066static int gigaset_initcshw(struct cardstate *cs)
2067{
2068 struct bas_cardstate *ucs;
2069
2070 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2071 if (!ucs)
2072 return 0;
2073
2074 ucs->urb_cmd_in = NULL;
2075 ucs->urb_cmd_out = NULL;
2076 ucs->rcvbuf = NULL;
2077 ucs->rcvbuf_size = 0;
2078
2079 spin_lock_init(&ucs->lock);
2080 ucs->pending = 0;
2081
2082 atomic_set(&ucs->basstate, 0);
2083 init_timer(&ucs->timer_ctrl);
2084 init_timer(&ucs->timer_atrdy);
2085 init_timer(&ucs->timer_cmd_in);
2086
2087 return 1;
2088}
2089
2090/* freeurbs
2091 * unlink and deallocate all URBs unconditionally
2092 * caller must make sure that no commands are still in progress
2093 * parameter:
2094 * cs controller state structure
2095 */
2096static void freeurbs(struct cardstate *cs)
2097{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002098 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002099 struct bas_bc_state *ubc;
2100 int i, j;
2101
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002102 for (j = 0; j < 2; ++j) {
2103 ubc = cs->bcs[j].hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002104 for (i = 0; i < BAS_OUTURBS; ++i)
2105 if (ubc->isoouturbs[i].urb) {
2106 usb_kill_urb(ubc->isoouturbs[i].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002107 gig_dbg(DEBUG_INIT,
2108 "%s: isoc output URB %d/%d unlinked",
2109 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002110 usb_free_urb(ubc->isoouturbs[i].urb);
2111 ubc->isoouturbs[i].urb = NULL;
2112 }
2113 for (i = 0; i < BAS_INURBS; ++i)
2114 if (ubc->isoinurbs[i]) {
2115 usb_kill_urb(ubc->isoinurbs[i]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002116 gig_dbg(DEBUG_INIT,
2117 "%s: isoc input URB %d/%d unlinked",
2118 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002119 usb_free_urb(ubc->isoinurbs[i]);
2120 ubc->isoinurbs[i] = NULL;
2121 }
2122 }
2123 if (ucs->urb_int_in) {
2124 usb_kill_urb(ucs->urb_int_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002125 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2126 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002127 usb_free_urb(ucs->urb_int_in);
2128 ucs->urb_int_in = NULL;
2129 }
2130 if (ucs->urb_cmd_out) {
2131 usb_kill_urb(ucs->urb_cmd_out);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002132 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2133 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002134 usb_free_urb(ucs->urb_cmd_out);
2135 ucs->urb_cmd_out = NULL;
2136 }
2137 if (ucs->urb_cmd_in) {
2138 usb_kill_urb(ucs->urb_cmd_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002139 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2140 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002141 usb_free_urb(ucs->urb_cmd_in);
2142 ucs->urb_cmd_in = NULL;
2143 }
2144 if (ucs->urb_ctrl) {
2145 usb_kill_urb(ucs->urb_ctrl);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002146 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2147 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002148 usb_free_urb(ucs->urb_ctrl);
2149 ucs->urb_ctrl = NULL;
2150 }
2151}
2152
2153/* gigaset_probe
2154 * This function is called when a new USB device is connected.
2155 * It checks whether the new device is handled by this driver.
2156 */
2157static int gigaset_probe(struct usb_interface *interface,
2158 const struct usb_device_id *id)
2159{
2160 struct usb_host_interface *hostif;
2161 struct usb_device *udev = interface_to_usbdev(interface);
2162 struct cardstate *cs = NULL;
2163 struct bas_cardstate *ucs = NULL;
2164 struct bas_bc_state *ubc;
2165 struct usb_endpoint_descriptor *endpoint;
2166 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002167 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002168
Tilman Schmidt784d5852006-04-10 22:55:04 -07002169 gig_dbg(DEBUG_ANY,
2170 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2171 __func__, le16_to_cpu(udev->descriptor.idVendor),
2172 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002173
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002174 /* set required alternate setting */
2175 hostif = interface->cur_altsetting;
2176 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002177 gig_dbg(DEBUG_ANY,
2178 "%s: wrong alternate setting %d - trying to switch",
2179 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002180 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002181 dev_warn(&udev->dev, "usb_set_interface failed, "
2182 "device %d interface %d altsetting %d\n",
2183 udev->devnum, hostif->desc.bInterfaceNumber,
2184 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002185 return -ENODEV;
2186 }
2187 hostif = interface->cur_altsetting;
2188 }
2189
2190 /* Reject application specific interfaces
2191 */
2192 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002193 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2194 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002195 return -ENODEV;
2196 }
2197
Tilman Schmidt784d5852006-04-10 22:55:04 -07002198 dev_info(&udev->dev,
2199 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2200 __func__, le16_to_cpu(udev->descriptor.idVendor),
2201 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002202
2203 cs = gigaset_getunassignedcs(driver);
2204 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002205 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002206 return -ENODEV;
2207 }
2208 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002209
2210 /* save off device structure ptrs for later use */
2211 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002212 ucs->udev = udev;
2213 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002214 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002215
2216 /* allocate URBs:
2217 * - one for the interrupt pipe
2218 * - three for the different uses of the default control pipe
2219 * - three for each isochronous pipe
2220 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002221 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2222 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2223 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2224 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002225 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002226
2227 for (j = 0; j < 2; ++j) {
2228 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002229 for (i = 0; i < BAS_OUTURBS; ++i)
2230 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002231 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002232 goto allocerr;
2233 for (i = 0; i < BAS_INURBS; ++i)
2234 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002235 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002236 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002237 }
2238
2239 ucs->rcvbuf = NULL;
2240 ucs->rcvbuf_size = 0;
2241
2242 /* Fill the interrupt urb and send it to the core */
2243 endpoint = &hostif->endpoint[0].desc;
2244 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002245 usb_rcvintpipe(udev,
2246 (endpoint->bEndpointAddress) & 0x0f),
2247 ucs->int_in_buf, 3, read_int_callback, cs,
2248 endpoint->bInterval);
Christoph Lametere94b1762006-12-06 20:33:17 -08002249 if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002250 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002251 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002252 goto error;
2253 }
2254
2255 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002256 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002257 goto error;
2258
2259 /* tell common part that the device is ready */
2260 if (startmode == SM_LOCKED)
2261 atomic_set(&cs->mstate, MS_LOCKED);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002262
2263 /* save address of controller structure */
2264 usb_set_intfdata(interface, cs);
2265
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002266 if (!gigaset_start(cs))
2267 goto error;
2268
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002269 return 0;
2270
Tilman Schmidt73a88812006-04-22 02:35:30 -07002271allocerr:
2272 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002273error:
2274 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002275 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002276 gigaset_unassign(cs);
2277 return -ENODEV;
2278}
2279
2280/* gigaset_disconnect
2281 * This function is called when the Gigaset base is unplugged.
2282 */
2283static void gigaset_disconnect(struct usb_interface *interface)
2284{
2285 struct cardstate *cs;
2286 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002287 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002288
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002289 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002290
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002291 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002292
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002293 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002294
2295 /* mark base as not ready, all channels disconnected */
2296 atomic_set(&ucs->basstate, 0);
2297
2298 /* tell LL all channels are down */
2299 //FIXME shouldn't gigaset_stop() do this?
2300 for (j = 0; j < 2; ++j)
2301 gigaset_bchannel_down(cs->bcs + j);
2302
2303 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002304 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002305
2306 /* stop timers and URBs, free ressources */
2307 del_timer_sync(&ucs->timer_ctrl);
2308 del_timer_sync(&ucs->timer_atrdy);
2309 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002310 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002311 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002312 kfree(ucs->rcvbuf);
2313 ucs->rcvbuf = NULL;
2314 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002315 usb_put_dev(ucs->udev);
2316 ucs->interface = NULL;
2317 ucs->udev = NULL;
2318 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002319 gigaset_unassign(cs);
2320}
2321
2322static struct gigaset_ops gigops = {
2323 gigaset_write_cmd,
2324 gigaset_write_room,
2325 gigaset_chars_in_buffer,
2326 gigaset_brkchars,
2327 gigaset_init_bchannel,
2328 gigaset_close_bchannel,
2329 gigaset_initbcshw,
2330 gigaset_freebcshw,
2331 gigaset_reinitbcshw,
2332 gigaset_initcshw,
2333 gigaset_freecshw,
2334 gigaset_set_modem_ctrl,
2335 gigaset_baud_rate,
2336 gigaset_set_line_ctrl,
2337 gigaset_isoc_send_skb,
2338 gigaset_isoc_input,
2339};
2340
2341/* bas_gigaset_init
2342 * This function is called after the kernel module is loaded.
2343 */
2344static int __init bas_gigaset_init(void)
2345{
2346 int result;
2347
2348 /* allocate memory for our driver state and intialize it */
2349 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002350 GIGASET_MODULENAME, GIGASET_DEVNAME,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07002351 &gigops, THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002352 goto error;
2353
2354 /* allocate memory for our device state and intialize it */
Tilman Schmidt917f5082006-04-10 22:55:00 -07002355 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2356 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002357 if (!cardstate)
2358 goto error;
2359
2360 /* register this driver with the USB subsystem */
2361 result = usb_register(&gigaset_usb_driver);
2362 if (result < 0) {
2363 err("usb_register failed (error %d)", -result);
2364 goto error;
2365 }
2366
2367 info(DRIVER_AUTHOR);
2368 info(DRIVER_DESC);
2369 return 0;
2370
2371error: if (cardstate)
2372 gigaset_freecs(cardstate);
2373 cardstate = NULL;
2374 if (driver)
2375 gigaset_freedriver(driver);
2376 driver = NULL;
2377 return -1;
2378}
2379
2380/* bas_gigaset_exit
2381 * This function is called before the kernel module is unloaded.
2382 */
2383static void __exit bas_gigaset_exit(void)
2384{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002385 struct bas_cardstate *ucs = cardstate->hw.bas;
2386
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002387 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002388 * => no gigaset_start any more
2389 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002390
2391 gigaset_shutdown(cardstate);
2392 /* from now on, no isdn callback should be possible */
2393
Tilman Schmidt73a88812006-04-22 02:35:30 -07002394 /* close all still open channels */
2395 if (atomic_read(&ucs->basstate) & BS_B1OPEN) {
2396 gig_dbg(DEBUG_INIT, "closing B1 channel");
2397 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2398 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ, 0, 0,
2399 NULL, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002400 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002401 if (atomic_read(&ucs->basstate) & BS_B2OPEN) {
2402 gig_dbg(DEBUG_INIT, "closing B2 channel");
2403 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2404 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ, 0, 0,
2405 NULL, 0, BAS_TIMEOUT);
2406 }
2407 if (atomic_read(&ucs->basstate) & BS_ATOPEN) {
2408 gig_dbg(DEBUG_INIT, "closing AT channel");
2409 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2410 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ, 0, 0,
2411 NULL, 0, BAS_TIMEOUT);
2412 }
2413 atomic_set(&ucs->basstate, 0);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002414
2415 /* deregister this driver with the USB subsystem */
2416 usb_deregister(&gigaset_usb_driver);
2417 /* this will call the disconnect-callback */
2418 /* from now on, no disconnect/probe callback should be running */
2419
2420 gigaset_freecs(cardstate);
2421 cardstate = NULL;
2422 gigaset_freedriver(driver);
2423 driver = NULL;
2424}
2425
2426
2427module_init(bas_gigaset_init);
2428module_exit(bas_gigaset_exit);
2429
2430MODULE_AUTHOR(DRIVER_AUTHOR);
2431MODULE_DESCRIPTION(DRIVER_DESC);
2432MODULE_LICENSE("GPL");