blob: 8a45715dd4c10393e6ea9314df12c2abee67ccf4 [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"
44#define GIGASET_DEVFSNAME "gig/bas/"
45#define GIGASET_DEVNAME "ttyGB"
46
Tilman Schmidt73a88812006-04-22 02:35:30 -070047/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
48#define IF_WRITEBUF 264
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080049
50/* Values for the Gigaset 307x */
51#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070052#define USB_3070_PRODUCT_ID 0x0001
53#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080054#define USB_SX303_PRODUCT_ID 0x0021
55#define USB_SX353_PRODUCT_ID 0x0022
56
57/* table of devices that work with this driver */
58static struct usb_device_id gigaset_table [] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070059 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
60 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080061 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
63 { } /* Terminating entry */
64};
65
66MODULE_DEVICE_TABLE(usb, gigaset_table);
67
Tilman Schmidt06163f82006-06-26 00:25:33 -070068/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080069
Tilman Schmidt06163f82006-06-26 00:25:33 -070070/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080071static int gigaset_probe(struct usb_interface *interface,
72 const struct usb_device_id *id);
73
74/* Function will be called if the device is unplugged */
75static void gigaset_disconnect(struct usb_interface *interface);
76
Tilman Schmidt06163f82006-06-26 00:25:33 -070077static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070078static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070079static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070080static int atwrite_submit(struct cardstate *, unsigned char *, int);
81static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080082
Tilman Schmidt06163f82006-06-26 00:25:33 -070083/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080084
85struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070086 struct usb_device *udev; /* USB device pointer */
87 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080088 unsigned char minor; /* starting minor number */
89
Tilman Schmidt784d5852006-04-10 22:55:04 -070090 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080091 struct usb_ctrlrequest dr_ctrl;
92 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -070093 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080094
95 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -070096 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080097 struct usb_ctrlrequest dr_cmd_out;
98 int retry_cmd_out;
99
Tilman Schmidt784d5852006-04-10 22:55:04 -0700100 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800101 struct usb_ctrlrequest dr_cmd_in;
102 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700103 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800104
Tilman Schmidt784d5852006-04-10 22:55:04 -0700105 struct urb *urb_int_in; /* URB for interrupt pipe */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800106 unsigned char int_in_buf[3];
107
108 spinlock_t lock; /* locks all following */
109 atomic_t basstate; /* bitmap (BS_*) */
110 int pending; /* uncompleted base request */
111 int rcvbuf_size; /* size of AT receive buffer */
112 /* 0: no receive in progress */
113 int retry_cmd_in; /* receive req retry count */
114};
115
116/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700117#define BS_ATOPEN 0x001 /* AT channel open */
118#define BS_B1OPEN 0x002 /* B channel 1 open */
119#define BS_B2OPEN 0x004 /* B channel 2 open */
120#define BS_ATREADY 0x008 /* base ready for AT command */
121#define BS_INIT 0x010 /* base has signalled INIT_OK */
122#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
123#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
124#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800125
126
127static struct gigaset_driver *driver = NULL;
128static struct cardstate *cardstate = NULL;
129
130/* usb specific object needed to register this driver with the usb subsystem */
131static struct usb_driver gigaset_usb_driver = {
132 .name = GIGASET_MODULENAME,
133 .probe = gigaset_probe,
134 .disconnect = gigaset_disconnect,
135 .id_table = gigaset_table,
136};
137
Tilman Schmidt73a88812006-04-22 02:35:30 -0700138/* get message text for usb_submit_urb return code
139 */
140static char *get_usb_rcmsg(int rc)
141{
142 static char unkmsg[28];
143
144 switch (rc) {
145 case 0:
146 return "success";
147 case -ENOMEM:
148 return "out of memory";
149 case -ENODEV:
150 return "device not present";
151 case -ENOENT:
152 return "endpoint not present";
153 case -ENXIO:
154 return "URB type not supported";
155 case -EINVAL:
156 return "invalid argument";
157 case -EAGAIN:
158 return "start frame too early or too much scheduled";
159 case -EFBIG:
160 return "too many isochronous frames requested";
161 case -EPIPE:
162 return "endpoint stalled";
163 case -EMSGSIZE:
164 return "invalid packet size";
165 case -ENOSPC:
166 return "would overcommit USB bandwidth";
167 case -ESHUTDOWN:
168 return "device shut down";
169 case -EPERM:
170 return "reject flag set";
171 case -EHOSTUNREACH:
172 return "device suspended";
173 default:
174 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
175 return unkmsg;
176 }
177}
178
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800179/* get message text for USB status code
180 */
181static char *get_usb_statmsg(int status)
182{
183 static char unkmsg[28];
184
185 switch (status) {
186 case 0:
187 return "success";
188 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700189 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800190 case -EINPROGRESS:
191 return "pending";
192 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700193 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800194 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700195 return "CRC mismatch, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800196 case -ETIMEDOUT:
197 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700198 case -EPIPE:
199 return "endpoint stalled";
200 case -ECOMM:
201 return "IN buffer overrun";
202 case -ENOSR:
203 return "OUT buffer underrun";
204 case -EOVERFLOW:
205 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800206 case -EREMOTEIO:
207 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700208 case -ENODEV:
209 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800210 case -EXDEV:
211 return "partial isochronous transfer";
212 case -EINVAL:
213 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700214 case -ECONNRESET:
215 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800216 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700217 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800218 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700219 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800220 return unkmsg;
221 }
222}
223
224/* usb_pipetype_str
225 * retrieve string representation of USB pipe type
226 */
227static inline char *usb_pipetype_str(int pipe)
228{
229 if (usb_pipeisoc(pipe))
230 return "Isoc";
231 if (usb_pipeint(pipe))
232 return "Int";
233 if (usb_pipecontrol(pipe))
234 return "Ctrl";
235 if (usb_pipebulk(pipe))
236 return "Bulk";
237 return "?";
238}
239
240/* dump_urb
241 * write content of URB to syslog for debugging
242 */
243static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700244 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800245{
246#ifdef CONFIG_GIGASET_DEBUG
247 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700248 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800249 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700250 gig_dbg(level,
251 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
252 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
253 (unsigned long) urb->dev,
254 usb_pipetype_str(urb->pipe),
255 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
256 usb_pipein(urb->pipe) ? "in" : "out",
257 urb->status, (unsigned long) urb->hcpriv,
258 urb->transfer_flags);
259 gig_dbg(level,
260 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
261 "bandwidth=%d, setup_packet=0x%08lx,",
262 (unsigned long) urb->transfer_buffer,
263 urb->transfer_buffer_length, urb->actual_length,
264 urb->bandwidth, (unsigned long) urb->setup_packet);
265 gig_dbg(level,
266 " start_frame=%d, number_of_packets=%d, interval=%d, "
267 "error_count=%d,",
268 urb->start_frame, urb->number_of_packets, urb->interval,
269 urb->error_count);
270 gig_dbg(level,
271 " context=0x%08lx, complete=0x%08lx, "
272 "iso_frame_desc[]={",
273 (unsigned long) urb->context,
274 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800275 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700276 struct usb_iso_packet_descriptor *pifd
277 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700278 gig_dbg(level,
279 " {offset=%u, length=%u, actual_length=%u, "
280 "status=%u}",
281 pifd->offset, pifd->length, pifd->actual_length,
282 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800283 }
284 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700285 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800286#endif
287}
288
289/* read/set modem control bits etc. (m10x only) */
290static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700291 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800292{
293 return -EINVAL;
294}
295
296static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
297{
298 return -EINVAL;
299}
300
301static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
302{
303 return -EINVAL;
304}
305
306/* error_hangup
307 * hang up any existing connection because of an unrecoverable error
308 * This function may be called from any context and takes care of scheduling
309 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700310 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800311 * argument:
312 * B channel control structure
313 */
314static inline void error_hangup(struct bc_state *bcs)
315{
316 struct cardstate *cs = bcs->cs;
317
Tilman Schmidt784d5852006-04-10 22:55:04 -0700318 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
319 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800320
Tilman Schmidt73a88812006-04-22 02:35:30 -0700321 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
322 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800323
324 gigaset_schedule_event(cs);
325}
326
327/* error_reset
328 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700329 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700330 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700331 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800332 * argument:
333 * controller state structure
334 */
335static inline void error_reset(struct cardstate *cs)
336{
Tilman Schmidt06163f82006-06-26 00:25:33 -0700337 /* close AT command channel to recover (ignore errors) */
338 req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
339
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800340 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700341 dev_err(cs->dev,
342 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800343}
344
345/* check_pending
346 * check for completion of pending control request
347 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700348 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800349 */
350static void check_pending(struct bas_cardstate *ucs)
351{
352 unsigned long flags;
353
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800354 spin_lock_irqsave(&ucs->lock, flags);
355 switch (ucs->pending) {
356 case 0:
357 break;
358 case HD_OPEN_ATCHANNEL:
359 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
360 ucs->pending = 0;
361 break;
362 case HD_OPEN_B1CHANNEL:
363 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
364 ucs->pending = 0;
365 break;
366 case HD_OPEN_B2CHANNEL:
367 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
368 ucs->pending = 0;
369 break;
370 case HD_CLOSE_ATCHANNEL:
371 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
372 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800373 break;
374 case HD_CLOSE_B1CHANNEL:
375 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
376 ucs->pending = 0;
377 break;
378 case HD_CLOSE_B2CHANNEL:
379 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
380 ucs->pending = 0;
381 break;
382 case HD_DEVICE_INIT_ACK: /* no reply expected */
383 ucs->pending = 0;
384 break;
385 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
386 * are handled separately and should never end up here
387 */
388 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700389 dev_warn(&ucs->interface->dev,
390 "unknown pending request 0x%02x cleared\n",
391 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800392 ucs->pending = 0;
393 }
394
395 if (!ucs->pending)
396 del_timer(&ucs->timer_ctrl);
397
398 spin_unlock_irqrestore(&ucs->lock, flags);
399}
400
401/* cmd_in_timeout
402 * timeout routine for command input request
403 * argument:
404 * controller state structure
405 */
406static void cmd_in_timeout(unsigned long data)
407{
408 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700409 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700410 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800411
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800412 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700413 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800414 return;
415 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800416
Tilman Schmidt06163f82006-06-26 00:25:33 -0700417 if (ucs->retry_cmd_in++ < BAS_RETRY) {
418 dev_notice(cs->dev, "control read: timeout, retry %d\n",
419 ucs->retry_cmd_in);
420 rc = atread_submit(cs, BAS_TIMEOUT);
421 if (rc >= 0 || rc == -ENODEV)
422 /* resubmitted or disconnected */
423 /* - bypass regular exit block */
424 return;
425 } else {
426 dev_err(cs->dev,
427 "control read: timeout, giving up after %d tries\n",
428 ucs->retry_cmd_in);
429 }
430 kfree(ucs->rcvbuf);
431 ucs->rcvbuf = NULL;
432 ucs->rcvbuf_size = 0;
433 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800434}
435
Tilman Schmidt73a88812006-04-22 02:35:30 -0700436/* set/clear bits in base connection state, return previous state
437 */
438inline static int update_basstate(struct bas_cardstate *ucs,
439 int set, int clear)
440{
441 unsigned long flags;
442 int state;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800443
Tilman Schmidt73a88812006-04-22 02:35:30 -0700444 spin_lock_irqsave(&ucs->lock, flags);
445 state = atomic_read(&ucs->basstate);
446 atomic_set(&ucs->basstate, (state & ~clear) | set);
447 spin_unlock_irqrestore(&ucs->lock, flags);
448 return state;
449}
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800450
Tilman Schmidt06163f82006-06-26 00:25:33 -0700451/* read_ctrl_callback
452 * USB completion handler for control pipe input
453 * called by the USB subsystem in interrupt context
454 * parameter:
455 * urb USB request block
456 * urb->context = inbuf structure for controller state
457 */
458static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
459{
460 struct inbuf_t *inbuf = urb->context;
461 struct cardstate *cs = inbuf->cs;
462 struct bas_cardstate *ucs = cs->hw.bas;
463 int have_data = 0;
464 unsigned numbytes;
465 int rc;
466
467 update_basstate(ucs, 0, BS_ATRDPEND);
468
469 if (!ucs->rcvbuf_size) {
470 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
471 return;
472 }
473
474 del_timer(&ucs->timer_cmd_in);
475
476 switch (urb->status) {
477 case 0: /* normal completion */
478 numbytes = urb->actual_length;
479 if (unlikely(numbytes != ucs->rcvbuf_size)) {
480 dev_warn(cs->dev,
481 "control read: received %d chars, expected %d\n",
482 numbytes, ucs->rcvbuf_size);
483 if (numbytes > ucs->rcvbuf_size)
484 numbytes = ucs->rcvbuf_size;
485 }
486
487 /* copy received bytes to inbuf */
488 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
489
490 if (unlikely(numbytes < ucs->rcvbuf_size)) {
491 /* incomplete - resubmit for remaining bytes */
492 ucs->rcvbuf_size -= numbytes;
493 ucs->retry_cmd_in = 0;
494 rc = atread_submit(cs, BAS_TIMEOUT);
495 if (rc >= 0 || rc == -ENODEV)
496 /* resubmitted or disconnected */
497 /* - bypass regular exit block */
498 return;
499 error_reset(cs);
500 }
501 break;
502
503 case -ENOENT: /* cancelled */
504 case -ECONNRESET: /* cancelled (async) */
505 case -EINPROGRESS: /* pending */
506 case -ENODEV: /* device removed */
507 case -ESHUTDOWN: /* device shut down */
508 /* no action necessary */
509 gig_dbg(DEBUG_USBREQ, "%s: %s",
510 __func__, get_usb_statmsg(urb->status));
511 break;
512
513 default: /* severe trouble */
514 dev_warn(cs->dev, "control read: %s\n",
515 get_usb_statmsg(urb->status));
516 if (ucs->retry_cmd_in++ < BAS_RETRY) {
517 dev_notice(cs->dev, "control read: retry %d\n",
518 ucs->retry_cmd_in);
519 rc = atread_submit(cs, BAS_TIMEOUT);
520 if (rc >= 0 || rc == -ENODEV)
521 /* resubmitted or disconnected */
522 /* - bypass regular exit block */
523 return;
524 } else {
525 dev_err(cs->dev,
526 "control read: giving up after %d tries\n",
527 ucs->retry_cmd_in);
528 }
529 error_reset(cs);
530 }
531
532 kfree(ucs->rcvbuf);
533 ucs->rcvbuf = NULL;
534 ucs->rcvbuf_size = 0;
535 if (have_data) {
536 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
537 gigaset_schedule_event(cs);
538 }
539}
540
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800541/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700542 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800543 * parameters:
544 * cs controller state structure
545 * timeout timeout in 1/10 sec., 0: none
546 * return value:
547 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800548 * -EBUSY if another request is pending
549 * any URB submission error code
550 */
551static int atread_submit(struct cardstate *cs, int timeout)
552{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700553 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800554 int ret;
555
Tilman Schmidt784d5852006-04-10 22:55:04 -0700556 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
557 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800558
Tilman Schmidt73a88812006-04-22 02:35:30 -0700559 if (update_basstate(ucs, BS_ATRDPEND, 0) & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700560 dev_err(cs->dev,
561 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800562 return -EBUSY;
563 }
564
565 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
566 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
567 ucs->dr_cmd_in.wValue = 0;
568 ucs->dr_cmd_in.wIndex = 0;
569 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
570 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700571 usb_rcvctrlpipe(ucs->udev, 0),
572 (unsigned char*) & ucs->dr_cmd_in,
573 ucs->rcvbuf, ucs->rcvbuf_size,
574 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800575
576 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700577 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700579 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800580 return ret;
581 }
582
583 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700584 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800585 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
586 ucs->timer_cmd_in.data = (unsigned long) cs;
587 ucs->timer_cmd_in.function = cmd_in_timeout;
588 add_timer(&ucs->timer_cmd_in);
589 }
590 return 0;
591}
592
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800593/* read_int_callback
594 * USB completion handler for interrupt pipe input
595 * called by the USB subsystem in interrupt context
596 * parameter:
597 * urb USB request block
598 * urb->context = controller state structure
599 */
600static void read_int_callback(struct urb *urb, struct pt_regs *regs)
601{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700602 struct cardstate *cs = urb->context;
603 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800604 struct bc_state *bcs;
605 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700606 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800607 unsigned l;
608 int channel;
609
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800610 switch (urb->status) {
611 case 0: /* success */
612 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700613 case -ENOENT: /* cancelled */
614 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800615 case -EINPROGRESS: /* pending */
616 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700617 gig_dbg(DEBUG_USBREQ, "%s: %s",
618 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800619 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700620 case -ENODEV: /* device removed */
621 case -ESHUTDOWN: /* device shut down */
622 //FIXME use this as disconnect indicator?
623 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
624 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800625 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700626 dev_warn(cs->dev, "interrupt read: %s\n",
627 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800628 //FIXME corrective action? resubmission always ok?
629 goto resubmit;
630 }
631
Tilman Schmidt73a88812006-04-22 02:35:30 -0700632 /* drop incomplete packets even if the missing bytes wouldn't matter */
633 if (unlikely(urb->actual_length < 3)) {
634 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
635 urb->actual_length);
636 goto resubmit;
637 }
638
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800639 l = (unsigned) ucs->int_in_buf[1] +
640 (((unsigned) ucs->int_in_buf[2]) << 8);
641
Tilman Schmidt784d5852006-04-10 22:55:04 -0700642 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
643 urb->actual_length, (int)ucs->int_in_buf[0], l,
644 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800645
646 channel = 0;
647
648 switch (ucs->int_in_buf[0]) {
649 case HD_DEVICE_INIT_OK:
650 update_basstate(ucs, BS_INIT, 0);
651 break;
652
653 case HD_READY_SEND_ATDATA:
654 del_timer(&ucs->timer_atrdy);
655 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
656 start_cbsend(cs);
657 break;
658
659 case HD_OPEN_B2CHANNEL_ACK:
660 ++channel;
661 case HD_OPEN_B1CHANNEL_ACK:
662 bcs = cs->bcs + channel;
663 update_basstate(ucs, BS_B1OPEN << channel, 0);
664 gigaset_bchannel_up(bcs);
665 break;
666
667 case HD_OPEN_ATCHANNEL_ACK:
668 update_basstate(ucs, BS_ATOPEN, 0);
669 start_cbsend(cs);
670 break;
671
672 case HD_CLOSE_B2CHANNEL_ACK:
673 ++channel;
674 case HD_CLOSE_B1CHANNEL_ACK:
675 bcs = cs->bcs + channel;
676 update_basstate(ucs, 0, BS_B1OPEN << channel);
677 stopurbs(bcs->hw.bas);
678 gigaset_bchannel_down(bcs);
679 break;
680
681 case HD_CLOSE_ATCHANNEL_ACK:
682 update_basstate(ucs, 0, BS_ATOPEN);
683 break;
684
685 case HD_B2_FLOW_CONTROL:
686 ++channel;
687 case HD_B1_FLOW_CONTROL:
688 bcs = cs->bcs + channel;
689 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700690 &bcs->hw.bas->corrbytes);
691 gig_dbg(DEBUG_ISO,
692 "Flow control (channel %d, sub %d): 0x%02x => %d",
693 channel, bcs->hw.bas->numsub, l,
694 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800695 break;
696
697 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
698 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700699 dev_warn(cs->dev,
700 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800701 break;
702 }
703 spin_lock_irqsave(&cs->lock, flags);
704 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700705 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700706 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700707 "receive AT data overrun, %d bytes lost\n",
708 ucs->rcvbuf_size);
709 kfree(ucs->rcvbuf);
710 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800711 }
712 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
713 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700714 dev_err(cs->dev, "out of memory receiving AT data\n");
715 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800716 break;
717 }
718 ucs->rcvbuf_size = l;
719 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700720 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800721 kfree(ucs->rcvbuf);
722 ucs->rcvbuf = NULL;
723 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700724 if (rc != -ENODEV) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700725 //FIXME corrective action?
Tilman Schmidt06163f82006-06-26 00:25:33 -0700726 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700727 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700728 break;
729 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800730 }
731 spin_unlock_irqrestore(&cs->lock, flags);
732 break;
733
734 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700735 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800736 break;
737
738 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700739 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800740 break;
741
742 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700743 dev_warn(cs->dev,
744 "unknown Gigaset signal 0x%02x (%u) ignored\n",
745 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800746 }
747
748 check_pending(ucs);
749
750resubmit:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700751 rc = usb_submit_urb(urb, SLAB_ATOMIC);
752 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700753 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700754 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800755 error_reset(cs);
756 }
757}
758
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800759/* read_iso_callback
760 * USB completion handler for B channel isochronous input
761 * called by the USB subsystem in interrupt context
762 * parameter:
763 * urb USB request block of completed request
764 * urb->context = bc_state structure
765 */
766static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
767{
768 struct bc_state *bcs;
769 struct bas_bc_state *ubc;
770 unsigned long flags;
771 int i, rc;
772
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800773 /* status codes not worth bothering the tasklet with */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700774 if (unlikely(urb->status == -ENOENT ||
775 urb->status == -ECONNRESET ||
776 urb->status == -EINPROGRESS ||
777 urb->status == -ENODEV ||
778 urb->status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700779 gig_dbg(DEBUG_ISO, "%s: %s",
780 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800781 return;
782 }
783
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700784 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800785 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800786
787 spin_lock_irqsave(&ubc->isoinlock, flags);
788 if (likely(ubc->isoindone == NULL)) {
789 /* pass URB to tasklet */
790 ubc->isoindone = urb;
791 tasklet_schedule(&ubc->rcvd_tasklet);
792 } else {
793 /* tasklet still busy, drop data and resubmit URB */
794 ubc->loststatus = urb->status;
795 for (i = 0; i < BAS_NUMFRAMES; i++) {
796 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
797 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700798 urb->iso_frame_desc[i].status !=
799 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800800 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800801 urb->iso_frame_desc[i].status = 0;
802 urb->iso_frame_desc[i].actual_length = 0;
803 }
804 if (likely(atomic_read(&ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700805 /* urb->dev is clobbered by USB subsystem */
806 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800807 urb->transfer_flags = URB_ISO_ASAP;
808 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700809 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
810 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800811 rc = usb_submit_urb(urb, SLAB_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700812 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700813 dev_err(bcs->cs->dev,
814 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700815 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800816 dump_urb(DEBUG_ISO, "isoc read", urb);
817 error_hangup(bcs);
818 }
819 }
820 }
821 spin_unlock_irqrestore(&ubc->isoinlock, flags);
822}
823
824/* write_iso_callback
825 * USB completion handler for B channel isochronous output
826 * called by the USB subsystem in interrupt context
827 * parameter:
828 * urb USB request block of completed request
829 * urb->context = isow_urbctx_t structure
830 */
831static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
832{
833 struct isow_urbctx_t *ucx;
834 struct bas_bc_state *ubc;
835 unsigned long flags;
836
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800837 /* status codes not worth bothering the tasklet with */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700838 if (unlikely(urb->status == -ENOENT ||
839 urb->status == -ECONNRESET ||
840 urb->status == -EINPROGRESS ||
841 urb->status == -ENODEV ||
842 urb->status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700843 gig_dbg(DEBUG_ISO, "%s: %s",
844 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800845 return;
846 }
847
848 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700849 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800850 ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800851
852 spin_lock_irqsave(&ubc->isooutlock, flags);
853 ubc->isooutovfl = ubc->isooutdone;
854 ubc->isooutdone = ucx;
855 spin_unlock_irqrestore(&ubc->isooutlock, flags);
856 tasklet_schedule(&ubc->sent_tasklet);
857}
858
859/* starturbs
860 * prepare and submit USB request blocks for isochronous input and output
861 * argument:
862 * B channel control structure
863 * return value:
864 * 0 on success
865 * < 0 on error (no URBs submitted)
866 */
867static int starturbs(struct bc_state *bcs)
868{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700869 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800870 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800871 int j, k;
872 int rc;
873
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800874 /* initialize L2 reception */
875 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
876 bcs->inputstate |= INS_flag_hunt;
877
878 /* submit all isochronous input URBs */
879 atomic_set(&ubc->running, 1);
880 for (k = 0; k < BAS_INURBS; k++) {
881 urb = ubc->isoinurbs[k];
882 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800883 rc = -EFAULT;
884 goto error;
885 }
886
887 urb->dev = bcs->cs->hw.bas->udev;
888 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
889 urb->transfer_flags = URB_ISO_ASAP;
890 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
891 urb->transfer_buffer_length = BAS_INBUFSIZE;
892 urb->number_of_packets = BAS_NUMFRAMES;
893 urb->interval = BAS_FRAMETIME;
894 urb->complete = read_iso_callback;
895 urb->context = bcs;
896 for (j = 0; j < BAS_NUMFRAMES; j++) {
897 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
898 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
899 urb->iso_frame_desc[j].status = 0;
900 urb->iso_frame_desc[j].actual_length = 0;
901 }
902
903 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700904 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800905 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800906 }
907
908 /* initialize L2 transmission */
909 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
910
911 /* set up isochronous output URBs for flag idling */
912 for (k = 0; k < BAS_OUTURBS; ++k) {
913 urb = ubc->isoouturbs[k].urb;
914 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800915 rc = -EFAULT;
916 goto error;
917 }
918 urb->dev = bcs->cs->hw.bas->udev;
919 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
920 urb->transfer_flags = URB_ISO_ASAP;
921 urb->transfer_buffer = ubc->isooutbuf->data;
922 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
923 urb->number_of_packets = BAS_NUMFRAMES;
924 urb->interval = BAS_FRAMETIME;
925 urb->complete = write_iso_callback;
926 urb->context = &ubc->isoouturbs[k];
927 for (j = 0; j < BAS_NUMFRAMES; ++j) {
928 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
929 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
930 urb->iso_frame_desc[j].status = 0;
931 urb->iso_frame_desc[j].actual_length = 0;
932 }
933 ubc->isoouturbs[k].limit = -1;
934 }
935
936 /* submit two URBs, keep third one */
937 for (k = 0; k < 2; ++k) {
938 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
939 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700940 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800941 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800942 }
943 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
944 ubc->isooutfree = &ubc->isoouturbs[2];
945 ubc->isooutdone = ubc->isooutovfl = NULL;
946 return 0;
947 error:
948 stopurbs(ubc);
949 return rc;
950}
951
952/* stopurbs
953 * cancel the USB request blocks for isochronous input and output
954 * errors are silently ignored
955 * argument:
956 * B channel control structure
957 */
958static void stopurbs(struct bas_bc_state *ubc)
959{
960 int k, rc;
961
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800962 atomic_set(&ubc->running, 0);
963
964 for (k = 0; k < BAS_INURBS; ++k) {
965 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700966 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700967 "%s: isoc input URB %d unlinked, result = %s",
968 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800969 }
970
971 for (k = 0; k < BAS_OUTURBS; ++k) {
972 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700973 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700974 "%s: isoc output URB %d unlinked, result = %s",
975 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800976 }
977}
978
979/* Isochronous Write - Bottom Half */
980/* =============================== */
981
982/* submit_iso_write_urb
983 * fill and submit the next isochronous write URB
984 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700985 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800986 * return value:
987 * number of frames submitted in URB
988 * 0 if URB not submitted because no data available (isooutbuf busy)
989 * error code < 0 on error
990 */
991static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
992{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700993 struct urb *urb = ucx->urb;
994 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800995 struct usb_iso_packet_descriptor *ifd;
996 int corrbytes, nframe, rc;
997
Tilman Schmidt784d5852006-04-10 22:55:04 -0700998 /* urb->dev is clobbered by USB subsystem */
999 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001000 urb->transfer_flags = URB_ISO_ASAP;
1001 urb->transfer_buffer = ubc->isooutbuf->data;
1002 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1003
1004 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1005 ifd = &urb->iso_frame_desc[nframe];
1006
1007 /* compute frame length according to flow control */
1008 ifd->length = BAS_NORMFRAME;
1009 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001010 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1011 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001012 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1013 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1014 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1015 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1016 ifd->length += corrbytes;
1017 atomic_add(-corrbytes, &ubc->corrbytes);
1018 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001019
1020 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -07001021 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1022 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001023 if (ifd->offset < 0) {
1024 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001025 gig_dbg(DEBUG_ISO,
1026 "%s: buffer busy at frame %d",
1027 __func__, nframe);
1028 /* tasklet will be restarted from
1029 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001030 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001031 dev_err(ucx->bcs->cs->dev,
1032 "%s: buffer error %d at frame %d\n",
1033 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001034 return ifd->offset;
1035 }
1036 break;
1037 }
1038 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1039 ifd->status = 0;
1040 ifd->actual_length = 0;
1041 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001042 if (unlikely(nframe == 0))
1043 return 0; /* no data to send */
1044 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001045
Tilman Schmidt73a88812006-04-22 02:35:30 -07001046 rc = usb_submit_urb(urb, SLAB_ATOMIC);
1047 if (unlikely(rc)) {
1048 if (rc == -ENODEV)
1049 /* device removed - give up silently */
1050 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1051 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001052 dev_err(ucx->bcs->cs->dev,
1053 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001054 get_usb_rcmsg(rc));
1055 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001056 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001057 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001058 return nframe;
1059}
1060
1061/* write_iso_tasklet
1062 * tasklet scheduled when an isochronous output URB from the Gigaset device
1063 * has completed
1064 * parameter:
1065 * data B channel state structure
1066 */
1067static void write_iso_tasklet(unsigned long data)
1068{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001069 struct bc_state *bcs = (struct bc_state *) data;
1070 struct bas_bc_state *ubc = bcs->hw.bas;
1071 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001072 struct isow_urbctx_t *done, *next, *ovfl;
1073 struct urb *urb;
1074 struct usb_iso_packet_descriptor *ifd;
1075 int offset;
1076 unsigned long flags;
1077 int i;
1078 struct sk_buff *skb;
1079 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001080 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001081
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001082 /* loop while completed URBs arrive in time */
1083 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001084 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001085 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001086 return;
1087 }
1088
1089 /* retrieve completed URBs */
1090 spin_lock_irqsave(&ubc->isooutlock, flags);
1091 done = ubc->isooutdone;
1092 ubc->isooutdone = NULL;
1093 ovfl = ubc->isooutovfl;
1094 ubc->isooutovfl = NULL;
1095 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1096 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001097 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001098 error_hangup(bcs);
1099 break;
1100 }
1101 if (!done)
1102 break;
1103
1104 /* submit free URB if available */
1105 spin_lock_irqsave(&ubc->isooutlock, flags);
1106 next = ubc->isooutfree;
1107 ubc->isooutfree = NULL;
1108 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1109 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001110 rc = submit_iso_write_urb(next);
1111 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001112 /* could not submit URB, put it back */
1113 spin_lock_irqsave(&ubc->isooutlock, flags);
1114 if (ubc->isooutfree == NULL) {
1115 ubc->isooutfree = next;
1116 next = NULL;
1117 }
1118 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1119 if (next) {
1120 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001121 dev_err(cs->dev,
1122 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001123 error_hangup(bcs);
1124 }
1125 }
1126 }
1127
1128 /* process completed URB */
1129 urb = done->urb;
1130 switch (urb->status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001131 case -EXDEV: /* partial completion */
1132 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1133 __func__);
1134 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001135 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001136 /* inspect individual frames
1137 * assumptions (for lack of documentation):
1138 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001139 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001140 * - all following frames are not sent at all
1141 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001142 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001143 for (i = 0; i < BAS_NUMFRAMES; i++) {
1144 ifd = &urb->iso_frame_desc[i];
1145 if (ifd->status ||
1146 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001147 dev_warn(cs->dev,
1148 "isochronous write: frame %d: %s, "
1149 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001150 i, get_usb_statmsg(ifd->status),
1151 ifd->actual_length, ifd->length);
1152 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001153 ifd->actual_length)
1154 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001155 break;
1156 }
1157 }
1158#ifdef CONFIG_GIGASET_DEBUG
1159 /* check assumption on remaining frames */
1160 for (; i < BAS_NUMFRAMES; i++) {
1161 ifd = &urb->iso_frame_desc[i];
1162 if (ifd->status != -EINPROGRESS
1163 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001164 dev_warn(cs->dev,
1165 "isochronous write: frame %d: %s, "
1166 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001167 i, get_usb_statmsg(ifd->status),
1168 ifd->actual_length, ifd->length);
1169 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001170 ifd->actual_length)
1171 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001172 break;
1173 }
1174 }
1175#endif
1176 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001177 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001178 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001179 error_hangup(bcs);
1180 break;
1181 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001182 dev_warn(cs->dev, "isochronous write: %s\n",
1183 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001184 }
1185
1186 /* mark the write buffer area covered by this URB as free */
1187 if (done->limit >= 0)
1188 atomic_set(&ubc->isooutbuf->read, done->limit);
1189
1190 /* mark URB as free */
1191 spin_lock_irqsave(&ubc->isooutlock, flags);
1192 next = ubc->isooutfree;
1193 ubc->isooutfree = done;
1194 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1195 if (next) {
1196 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001197 rc = submit_iso_write_urb(next);
1198 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001199 /* couldn't submit */
1200 error_hangup(bcs);
1201 }
1202 }
1203 }
1204
1205 /* process queued SKBs */
1206 while ((skb = skb_dequeue(&bcs->squeue))) {
1207 /* copy to output buffer, doing L2 encapsulation */
1208 len = skb->len;
1209 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1210 /* insufficient buffer space, push back onto queue */
1211 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001212 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1213 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001214 break;
1215 }
1216 skb_pull(skb, len);
1217 gigaset_skb_sent(bcs, skb);
1218 dev_kfree_skb_any(skb);
1219 }
1220}
1221
1222/* Isochronous Read - Bottom Half */
1223/* ============================== */
1224
1225/* read_iso_tasklet
1226 * tasklet scheduled when an isochronous input URB from the Gigaset device
1227 * has completed
1228 * parameter:
1229 * data B channel state structure
1230 */
1231static void read_iso_tasklet(unsigned long data)
1232{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001233 struct bc_state *bcs = (struct bc_state *) data;
1234 struct bas_bc_state *ubc = bcs->hw.bas;
1235 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001236 struct urb *urb;
1237 char *rcvbuf;
1238 unsigned long flags;
1239 int totleft, numbytes, offset, frame, rc;
1240
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001241 /* loop while more completed URBs arrive in the meantime */
1242 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001243 /* retrieve URB */
1244 spin_lock_irqsave(&ubc->isoinlock, flags);
1245 if (!(urb = ubc->isoindone)) {
1246 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1247 return;
1248 }
1249 ubc->isoindone = NULL;
1250 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001251 dev_warn(cs->dev,
1252 "isochronous read overrun, "
1253 "dropped URB with status: %s, %d bytes lost\n",
1254 get_usb_statmsg(ubc->loststatus),
1255 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001256 ubc->loststatus = -EINPROGRESS;
1257 }
1258 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1259
1260 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001261 gig_dbg(DEBUG_ISO,
1262 "%s: channel not running, "
1263 "dropped URB with status: %s",
1264 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001265 return;
1266 }
1267
1268 switch (urb->status) {
1269 case 0: /* normal completion */
1270 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001271 case -EXDEV: /* inspect individual frames
1272 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001273 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1274 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001275 break;
1276 case -ENOENT:
1277 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001278 case -EINPROGRESS:
1279 gig_dbg(DEBUG_ISO, "%s: %s",
1280 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001281 continue; /* -> skip */
1282 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001283 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001284 error_hangup(bcs);
1285 continue; /* -> skip */
1286 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001287 dev_warn(cs->dev, "isochronous read: %s\n",
1288 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001289 goto error;
1290 }
1291
1292 rcvbuf = urb->transfer_buffer;
1293 totleft = urb->actual_length;
1294 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1295 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001296 dev_warn(cs->dev,
1297 "isochronous read: frame %d: %s\n",
1298 frame,
1299 get_usb_statmsg(
1300 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001301 break;
1302 }
1303 numbytes = urb->iso_frame_desc[frame].actual_length;
1304 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001305 dev_warn(cs->dev,
1306 "isochronous read: frame %d: "
1307 "numbytes (%d) > BAS_MAXFRAME\n",
1308 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001309 break;
1310 }
1311 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001312 dev_warn(cs->dev,
1313 "isochronous read: frame %d: "
1314 "numbytes (%d) > totleft (%d)\n",
1315 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001316 break;
1317 }
1318 offset = urb->iso_frame_desc[frame].offset;
1319 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001320 dev_warn(cs->dev,
1321 "isochronous read: frame %d: "
1322 "offset (%d) + numbytes (%d) "
1323 "> BAS_INBUFSIZE\n",
1324 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001325 break;
1326 }
1327 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1328 totleft -= numbytes;
1329 }
1330 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001331 dev_warn(cs->dev,
1332 "isochronous read: %d data bytes missing\n",
1333 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001334
1335 error:
1336 /* URB processed, resubmit */
1337 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1338 urb->iso_frame_desc[frame].status = 0;
1339 urb->iso_frame_desc[frame].actual_length = 0;
1340 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001341 /* urb->dev is clobbered by USB subsystem */
1342 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001343 urb->transfer_flags = URB_ISO_ASAP;
1344 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001345 rc = usb_submit_urb(urb, SLAB_ATOMIC);
1346 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001347 dev_err(cs->dev,
1348 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001349 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001350 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1351 error_hangup(bcs);
1352 }
1353 }
1354}
1355
1356/* Channel Operations */
1357/* ================== */
1358
1359/* req_timeout
1360 * timeout routine for control output request
1361 * argument:
1362 * B channel control structure
1363 */
1364static void req_timeout(unsigned long data)
1365{
1366 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001367 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001368 int pending;
1369 unsigned long flags;
1370
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001371 check_pending(ucs);
1372
1373 spin_lock_irqsave(&ucs->lock, flags);
1374 pending = ucs->pending;
1375 ucs->pending = 0;
1376 spin_unlock_irqrestore(&ucs->lock, flags);
1377
1378 switch (pending) {
1379 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001380 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001381 break;
1382
1383 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001384 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001385 error_reset(bcs->cs);
1386 break;
1387
1388 case HD_OPEN_B2CHANNEL:
1389 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001390 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1391 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001392 error_hangup(bcs);
1393 break;
1394
1395 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001396 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001397 break;
1398
1399 case HD_CLOSE_B2CHANNEL:
1400 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001401 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1402 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001403 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001404 break;
1405
1406 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001407 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1408 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001409 }
1410}
1411
1412/* write_ctrl_callback
1413 * USB completion handler for control pipe output
1414 * called by the USB subsystem in interrupt context
1415 * parameter:
1416 * urb USB request block of completed request
1417 * urb->context = hardware specific controller state structure
1418 */
1419static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1420{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001421 struct bas_cardstate *ucs = urb->context;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001422 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001423 unsigned long flags;
1424
Tilman Schmidt06163f82006-06-26 00:25:33 -07001425 /* check status */
1426 switch (urb->status) {
1427 case 0: /* normal completion */
1428 spin_lock_irqsave(&ucs->lock, flags);
1429 switch (ucs->pending) {
1430 case HD_DEVICE_INIT_ACK: /* no reply expected */
1431 del_timer(&ucs->timer_ctrl);
1432 ucs->pending = 0;
1433 break;
1434 }
1435 spin_unlock_irqrestore(&ucs->lock, flags);
1436 return;
1437
1438 case -ENOENT: /* cancelled */
1439 case -ECONNRESET: /* cancelled (async) */
1440 case -EINPROGRESS: /* pending */
1441 case -ENODEV: /* device removed */
1442 case -ESHUTDOWN: /* device shut down */
1443 /* ignore silently */
1444 gig_dbg(DEBUG_USBREQ, "%s: %s",
1445 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001446 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001447
1448 default: /* any failure */
1449 if (++ucs->retry_ctrl > BAS_RETRY) {
1450 dev_err(&ucs->interface->dev,
1451 "control request 0x%02x failed: %s\n",
1452 ucs->dr_ctrl.bRequest,
1453 get_usb_statmsg(urb->status));
1454 break; /* give up */
1455 }
1456 dev_notice(&ucs->interface->dev,
1457 "control request 0x%02x: %s, retry %d\n",
1458 ucs->dr_ctrl.bRequest, get_usb_statmsg(urb->status),
1459 ucs->retry_ctrl);
1460 /* urb->dev is clobbered by USB subsystem */
1461 urb->dev = ucs->udev;
1462 rc = usb_submit_urb(urb, SLAB_ATOMIC);
1463 if (unlikely(rc)) {
1464 dev_err(&ucs->interface->dev,
1465 "could not resubmit request 0x%02x: %s\n",
1466 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1467 break;
1468 }
1469 /* resubmitted */
1470 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001471 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001472
1473 /* failed, clear pending request */
1474 spin_lock_irqsave(&ucs->lock, flags);
1475 del_timer(&ucs->timer_ctrl);
1476 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001477 spin_unlock_irqrestore(&ucs->lock, flags);
1478}
1479
1480/* req_submit
1481 * submit a control output request without message buffer to the Gigaset base
1482 * and optionally start a timeout
1483 * parameters:
1484 * bcs B channel control structure
1485 * req control request code (HD_*)
1486 * val control request parameter value (set to 0 if unused)
1487 * timeout timeout in seconds (0: no timeout)
1488 * return value:
1489 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001490 * -EBUSY if another request is pending
1491 * any URB submission error code
1492 */
1493static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1494{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001495 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001496 int ret;
1497 unsigned long flags;
1498
Tilman Schmidt784d5852006-04-10 22:55:04 -07001499 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001500
1501 spin_lock_irqsave(&ucs->lock, flags);
1502 if (ucs->pending) {
1503 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001504 dev_err(bcs->cs->dev,
1505 "submission of request 0x%02x failed: "
1506 "request 0x%02x still pending\n",
1507 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001508 return -EBUSY;
1509 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001510
1511 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1512 ucs->dr_ctrl.bRequest = req;
1513 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1514 ucs->dr_ctrl.wIndex = 0;
1515 ucs->dr_ctrl.wLength = 0;
1516 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001517 usb_sndctrlpipe(ucs->udev, 0),
1518 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1519 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001520 ucs->retry_ctrl = 0;
1521 ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC);
1522 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001523 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001524 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001525 spin_unlock_irqrestore(&ucs->lock, flags);
1526 return ret;
1527 }
1528 ucs->pending = req;
1529
1530 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001531 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001532 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1533 ucs->timer_ctrl.data = (unsigned long) bcs;
1534 ucs->timer_ctrl.function = req_timeout;
1535 add_timer(&ucs->timer_ctrl);
1536 }
1537
1538 spin_unlock_irqrestore(&ucs->lock, flags);
1539 return 0;
1540}
1541
1542/* gigaset_init_bchannel
1543 * called by common.c to connect a B channel
1544 * initialize isochronous I/O and tell the Gigaset base to open the channel
1545 * argument:
1546 * B channel control structure
1547 * return value:
1548 * 0 on success, error code < 0 on error
1549 */
1550static int gigaset_init_bchannel(struct bc_state *bcs)
1551{
1552 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001553 unsigned long flags;
1554
1555 spin_lock_irqsave(&bcs->cs->lock, flags);
1556 if (unlikely(!bcs->cs->connected)) {
1557 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1558 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1559 return -ENODEV;
1560 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001561
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001562 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001563 dev_err(bcs->cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001564 "could not start isochronous I/O for channel B%d: %s\n",
1565 bcs->channel + 1,
1566 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1567 if (ret != -ENODEV)
1568 error_hangup(bcs);
1569 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001570 return ret;
1571 }
1572
1573 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1574 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001575 dev_err(bcs->cs->dev, "could not open channel B%d\n",
1576 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001577 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001578 if (ret != -ENODEV)
1579 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001580 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001581
1582 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001583 return ret;
1584}
1585
1586/* gigaset_close_bchannel
1587 * called by common.c to disconnect a B channel
1588 * tell the Gigaset base to close the channel
1589 * stopping isochronous I/O and LL notification will be done when the
1590 * acknowledgement for the close arrives
1591 * argument:
1592 * B channel control structure
1593 * return value:
1594 * 0 on success, error code < 0 on error
1595 */
1596static int gigaset_close_bchannel(struct bc_state *bcs)
1597{
1598 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001599 unsigned long flags;
1600
1601 spin_lock_irqsave(&bcs->cs->lock, flags);
1602 if (unlikely(!bcs->cs->connected)) {
1603 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1604 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1605 return -ENODEV;
1606 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001607
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001608 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1609 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1610 /* channel not running: just signal common.c */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001611 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001612 gigaset_bchannel_down(bcs);
1613 return 0;
1614 }
1615
Tilman Schmidt73a88812006-04-22 02:35:30 -07001616 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001617 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1618 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidt73a88812006-04-22 02:35:30 -07001619 dev_err(bcs->cs->dev, "closing channel B%d failed\n",
1620 bcs->channel + 1);
1621
1622 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001623 return ret;
1624}
1625
1626/* Device Operations */
1627/* ================= */
1628
1629/* complete_cb
1630 * unqueue first command buffer from queue, waking any sleepers
1631 * must be called with cs->cmdlock held
1632 * parameter:
1633 * cs controller state structure
1634 */
1635static void complete_cb(struct cardstate *cs)
1636{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001637 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001638
1639 /* unqueue completed buffer */
1640 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001641 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1642 "write_command: sent %u bytes, %u left",
1643 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001644 if ((cs->cmdbuf = cb->next) != NULL) {
1645 cs->cmdbuf->prev = NULL;
1646 cs->curlen = cs->cmdbuf->len;
1647 } else {
1648 cs->lastcmdbuf = NULL;
1649 cs->curlen = 0;
1650 }
1651
1652 if (cb->wake_tasklet)
1653 tasklet_schedule(cb->wake_tasklet);
1654
1655 kfree(cb);
1656}
1657
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001658/* write_command_callback
1659 * USB completion handler for AT command transmission
1660 * called by the USB subsystem in interrupt context
1661 * parameter:
1662 * urb USB request block of completed request
1663 * urb->context = controller state structure
1664 */
1665static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1666{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001667 struct cardstate *cs = urb->context;
1668 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001669 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001670
Tilman Schmidt73a88812006-04-22 02:35:30 -07001671 update_basstate(ucs, 0, BS_ATWRPEND);
1672
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001673 /* check status */
1674 switch (urb->status) {
1675 case 0: /* normal completion */
1676 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001677 case -ENOENT: /* cancelled */
1678 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001679 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001680 case -ENODEV: /* device removed */
1681 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001682 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001683 gig_dbg(DEBUG_USBREQ, "%s: %s",
1684 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001685 return;
1686 default: /* any failure */
1687 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001688 dev_warn(cs->dev,
1689 "command write: %s, "
1690 "giving up after %d retries\n",
1691 get_usb_statmsg(urb->status),
1692 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001693 break;
1694 }
1695 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001696 dev_warn(cs->dev,
1697 "command write: %s, "
1698 "cannot retry - cmdbuf gone\n",
1699 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001700 break;
1701 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001702 dev_notice(cs->dev, "command write: %s, retry %d\n",
1703 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001704 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1705 /* resubmitted - bypass regular exit block */
1706 return;
1707 /* command send failed, assume base still waiting */
1708 update_basstate(ucs, BS_ATREADY, 0);
1709 }
1710
1711 spin_lock_irqsave(&cs->cmdlock, flags);
1712 if (cs->cmdbuf != NULL)
1713 complete_cb(cs);
1714 spin_unlock_irqrestore(&cs->cmdlock, flags);
1715}
1716
1717/* atrdy_timeout
1718 * timeout routine for AT command transmission
1719 * argument:
1720 * controller state structure
1721 */
1722static void atrdy_timeout(unsigned long data)
1723{
1724 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001725 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001726
Tilman Schmidt784d5852006-04-10 22:55:04 -07001727 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001728
1729 /* fake the missing signal - what else can I do? */
1730 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1731 start_cbsend(cs);
1732}
1733
1734/* atwrite_submit
1735 * submit an HD_WRITE_ATMESSAGE command URB
1736 * parameters:
1737 * cs controller state structure
1738 * buf buffer containing command to send
1739 * len length of command to send
1740 * return value:
1741 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001742 * -EBUSY if another request is pending
1743 * any URB submission error code
1744 */
1745static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1746{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001747 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001748 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001749
Tilman Schmidt784d5852006-04-10 22:55:04 -07001750 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001751
Tilman Schmidt73a88812006-04-22 02:35:30 -07001752 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001753 dev_err(cs->dev,
1754 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001755 return -EBUSY;
1756 }
1757
1758 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1759 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1760 ucs->dr_cmd_out.wValue = 0;
1761 ucs->dr_cmd_out.wIndex = 0;
1762 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1763 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1764 usb_sndctrlpipe(ucs->udev, 0),
1765 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1766 write_command_callback, cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001767 rc = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC);
1768 if (unlikely(rc)) {
1769 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001770 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001771 get_usb_rcmsg(rc));
1772 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001773 }
1774
Tilman Schmidt73a88812006-04-22 02:35:30 -07001775 /* submitted successfully, start timeout if necessary */
1776 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001777 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1778 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001779 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1780 ucs->timer_atrdy.data = (unsigned long) cs;
1781 ucs->timer_atrdy.function = atrdy_timeout;
1782 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001783 }
1784 return 0;
1785}
1786
1787/* start_cbsend
1788 * start transmission of AT command queue if necessary
1789 * parameter:
1790 * cs controller state structure
1791 * return value:
1792 * 0 on success
1793 * error code < 0 on error
1794 */
1795static int start_cbsend(struct cardstate *cs)
1796{
1797 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001798 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001799 unsigned long flags;
1800 int rc;
1801 int retval = 0;
1802
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001803 /* check if AT channel is open */
1804 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001805 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001806 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1807 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001808 /* flush command queue */
1809 spin_lock_irqsave(&cs->cmdlock, flags);
1810 while (cs->cmdbuf != NULL)
1811 complete_cb(cs);
1812 spin_unlock_irqrestore(&cs->cmdlock, flags);
1813 }
1814 return rc;
1815 }
1816
1817 /* try to send first command in queue */
1818 spin_lock_irqsave(&cs->cmdlock, flags);
1819
1820 while ((cb = cs->cmdbuf) != NULL &&
1821 atomic_read(&ucs->basstate) & BS_ATREADY) {
1822 ucs->retry_cmd_out = 0;
1823 rc = atwrite_submit(cs, cb->buf, cb->len);
1824 if (unlikely(rc)) {
1825 retval = rc;
1826 complete_cb(cs);
1827 }
1828 }
1829
1830 spin_unlock_irqrestore(&cs->cmdlock, flags);
1831 return retval;
1832}
1833
1834/* gigaset_write_cmd
1835 * This function is called by the device independent part of the driver
1836 * to transmit an AT command string to the Gigaset device.
1837 * It encapsulates the device specific method for transmission over the
1838 * direct USB connection to the base.
1839 * The command string is added to the queue of commands to send, and
1840 * USB transmission is started if necessary.
1841 * parameters:
1842 * cs controller state structure
1843 * buf command string to send
1844 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001845 * wake_tasklet tasklet to run when transmission is completed
1846 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001847 * return value:
1848 * number of bytes queued on success
1849 * error code < 0 on error
1850 */
1851static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001852 const unsigned char *buf, int len,
1853 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001854{
1855 struct cmdbuf_t *cb;
1856 unsigned long flags;
1857 int status;
1858
1859 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001860 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001861 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001862
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001863 if (len <= 0)
1864 return 0; /* nothing to do */
1865
1866 if (len > IF_WRITEBUF)
1867 len = IF_WRITEBUF;
1868 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001869 dev_err(cs->dev, "%s: out of memory\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001870 return -ENOMEM;
1871 }
1872
1873 memcpy(cb->buf, buf, len);
1874 cb->len = len;
1875 cb->offset = 0;
1876 cb->next = NULL;
1877 cb->wake_tasklet = wake_tasklet;
1878
1879 spin_lock_irqsave(&cs->cmdlock, flags);
1880 cb->prev = cs->lastcmdbuf;
1881 if (cs->lastcmdbuf)
1882 cs->lastcmdbuf->next = cb;
1883 else {
1884 cs->cmdbuf = cb;
1885 cs->curlen = len;
1886 }
1887 cs->cmdbytes += len;
1888 cs->lastcmdbuf = cb;
1889 spin_unlock_irqrestore(&cs->cmdlock, flags);
1890
Tilman Schmidt73a88812006-04-22 02:35:30 -07001891 spin_lock_irqsave(&cs->lock, flags);
1892 if (unlikely(!cs->connected)) {
1893 spin_unlock_irqrestore(&cs->lock, flags);
1894 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1895 return -ENODEV;
1896 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001897 status = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001898 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001899 return status < 0 ? status : len;
1900}
1901
1902/* gigaset_write_room
1903 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001904 * return number of characters the driver will accept to be written via
1905 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001906 * parameter:
1907 * controller state structure
1908 * return value:
1909 * number of characters
1910 */
1911static int gigaset_write_room(struct cardstate *cs)
1912{
1913 return IF_WRITEBUF;
1914}
1915
1916/* gigaset_chars_in_buffer
1917 * tty_driver.chars_in_buffer interface routine
1918 * return number of characters waiting to be sent
1919 * parameter:
1920 * controller state structure
1921 * return value:
1922 * number of characters
1923 */
1924static int gigaset_chars_in_buffer(struct cardstate *cs)
1925{
1926 unsigned long flags;
1927 unsigned bytes;
1928
1929 spin_lock_irqsave(&cs->cmdlock, flags);
1930 bytes = cs->cmdbytes;
1931 spin_unlock_irqrestore(&cs->cmdlock, flags);
1932
1933 return bytes;
1934}
1935
1936/* gigaset_brkchars
1937 * implementation of ioctl(GIGASET_BRKCHARS)
1938 * parameter:
1939 * controller state structure
1940 * return value:
1941 * -EINVAL (unimplemented function)
1942 */
1943static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1944{
1945 return -EINVAL;
1946}
1947
1948
1949/* Device Initialization/Shutdown */
1950/* ============================== */
1951
1952/* Free hardware dependent part of the B channel structure
1953 * parameter:
1954 * bcs B channel structure
1955 * return value:
1956 * !=0 on success
1957 */
1958static int gigaset_freebcshw(struct bc_state *bcs)
1959{
Tilman Schmidt73a88812006-04-22 02:35:30 -07001960 struct bas_bc_state *ubc = bcs->hw.bas;
1961 int i;
1962
1963 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001964 return 0;
1965
Tilman Schmidt73a88812006-04-22 02:35:30 -07001966 /* kill URBs and tasklets before freeing - better safe than sorry */
1967 atomic_set(&ubc->running, 0);
1968 for (i = 0; i < BAS_OUTURBS; ++i)
1969 if (ubc->isoouturbs[i].urb) {
1970 gig_dbg(DEBUG_INIT, "%s: killing iso out URB %d",
1971 __func__, i);
1972 usb_kill_urb(ubc->isoouturbs[i].urb);
1973 usb_free_urb(ubc->isoouturbs[i].urb);
1974 }
1975 for (i = 0; i < BAS_INURBS; ++i)
1976 if (ubc->isoinurbs[i]) {
1977 gig_dbg(DEBUG_INIT, "%s: killing iso in URB %d",
1978 __func__, i);
1979 usb_kill_urb(ubc->isoinurbs[i]);
1980 usb_free_urb(ubc->isoinurbs[i]);
1981 }
1982 tasklet_kill(&ubc->sent_tasklet);
1983 tasklet_kill(&ubc->rcvd_tasklet);
1984 kfree(ubc->isooutbuf);
1985 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001986 bcs->hw.bas = NULL;
1987 return 1;
1988}
1989
1990/* Initialize hardware dependent part of the B channel structure
1991 * parameter:
1992 * bcs B channel structure
1993 * return value:
1994 * !=0 on success
1995 */
1996static int gigaset_initbcshw(struct bc_state *bcs)
1997{
1998 int i;
1999 struct bas_bc_state *ubc;
2000
2001 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2002 if (!ubc) {
2003 err("could not allocate bas_bc_state");
2004 return 0;
2005 }
2006
2007 atomic_set(&ubc->running, 0);
2008 atomic_set(&ubc->corrbytes, 0);
2009 spin_lock_init(&ubc->isooutlock);
2010 for (i = 0; i < BAS_OUTURBS; ++i) {
2011 ubc->isoouturbs[i].urb = NULL;
2012 ubc->isoouturbs[i].bcs = bcs;
2013 }
2014 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2015 ubc->numsub = 0;
2016 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2017 err("could not allocate isochronous output buffer");
2018 kfree(ubc);
2019 bcs->hw.bas = NULL;
2020 return 0;
2021 }
2022 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002023 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002024
2025 spin_lock_init(&ubc->isoinlock);
2026 for (i = 0; i < BAS_INURBS; ++i)
2027 ubc->isoinurbs[i] = NULL;
2028 ubc->isoindone = NULL;
2029 ubc->loststatus = -EINPROGRESS;
2030 ubc->isoinlost = 0;
2031 ubc->seqlen = 0;
2032 ubc->inbyte = 0;
2033 ubc->inbits = 0;
2034 ubc->goodbytes = 0;
2035 ubc->alignerrs = 0;
2036 ubc->fcserrs = 0;
2037 ubc->frameerrs = 0;
2038 ubc->giants = 0;
2039 ubc->runts = 0;
2040 ubc->aborts = 0;
2041 ubc->shared0s = 0;
2042 ubc->stolen0s = 0;
2043 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002044 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002045 return 1;
2046}
2047
2048static void gigaset_reinitbcshw(struct bc_state *bcs)
2049{
2050 struct bas_bc_state *ubc = bcs->hw.bas;
2051
2052 atomic_set(&bcs->hw.bas->running, 0);
2053 atomic_set(&bcs->hw.bas->corrbytes, 0);
2054 bcs->hw.bas->numsub = 0;
2055 spin_lock_init(&ubc->isooutlock);
2056 spin_lock_init(&ubc->isoinlock);
2057 ubc->loststatus = -EINPROGRESS;
2058}
2059
2060static void gigaset_freecshw(struct cardstate *cs)
2061{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002062 /* timers, URBs and rcvbuf are disposed of in disconnect */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002063 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002064 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002065}
2066
2067static int gigaset_initcshw(struct cardstate *cs)
2068{
2069 struct bas_cardstate *ucs;
2070
2071 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2072 if (!ucs)
2073 return 0;
2074
2075 ucs->urb_cmd_in = NULL;
2076 ucs->urb_cmd_out = NULL;
2077 ucs->rcvbuf = NULL;
2078 ucs->rcvbuf_size = 0;
2079
2080 spin_lock_init(&ucs->lock);
2081 ucs->pending = 0;
2082
2083 atomic_set(&ucs->basstate, 0);
2084 init_timer(&ucs->timer_ctrl);
2085 init_timer(&ucs->timer_atrdy);
2086 init_timer(&ucs->timer_cmd_in);
2087
2088 return 1;
2089}
2090
2091/* freeurbs
2092 * unlink and deallocate all URBs unconditionally
2093 * caller must make sure that no commands are still in progress
2094 * parameter:
2095 * cs controller state structure
2096 */
2097static void freeurbs(struct cardstate *cs)
2098{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002099 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002100 struct bas_bc_state *ubc;
2101 int i, j;
2102
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002103 for (j = 0; j < 2; ++j) {
2104 ubc = cs->bcs[j].hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002105 for (i = 0; i < BAS_OUTURBS; ++i)
2106 if (ubc->isoouturbs[i].urb) {
2107 usb_kill_urb(ubc->isoouturbs[i].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002108 gig_dbg(DEBUG_INIT,
2109 "%s: isoc output URB %d/%d unlinked",
2110 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002111 usb_free_urb(ubc->isoouturbs[i].urb);
2112 ubc->isoouturbs[i].urb = NULL;
2113 }
2114 for (i = 0; i < BAS_INURBS; ++i)
2115 if (ubc->isoinurbs[i]) {
2116 usb_kill_urb(ubc->isoinurbs[i]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002117 gig_dbg(DEBUG_INIT,
2118 "%s: isoc input URB %d/%d unlinked",
2119 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002120 usb_free_urb(ubc->isoinurbs[i]);
2121 ubc->isoinurbs[i] = NULL;
2122 }
2123 }
2124 if (ucs->urb_int_in) {
2125 usb_kill_urb(ucs->urb_int_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002126 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2127 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002128 usb_free_urb(ucs->urb_int_in);
2129 ucs->urb_int_in = NULL;
2130 }
2131 if (ucs->urb_cmd_out) {
2132 usb_kill_urb(ucs->urb_cmd_out);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002133 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2134 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002135 usb_free_urb(ucs->urb_cmd_out);
2136 ucs->urb_cmd_out = NULL;
2137 }
2138 if (ucs->urb_cmd_in) {
2139 usb_kill_urb(ucs->urb_cmd_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002140 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2141 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002142 usb_free_urb(ucs->urb_cmd_in);
2143 ucs->urb_cmd_in = NULL;
2144 }
2145 if (ucs->urb_ctrl) {
2146 usb_kill_urb(ucs->urb_ctrl);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002147 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2148 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002149 usb_free_urb(ucs->urb_ctrl);
2150 ucs->urb_ctrl = NULL;
2151 }
2152}
2153
2154/* gigaset_probe
2155 * This function is called when a new USB device is connected.
2156 * It checks whether the new device is handled by this driver.
2157 */
2158static int gigaset_probe(struct usb_interface *interface,
2159 const struct usb_device_id *id)
2160{
2161 struct usb_host_interface *hostif;
2162 struct usb_device *udev = interface_to_usbdev(interface);
2163 struct cardstate *cs = NULL;
2164 struct bas_cardstate *ucs = NULL;
2165 struct bas_bc_state *ubc;
2166 struct usb_endpoint_descriptor *endpoint;
2167 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002168 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002169
Tilman Schmidt784d5852006-04-10 22:55:04 -07002170 gig_dbg(DEBUG_ANY,
2171 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2172 __func__, le16_to_cpu(udev->descriptor.idVendor),
2173 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002174
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002175 /* set required alternate setting */
2176 hostif = interface->cur_altsetting;
2177 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002178 gig_dbg(DEBUG_ANY,
2179 "%s: wrong alternate setting %d - trying to switch",
2180 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002181 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002182 dev_warn(&udev->dev, "usb_set_interface failed, "
2183 "device %d interface %d altsetting %d\n",
2184 udev->devnum, hostif->desc.bInterfaceNumber,
2185 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002186 return -ENODEV;
2187 }
2188 hostif = interface->cur_altsetting;
2189 }
2190
2191 /* Reject application specific interfaces
2192 */
2193 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002194 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2195 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002196 return -ENODEV;
2197 }
2198
Tilman Schmidt784d5852006-04-10 22:55:04 -07002199 dev_info(&udev->dev,
2200 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2201 __func__, le16_to_cpu(udev->descriptor.idVendor),
2202 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002203
2204 cs = gigaset_getunassignedcs(driver);
2205 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002206 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002207 return -ENODEV;
2208 }
2209 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002210
2211 /* save off device structure ptrs for later use */
2212 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002213 ucs->udev = udev;
2214 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002215 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002216
2217 /* allocate URBs:
2218 * - one for the interrupt pipe
2219 * - three for the different uses of the default control pipe
2220 * - three for each isochronous pipe
2221 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002222 if (!(ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL)) ||
2223 !(ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL)) ||
2224 !(ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL)) ||
2225 !(ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL)))
2226 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002227
2228 for (j = 0; j < 2; ++j) {
2229 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002230 for (i = 0; i < BAS_OUTURBS; ++i)
2231 if (!(ubc->isoouturbs[i].urb =
2232 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL)))
2233 goto allocerr;
2234 for (i = 0; i < BAS_INURBS; ++i)
2235 if (!(ubc->isoinurbs[i] =
2236 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL)))
2237 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002238 }
2239
2240 ucs->rcvbuf = NULL;
2241 ucs->rcvbuf_size = 0;
2242
2243 /* Fill the interrupt urb and send it to the core */
2244 endpoint = &hostif->endpoint[0].desc;
2245 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002246 usb_rcvintpipe(udev,
2247 (endpoint->bEndpointAddress) & 0x0f),
2248 ucs->int_in_buf, 3, read_int_callback, cs,
2249 endpoint->bInterval);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002250 if ((rc = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002251 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002252 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002253 goto error;
2254 }
2255
2256 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002257 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002258 goto error;
2259
2260 /* tell common part that the device is ready */
2261 if (startmode == SM_LOCKED)
2262 atomic_set(&cs->mstate, MS_LOCKED);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002263
2264 /* save address of controller structure */
2265 usb_set_intfdata(interface, cs);
2266
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002267 if (!gigaset_start(cs))
2268 goto error;
2269
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002270 return 0;
2271
Tilman Schmidt73a88812006-04-22 02:35:30 -07002272allocerr:
2273 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002274error:
2275 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002276 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002277 gigaset_unassign(cs);
2278 return -ENODEV;
2279}
2280
2281/* gigaset_disconnect
2282 * This function is called when the Gigaset base is unplugged.
2283 */
2284static void gigaset_disconnect(struct usb_interface *interface)
2285{
2286 struct cardstate *cs;
2287 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002288 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002289
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002290 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002291
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002292 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002293
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002294 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002295
2296 /* mark base as not ready, all channels disconnected */
2297 atomic_set(&ucs->basstate, 0);
2298
2299 /* tell LL all channels are down */
2300 //FIXME shouldn't gigaset_stop() do this?
2301 for (j = 0; j < 2; ++j)
2302 gigaset_bchannel_down(cs->bcs + j);
2303
2304 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002305 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002306
2307 /* stop timers and URBs, free ressources */
2308 del_timer_sync(&ucs->timer_ctrl);
2309 del_timer_sync(&ucs->timer_atrdy);
2310 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002311 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002312 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002313 kfree(ucs->rcvbuf);
2314 ucs->rcvbuf = NULL;
2315 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002316 usb_put_dev(ucs->udev);
2317 ucs->interface = NULL;
2318 ucs->udev = NULL;
2319 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002320 gigaset_unassign(cs);
2321}
2322
2323static struct gigaset_ops gigops = {
2324 gigaset_write_cmd,
2325 gigaset_write_room,
2326 gigaset_chars_in_buffer,
2327 gigaset_brkchars,
2328 gigaset_init_bchannel,
2329 gigaset_close_bchannel,
2330 gigaset_initbcshw,
2331 gigaset_freebcshw,
2332 gigaset_reinitbcshw,
2333 gigaset_initcshw,
2334 gigaset_freecshw,
2335 gigaset_set_modem_ctrl,
2336 gigaset_baud_rate,
2337 gigaset_set_line_ctrl,
2338 gigaset_isoc_send_skb,
2339 gigaset_isoc_input,
2340};
2341
2342/* bas_gigaset_init
2343 * This function is called after the kernel module is loaded.
2344 */
2345static int __init bas_gigaset_init(void)
2346{
2347 int result;
2348
2349 /* allocate memory for our driver state and intialize it */
2350 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002351 GIGASET_MODULENAME, GIGASET_DEVNAME,
2352 GIGASET_DEVFSNAME, &gigops,
2353 THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002354 goto error;
2355
2356 /* allocate memory for our device state and intialize it */
Tilman Schmidt917f5082006-04-10 22:55:00 -07002357 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2358 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002359 if (!cardstate)
2360 goto error;
2361
2362 /* register this driver with the USB subsystem */
2363 result = usb_register(&gigaset_usb_driver);
2364 if (result < 0) {
2365 err("usb_register failed (error %d)", -result);
2366 goto error;
2367 }
2368
2369 info(DRIVER_AUTHOR);
2370 info(DRIVER_DESC);
2371 return 0;
2372
2373error: if (cardstate)
2374 gigaset_freecs(cardstate);
2375 cardstate = NULL;
2376 if (driver)
2377 gigaset_freedriver(driver);
2378 driver = NULL;
2379 return -1;
2380}
2381
2382/* bas_gigaset_exit
2383 * This function is called before the kernel module is unloaded.
2384 */
2385static void __exit bas_gigaset_exit(void)
2386{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002387 struct bas_cardstate *ucs = cardstate->hw.bas;
2388
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002389 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002390 * => no gigaset_start any more
2391 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002392
2393 gigaset_shutdown(cardstate);
2394 /* from now on, no isdn callback should be possible */
2395
Tilman Schmidt73a88812006-04-22 02:35:30 -07002396 /* close all still open channels */
2397 if (atomic_read(&ucs->basstate) & BS_B1OPEN) {
2398 gig_dbg(DEBUG_INIT, "closing B1 channel");
2399 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2400 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ, 0, 0,
2401 NULL, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002402 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002403 if (atomic_read(&ucs->basstate) & BS_B2OPEN) {
2404 gig_dbg(DEBUG_INIT, "closing B2 channel");
2405 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2406 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ, 0, 0,
2407 NULL, 0, BAS_TIMEOUT);
2408 }
2409 if (atomic_read(&ucs->basstate) & BS_ATOPEN) {
2410 gig_dbg(DEBUG_INIT, "closing AT channel");
2411 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2412 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ, 0, 0,
2413 NULL, 0, BAS_TIMEOUT);
2414 }
2415 atomic_set(&ucs->basstate, 0);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002416
2417 /* deregister this driver with the USB subsystem */
2418 usb_deregister(&gigaset_usb_driver);
2419 /* this will call the disconnect-callback */
2420 /* from now on, no disconnect/probe callback should be running */
2421
2422 gigaset_freecs(cardstate);
2423 cardstate = NULL;
2424 gigaset_freedriver(driver);
2425 driver = NULL;
2426}
2427
2428
2429module_init(bas_gigaset_init);
2430module_exit(bas_gigaset_exit);
2431
2432MODULE_AUTHOR(DRIVER_AUTHOR);
2433MODULE_DESCRIPTION(DRIVER_DESC);
2434MODULE_LICENSE("GPL");