blob: eb41aba3ddef4bdd72be0b2cbfc5f6aaf6ff8d1a [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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080068/*======================= local function prototypes =============================*/
69
70/* This function is called if a new device is connected to the USB port. It
71 * checks whether this new device belongs to this driver.
72 */
73static int gigaset_probe(struct usb_interface *interface,
74 const struct usb_device_id *id);
75
76/* Function will be called if the device is unplugged */
77static void gigaset_disconnect(struct usb_interface *interface);
78
Tilman Schmidt73a88812006-04-22 02:35:30 -070079static void read_ctrl_callback(struct urb *, struct pt_regs *);
80static void stopurbs(struct bas_bc_state *);
81static int atwrite_submit(struct cardstate *, unsigned char *, int);
82static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080083
84/*==============================================================================*/
85
86struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070087 struct usb_device *udev; /* USB device pointer */
88 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080089 unsigned char minor; /* starting minor number */
90
Tilman Schmidt784d5852006-04-10 22:55:04 -070091 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080092 struct usb_ctrlrequest dr_ctrl;
93 struct timer_list timer_ctrl; /* control request timeout */
94
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.
310 * argument:
311 * B channel control structure
312 */
313static inline void error_hangup(struct bc_state *bcs)
314{
315 struct cardstate *cs = bcs->cs;
316
Tilman Schmidt784d5852006-04-10 22:55:04 -0700317 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
318 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800319
Tilman Schmidt73a88812006-04-22 02:35:30 -0700320 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
321 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800322
323 gigaset_schedule_event(cs);
324}
325
326/* error_reset
327 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt73a88812006-04-22 02:35:30 -0700328 * This function may be called from any context, and should take care of
329 * scheduling the necessary actions for execution outside of interrupt context.
330 * Right now, it just generates a kernel message calling for help.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800331 * argument:
332 * controller state structure
333 */
334static inline void error_reset(struct cardstate *cs)
335{
336 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700337 dev_err(cs->dev,
338 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800339}
340
341/* check_pending
342 * check for completion of pending control request
343 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700344 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800345 */
346static void check_pending(struct bas_cardstate *ucs)
347{
348 unsigned long flags;
349
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800350 spin_lock_irqsave(&ucs->lock, flags);
351 switch (ucs->pending) {
352 case 0:
353 break;
354 case HD_OPEN_ATCHANNEL:
355 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
356 ucs->pending = 0;
357 break;
358 case HD_OPEN_B1CHANNEL:
359 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
360 ucs->pending = 0;
361 break;
362 case HD_OPEN_B2CHANNEL:
363 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
364 ucs->pending = 0;
365 break;
366 case HD_CLOSE_ATCHANNEL:
367 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
368 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800369 break;
370 case HD_CLOSE_B1CHANNEL:
371 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
372 ucs->pending = 0;
373 break;
374 case HD_CLOSE_B2CHANNEL:
375 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
376 ucs->pending = 0;
377 break;
378 case HD_DEVICE_INIT_ACK: /* no reply expected */
379 ucs->pending = 0;
380 break;
381 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
382 * are handled separately and should never end up here
383 */
384 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700385 dev_warn(&ucs->interface->dev,
386 "unknown pending request 0x%02x cleared\n",
387 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800388 ucs->pending = 0;
389 }
390
391 if (!ucs->pending)
392 del_timer(&ucs->timer_ctrl);
393
394 spin_unlock_irqrestore(&ucs->lock, flags);
395}
396
397/* cmd_in_timeout
398 * timeout routine for command input request
399 * argument:
400 * controller state structure
401 */
402static void cmd_in_timeout(unsigned long data)
403{
404 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700405 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800406
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800407 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700408 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800409 return;
410 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800411
Tilman Schmidt784d5852006-04-10 22:55:04 -0700412 dev_err(cs->dev, "timeout reading AT response\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800413 error_reset(cs); //FIXME retry?
414}
415
Tilman Schmidt73a88812006-04-22 02:35:30 -0700416/* set/clear bits in base connection state, return previous state
417 */
418inline static int update_basstate(struct bas_cardstate *ucs,
419 int set, int clear)
420{
421 unsigned long flags;
422 int state;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800423
Tilman Schmidt73a88812006-04-22 02:35:30 -0700424 spin_lock_irqsave(&ucs->lock, flags);
425 state = atomic_read(&ucs->basstate);
426 atomic_set(&ucs->basstate, (state & ~clear) | set);
427 spin_unlock_irqrestore(&ucs->lock, flags);
428 return state;
429}
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800430
431/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700432 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800433 * parameters:
434 * cs controller state structure
435 * timeout timeout in 1/10 sec., 0: none
436 * return value:
437 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800438 * -EBUSY if another request is pending
439 * any URB submission error code
440 */
441static int atread_submit(struct cardstate *cs, int timeout)
442{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700443 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800444 int ret;
445
Tilman Schmidt784d5852006-04-10 22:55:04 -0700446 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
447 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800448
Tilman Schmidt73a88812006-04-22 02:35:30 -0700449 if (update_basstate(ucs, BS_ATRDPEND, 0) & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700450 dev_err(cs->dev,
451 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800452 return -EBUSY;
453 }
454
455 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
456 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
457 ucs->dr_cmd_in.wValue = 0;
458 ucs->dr_cmd_in.wIndex = 0;
459 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
460 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700461 usb_rcvctrlpipe(ucs->udev, 0),
462 (unsigned char*) & ucs->dr_cmd_in,
463 ucs->rcvbuf, ucs->rcvbuf_size,
464 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800465
466 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700467 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700468 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
469 get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800470 return ret;
471 }
472
473 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700474 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800475 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
476 ucs->timer_cmd_in.data = (unsigned long) cs;
477 ucs->timer_cmd_in.function = cmd_in_timeout;
478 add_timer(&ucs->timer_cmd_in);
479 }
480 return 0;
481}
482
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800483/* read_int_callback
484 * USB completion handler for interrupt pipe input
485 * called by the USB subsystem in interrupt context
486 * parameter:
487 * urb USB request block
488 * urb->context = controller state structure
489 */
490static void read_int_callback(struct urb *urb, struct pt_regs *regs)
491{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700492 struct cardstate *cs = urb->context;
493 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800494 struct bc_state *bcs;
495 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700496 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800497 unsigned l;
498 int channel;
499
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800500 switch (urb->status) {
501 case 0: /* success */
502 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700503 case -ENOENT: /* cancelled */
504 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800505 case -EINPROGRESS: /* pending */
506 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700507 gig_dbg(DEBUG_USBREQ, "%s: %s",
508 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800509 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700510 case -ENODEV: /* device removed */
511 case -ESHUTDOWN: /* device shut down */
512 //FIXME use this as disconnect indicator?
513 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
514 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800515 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700516 dev_warn(cs->dev, "interrupt read: %s\n",
517 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800518 //FIXME corrective action? resubmission always ok?
519 goto resubmit;
520 }
521
Tilman Schmidt73a88812006-04-22 02:35:30 -0700522 /* drop incomplete packets even if the missing bytes wouldn't matter */
523 if (unlikely(urb->actual_length < 3)) {
524 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
525 urb->actual_length);
526 goto resubmit;
527 }
528
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800529 l = (unsigned) ucs->int_in_buf[1] +
530 (((unsigned) ucs->int_in_buf[2]) << 8);
531
Tilman Schmidt784d5852006-04-10 22:55:04 -0700532 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
533 urb->actual_length, (int)ucs->int_in_buf[0], l,
534 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800535
536 channel = 0;
537
538 switch (ucs->int_in_buf[0]) {
539 case HD_DEVICE_INIT_OK:
540 update_basstate(ucs, BS_INIT, 0);
541 break;
542
543 case HD_READY_SEND_ATDATA:
544 del_timer(&ucs->timer_atrdy);
545 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
546 start_cbsend(cs);
547 break;
548
549 case HD_OPEN_B2CHANNEL_ACK:
550 ++channel;
551 case HD_OPEN_B1CHANNEL_ACK:
552 bcs = cs->bcs + channel;
553 update_basstate(ucs, BS_B1OPEN << channel, 0);
554 gigaset_bchannel_up(bcs);
555 break;
556
557 case HD_OPEN_ATCHANNEL_ACK:
558 update_basstate(ucs, BS_ATOPEN, 0);
559 start_cbsend(cs);
560 break;
561
562 case HD_CLOSE_B2CHANNEL_ACK:
563 ++channel;
564 case HD_CLOSE_B1CHANNEL_ACK:
565 bcs = cs->bcs + channel;
566 update_basstate(ucs, 0, BS_B1OPEN << channel);
567 stopurbs(bcs->hw.bas);
568 gigaset_bchannel_down(bcs);
569 break;
570
571 case HD_CLOSE_ATCHANNEL_ACK:
572 update_basstate(ucs, 0, BS_ATOPEN);
573 break;
574
575 case HD_B2_FLOW_CONTROL:
576 ++channel;
577 case HD_B1_FLOW_CONTROL:
578 bcs = cs->bcs + channel;
579 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700580 &bcs->hw.bas->corrbytes);
581 gig_dbg(DEBUG_ISO,
582 "Flow control (channel %d, sub %d): 0x%02x => %d",
583 channel, bcs->hw.bas->numsub, l,
584 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800585 break;
586
587 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
588 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700589 dev_warn(cs->dev,
590 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800591 break;
592 }
593 spin_lock_irqsave(&cs->lock, flags);
594 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700595 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700596 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700597 "receive AT data overrun, %d bytes lost\n",
598 ucs->rcvbuf_size);
599 kfree(ucs->rcvbuf);
600 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800601 }
602 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
603 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700604 dev_err(cs->dev, "out of memory receiving AT data\n");
605 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800606 break;
607 }
608 ucs->rcvbuf_size = l;
609 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700610 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800611 kfree(ucs->rcvbuf);
612 ucs->rcvbuf = NULL;
613 ucs->rcvbuf_size = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700614 if (rc != -ENODEV)
615 //FIXME corrective action?
616 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800617 }
618 spin_unlock_irqrestore(&cs->lock, flags);
619 break;
620
621 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700622 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800623 break;
624
625 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700626 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800627 break;
628
629 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700630 dev_warn(cs->dev,
631 "unknown Gigaset signal 0x%02x (%u) ignored\n",
632 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800633 }
634
635 check_pending(ucs);
636
637resubmit:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700638 rc = usb_submit_urb(urb, SLAB_ATOMIC);
639 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700640 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700641 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800642 error_reset(cs);
643 }
644}
645
646/* read_ctrl_callback
647 * USB completion handler for control pipe input
648 * called by the USB subsystem in interrupt context
649 * parameter:
650 * urb USB request block
651 * urb->context = inbuf structure for controller state
652 */
653static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
654{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700655 struct inbuf_t *inbuf = urb->context;
656 struct cardstate *cs = inbuf->cs;
657 struct bas_cardstate *ucs = cs->hw.bas;
658 int have_data = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800659 unsigned numbytes;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700660 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800661
Tilman Schmidt73a88812006-04-22 02:35:30 -0700662 update_basstate(ucs, 0, BS_ATRDPEND);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800663
664 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700665 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800666 return;
667 }
668
669 del_timer(&ucs->timer_cmd_in);
670
671 switch (urb->status) {
672 case 0: /* normal completion */
673 numbytes = urb->actual_length;
674 if (unlikely(numbytes == 0)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700675 dev_warn(cs->dev,
676 "control read: empty block received\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800677 goto retry;
678 }
679 if (unlikely(numbytes != ucs->rcvbuf_size)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700680 dev_warn(cs->dev,
681 "control read: received %d chars, expected %d\n",
682 numbytes, ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800683 if (numbytes > ucs->rcvbuf_size)
684 numbytes = ucs->rcvbuf_size;
685 }
686
687 /* copy received bytes to inbuf */
688 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
689
690 if (unlikely(numbytes < ucs->rcvbuf_size)) {
691 /* incomplete - resubmit for remaining bytes */
692 ucs->rcvbuf_size -= numbytes;
693 ucs->retry_cmd_in = 0;
694 goto retry;
695 }
696 break;
697
Tilman Schmidt73a88812006-04-22 02:35:30 -0700698 case -ENOENT: /* cancelled */
699 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800700 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700701 case -ENODEV: /* device removed */
702 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800703 /* no action necessary */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700704 gig_dbg(DEBUG_USBREQ, "%s: %s",
705 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800706 break;
707
708 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700709 dev_warn(cs->dev, "control read: %s\n",
710 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800711 retry:
712 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700713 dev_notice(cs->dev, "control read: retry %d\n",
714 ucs->retry_cmd_in);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700715 rc = atread_submit(cs, BAS_TIMEOUT);
716 if (rc >= 0 || rc == -ENODEV)
717 /* resubmitted or disconnected */
718 /* - bypass regular exit block */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800719 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800720 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700721 dev_err(cs->dev,
722 "control read: giving up after %d tries\n",
723 ucs->retry_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800724 }
725 error_reset(cs);
726 }
727
728 kfree(ucs->rcvbuf);
729 ucs->rcvbuf = NULL;
730 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800731 if (have_data) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700732 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800733 gigaset_schedule_event(cs);
734 }
735}
736
737/* read_iso_callback
738 * USB completion handler for B channel isochronous input
739 * called by the USB subsystem in interrupt context
740 * parameter:
741 * urb USB request block of completed request
742 * urb->context = bc_state structure
743 */
744static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
745{
746 struct bc_state *bcs;
747 struct bas_bc_state *ubc;
748 unsigned long flags;
749 int i, rc;
750
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800751 /* status codes not worth bothering the tasklet with */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700752 if (unlikely(urb->status == -ENOENT ||
753 urb->status == -ECONNRESET ||
754 urb->status == -EINPROGRESS ||
755 urb->status == -ENODEV ||
756 urb->status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700757 gig_dbg(DEBUG_ISO, "%s: %s",
758 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800759 return;
760 }
761
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700762 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800763 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800764
765 spin_lock_irqsave(&ubc->isoinlock, flags);
766 if (likely(ubc->isoindone == NULL)) {
767 /* pass URB to tasklet */
768 ubc->isoindone = urb;
769 tasklet_schedule(&ubc->rcvd_tasklet);
770 } else {
771 /* tasklet still busy, drop data and resubmit URB */
772 ubc->loststatus = urb->status;
773 for (i = 0; i < BAS_NUMFRAMES; i++) {
774 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
775 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700776 urb->iso_frame_desc[i].status !=
777 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800778 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800779 urb->iso_frame_desc[i].status = 0;
780 urb->iso_frame_desc[i].actual_length = 0;
781 }
782 if (likely(atomic_read(&ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700783 /* urb->dev is clobbered by USB subsystem */
784 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800785 urb->transfer_flags = URB_ISO_ASAP;
786 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700787 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
788 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800789 rc = usb_submit_urb(urb, SLAB_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700790 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700791 dev_err(bcs->cs->dev,
792 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700793 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800794 dump_urb(DEBUG_ISO, "isoc read", urb);
795 error_hangup(bcs);
796 }
797 }
798 }
799 spin_unlock_irqrestore(&ubc->isoinlock, flags);
800}
801
802/* write_iso_callback
803 * USB completion handler for B channel isochronous output
804 * called by the USB subsystem in interrupt context
805 * parameter:
806 * urb USB request block of completed request
807 * urb->context = isow_urbctx_t structure
808 */
809static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
810{
811 struct isow_urbctx_t *ucx;
812 struct bas_bc_state *ubc;
813 unsigned long flags;
814
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800815 /* status codes not worth bothering the tasklet with */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700816 if (unlikely(urb->status == -ENOENT ||
817 urb->status == -ECONNRESET ||
818 urb->status == -EINPROGRESS ||
819 urb->status == -ENODEV ||
820 urb->status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700821 gig_dbg(DEBUG_ISO, "%s: %s",
822 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800823 return;
824 }
825
826 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700827 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800828 ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800829
830 spin_lock_irqsave(&ubc->isooutlock, flags);
831 ubc->isooutovfl = ubc->isooutdone;
832 ubc->isooutdone = ucx;
833 spin_unlock_irqrestore(&ubc->isooutlock, flags);
834 tasklet_schedule(&ubc->sent_tasklet);
835}
836
837/* starturbs
838 * prepare and submit USB request blocks for isochronous input and output
839 * argument:
840 * B channel control structure
841 * return value:
842 * 0 on success
843 * < 0 on error (no URBs submitted)
844 */
845static int starturbs(struct bc_state *bcs)
846{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700847 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800848 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800849 int j, k;
850 int rc;
851
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800852 /* initialize L2 reception */
853 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
854 bcs->inputstate |= INS_flag_hunt;
855
856 /* submit all isochronous input URBs */
857 atomic_set(&ubc->running, 1);
858 for (k = 0; k < BAS_INURBS; k++) {
859 urb = ubc->isoinurbs[k];
860 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800861 rc = -EFAULT;
862 goto error;
863 }
864
865 urb->dev = bcs->cs->hw.bas->udev;
866 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
867 urb->transfer_flags = URB_ISO_ASAP;
868 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
869 urb->transfer_buffer_length = BAS_INBUFSIZE;
870 urb->number_of_packets = BAS_NUMFRAMES;
871 urb->interval = BAS_FRAMETIME;
872 urb->complete = read_iso_callback;
873 urb->context = bcs;
874 for (j = 0; j < BAS_NUMFRAMES; j++) {
875 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
876 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
877 urb->iso_frame_desc[j].status = 0;
878 urb->iso_frame_desc[j].actual_length = 0;
879 }
880
881 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700882 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800883 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800884 }
885
886 /* initialize L2 transmission */
887 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
888
889 /* set up isochronous output URBs for flag idling */
890 for (k = 0; k < BAS_OUTURBS; ++k) {
891 urb = ubc->isoouturbs[k].urb;
892 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800893 rc = -EFAULT;
894 goto error;
895 }
896 urb->dev = bcs->cs->hw.bas->udev;
897 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
898 urb->transfer_flags = URB_ISO_ASAP;
899 urb->transfer_buffer = ubc->isooutbuf->data;
900 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
901 urb->number_of_packets = BAS_NUMFRAMES;
902 urb->interval = BAS_FRAMETIME;
903 urb->complete = write_iso_callback;
904 urb->context = &ubc->isoouturbs[k];
905 for (j = 0; j < BAS_NUMFRAMES; ++j) {
906 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
907 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
908 urb->iso_frame_desc[j].status = 0;
909 urb->iso_frame_desc[j].actual_length = 0;
910 }
911 ubc->isoouturbs[k].limit = -1;
912 }
913
914 /* submit two URBs, keep third one */
915 for (k = 0; k < 2; ++k) {
916 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
917 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700918 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800919 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800920 }
921 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
922 ubc->isooutfree = &ubc->isoouturbs[2];
923 ubc->isooutdone = ubc->isooutovfl = NULL;
924 return 0;
925 error:
926 stopurbs(ubc);
927 return rc;
928}
929
930/* stopurbs
931 * cancel the USB request blocks for isochronous input and output
932 * errors are silently ignored
933 * argument:
934 * B channel control structure
935 */
936static void stopurbs(struct bas_bc_state *ubc)
937{
938 int k, rc;
939
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800940 atomic_set(&ubc->running, 0);
941
942 for (k = 0; k < BAS_INURBS; ++k) {
943 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700944 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700945 "%s: isoc input URB %d unlinked, result = %s",
946 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800947 }
948
949 for (k = 0; k < BAS_OUTURBS; ++k) {
950 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700951 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700952 "%s: isoc output URB %d unlinked, result = %s",
953 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800954 }
955}
956
957/* Isochronous Write - Bottom Half */
958/* =============================== */
959
960/* submit_iso_write_urb
961 * fill and submit the next isochronous write URB
962 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700963 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800964 * return value:
965 * number of frames submitted in URB
966 * 0 if URB not submitted because no data available (isooutbuf busy)
967 * error code < 0 on error
968 */
969static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
970{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700971 struct urb *urb = ucx->urb;
972 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800973 struct usb_iso_packet_descriptor *ifd;
974 int corrbytes, nframe, rc;
975
Tilman Schmidt784d5852006-04-10 22:55:04 -0700976 /* urb->dev is clobbered by USB subsystem */
977 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800978 urb->transfer_flags = URB_ISO_ASAP;
979 urb->transfer_buffer = ubc->isooutbuf->data;
980 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
981
982 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
983 ifd = &urb->iso_frame_desc[nframe];
984
985 /* compute frame length according to flow control */
986 ifd->length = BAS_NORMFRAME;
987 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700988 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
989 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800990 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
991 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
992 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
993 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
994 ifd->length += corrbytes;
995 atomic_add(-corrbytes, &ubc->corrbytes);
996 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800997
998 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -0700999 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1000 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001001 if (ifd->offset < 0) {
1002 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001003 gig_dbg(DEBUG_ISO,
1004 "%s: buffer busy at frame %d",
1005 __func__, nframe);
1006 /* tasklet will be restarted from
1007 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001008 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001009 dev_err(ucx->bcs->cs->dev,
1010 "%s: buffer error %d at frame %d\n",
1011 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001012 return ifd->offset;
1013 }
1014 break;
1015 }
1016 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1017 ifd->status = 0;
1018 ifd->actual_length = 0;
1019 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001020 if (unlikely(nframe == 0))
1021 return 0; /* no data to send */
1022 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001023
Tilman Schmidt73a88812006-04-22 02:35:30 -07001024 rc = usb_submit_urb(urb, SLAB_ATOMIC);
1025 if (unlikely(rc)) {
1026 if (rc == -ENODEV)
1027 /* device removed - give up silently */
1028 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1029 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001030 dev_err(ucx->bcs->cs->dev,
1031 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001032 get_usb_rcmsg(rc));
1033 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001034 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001035 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001036 return nframe;
1037}
1038
1039/* write_iso_tasklet
1040 * tasklet scheduled when an isochronous output URB from the Gigaset device
1041 * has completed
1042 * parameter:
1043 * data B channel state structure
1044 */
1045static void write_iso_tasklet(unsigned long data)
1046{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001047 struct bc_state *bcs = (struct bc_state *) data;
1048 struct bas_bc_state *ubc = bcs->hw.bas;
1049 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001050 struct isow_urbctx_t *done, *next, *ovfl;
1051 struct urb *urb;
1052 struct usb_iso_packet_descriptor *ifd;
1053 int offset;
1054 unsigned long flags;
1055 int i;
1056 struct sk_buff *skb;
1057 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001058 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001059
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001060 /* loop while completed URBs arrive in time */
1061 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001062 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001063 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001064 return;
1065 }
1066
1067 /* retrieve completed URBs */
1068 spin_lock_irqsave(&ubc->isooutlock, flags);
1069 done = ubc->isooutdone;
1070 ubc->isooutdone = NULL;
1071 ovfl = ubc->isooutovfl;
1072 ubc->isooutovfl = NULL;
1073 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1074 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001075 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001076 error_hangup(bcs);
1077 break;
1078 }
1079 if (!done)
1080 break;
1081
1082 /* submit free URB if available */
1083 spin_lock_irqsave(&ubc->isooutlock, flags);
1084 next = ubc->isooutfree;
1085 ubc->isooutfree = NULL;
1086 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1087 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001088 rc = submit_iso_write_urb(next);
1089 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001090 /* could not submit URB, put it back */
1091 spin_lock_irqsave(&ubc->isooutlock, flags);
1092 if (ubc->isooutfree == NULL) {
1093 ubc->isooutfree = next;
1094 next = NULL;
1095 }
1096 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1097 if (next) {
1098 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001099 dev_err(cs->dev,
1100 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001101 error_hangup(bcs);
1102 }
1103 }
1104 }
1105
1106 /* process completed URB */
1107 urb = done->urb;
1108 switch (urb->status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001109 case -EXDEV: /* partial completion */
1110 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1111 __func__);
1112 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001113 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001114 /* inspect individual frames
1115 * assumptions (for lack of documentation):
1116 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001117 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001118 * - all following frames are not sent at all
1119 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001120 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001121 for (i = 0; i < BAS_NUMFRAMES; i++) {
1122 ifd = &urb->iso_frame_desc[i];
1123 if (ifd->status ||
1124 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001125 dev_warn(cs->dev,
1126 "isochronous write: frame %d: %s, "
1127 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001128 i, get_usb_statmsg(ifd->status),
1129 ifd->actual_length, ifd->length);
1130 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001131 ifd->actual_length)
1132 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001133 break;
1134 }
1135 }
1136#ifdef CONFIG_GIGASET_DEBUG
1137 /* check assumption on remaining frames */
1138 for (; i < BAS_NUMFRAMES; i++) {
1139 ifd = &urb->iso_frame_desc[i];
1140 if (ifd->status != -EINPROGRESS
1141 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001142 dev_warn(cs->dev,
1143 "isochronous write: frame %d: %s, "
1144 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001145 i, get_usb_statmsg(ifd->status),
1146 ifd->actual_length, ifd->length);
1147 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001148 ifd->actual_length)
1149 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001150 break;
1151 }
1152 }
1153#endif
1154 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001155 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001156 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001157 error_hangup(bcs);
1158 break;
1159 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001160 dev_warn(cs->dev, "isochronous write: %s\n",
1161 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001162 }
1163
1164 /* mark the write buffer area covered by this URB as free */
1165 if (done->limit >= 0)
1166 atomic_set(&ubc->isooutbuf->read, done->limit);
1167
1168 /* mark URB as free */
1169 spin_lock_irqsave(&ubc->isooutlock, flags);
1170 next = ubc->isooutfree;
1171 ubc->isooutfree = done;
1172 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1173 if (next) {
1174 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001175 rc = submit_iso_write_urb(next);
1176 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001177 /* couldn't submit */
1178 error_hangup(bcs);
1179 }
1180 }
1181 }
1182
1183 /* process queued SKBs */
1184 while ((skb = skb_dequeue(&bcs->squeue))) {
1185 /* copy to output buffer, doing L2 encapsulation */
1186 len = skb->len;
1187 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1188 /* insufficient buffer space, push back onto queue */
1189 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001190 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1191 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001192 break;
1193 }
1194 skb_pull(skb, len);
1195 gigaset_skb_sent(bcs, skb);
1196 dev_kfree_skb_any(skb);
1197 }
1198}
1199
1200/* Isochronous Read - Bottom Half */
1201/* ============================== */
1202
1203/* read_iso_tasklet
1204 * tasklet scheduled when an isochronous input URB from the Gigaset device
1205 * has completed
1206 * parameter:
1207 * data B channel state structure
1208 */
1209static void read_iso_tasklet(unsigned long data)
1210{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001211 struct bc_state *bcs = (struct bc_state *) data;
1212 struct bas_bc_state *ubc = bcs->hw.bas;
1213 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001214 struct urb *urb;
1215 char *rcvbuf;
1216 unsigned long flags;
1217 int totleft, numbytes, offset, frame, rc;
1218
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001219 /* loop while more completed URBs arrive in the meantime */
1220 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001221 /* retrieve URB */
1222 spin_lock_irqsave(&ubc->isoinlock, flags);
1223 if (!(urb = ubc->isoindone)) {
1224 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1225 return;
1226 }
1227 ubc->isoindone = NULL;
1228 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001229 dev_warn(cs->dev,
1230 "isochronous read overrun, "
1231 "dropped URB with status: %s, %d bytes lost\n",
1232 get_usb_statmsg(ubc->loststatus),
1233 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001234 ubc->loststatus = -EINPROGRESS;
1235 }
1236 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1237
1238 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001239 gig_dbg(DEBUG_ISO,
1240 "%s: channel not running, "
1241 "dropped URB with status: %s",
1242 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001243 return;
1244 }
1245
1246 switch (urb->status) {
1247 case 0: /* normal completion */
1248 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001249 case -EXDEV: /* inspect individual frames
1250 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001251 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1252 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001253 break;
1254 case -ENOENT:
1255 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001256 case -EINPROGRESS:
1257 gig_dbg(DEBUG_ISO, "%s: %s",
1258 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001259 continue; /* -> skip */
1260 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001261 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001262 error_hangup(bcs);
1263 continue; /* -> skip */
1264 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001265 dev_warn(cs->dev, "isochronous read: %s\n",
1266 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001267 goto error;
1268 }
1269
1270 rcvbuf = urb->transfer_buffer;
1271 totleft = urb->actual_length;
1272 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1273 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001274 dev_warn(cs->dev,
1275 "isochronous read: frame %d: %s\n",
1276 frame,
1277 get_usb_statmsg(
1278 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001279 break;
1280 }
1281 numbytes = urb->iso_frame_desc[frame].actual_length;
1282 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001283 dev_warn(cs->dev,
1284 "isochronous read: frame %d: "
1285 "numbytes (%d) > BAS_MAXFRAME\n",
1286 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001287 break;
1288 }
1289 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001290 dev_warn(cs->dev,
1291 "isochronous read: frame %d: "
1292 "numbytes (%d) > totleft (%d)\n",
1293 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001294 break;
1295 }
1296 offset = urb->iso_frame_desc[frame].offset;
1297 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001298 dev_warn(cs->dev,
1299 "isochronous read: frame %d: "
1300 "offset (%d) + numbytes (%d) "
1301 "> BAS_INBUFSIZE\n",
1302 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001303 break;
1304 }
1305 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1306 totleft -= numbytes;
1307 }
1308 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001309 dev_warn(cs->dev,
1310 "isochronous read: %d data bytes missing\n",
1311 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001312
1313 error:
1314 /* URB processed, resubmit */
1315 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1316 urb->iso_frame_desc[frame].status = 0;
1317 urb->iso_frame_desc[frame].actual_length = 0;
1318 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001319 /* urb->dev is clobbered by USB subsystem */
1320 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001321 urb->transfer_flags = URB_ISO_ASAP;
1322 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001323 rc = usb_submit_urb(urb, SLAB_ATOMIC);
1324 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001325 dev_err(cs->dev,
1326 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001327 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001328 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1329 error_hangup(bcs);
1330 }
1331 }
1332}
1333
1334/* Channel Operations */
1335/* ================== */
1336
1337/* req_timeout
1338 * timeout routine for control output request
1339 * argument:
1340 * B channel control structure
1341 */
1342static void req_timeout(unsigned long data)
1343{
1344 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001345 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001346 int pending;
1347 unsigned long flags;
1348
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001349 check_pending(ucs);
1350
1351 spin_lock_irqsave(&ucs->lock, flags);
1352 pending = ucs->pending;
1353 ucs->pending = 0;
1354 spin_unlock_irqrestore(&ucs->lock, flags);
1355
1356 switch (pending) {
1357 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001358 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001359 break;
1360
1361 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001362 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001363 error_reset(bcs->cs);
1364 break;
1365
1366 case HD_OPEN_B2CHANNEL:
1367 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001368 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1369 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001370 error_hangup(bcs);
1371 break;
1372
1373 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001374 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001375 break;
1376
1377 case HD_CLOSE_B2CHANNEL:
1378 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001379 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1380 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001381 break;
1382
1383 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001384 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1385 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001386 }
1387}
1388
1389/* write_ctrl_callback
1390 * USB completion handler for control pipe output
1391 * called by the USB subsystem in interrupt context
1392 * parameter:
1393 * urb USB request block of completed request
1394 * urb->context = hardware specific controller state structure
1395 */
1396static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1397{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001398 struct bas_cardstate *ucs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001399 unsigned long flags;
1400
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001401 spin_lock_irqsave(&ucs->lock, flags);
1402 if (urb->status && ucs->pending) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001403 dev_err(&ucs->interface->dev,
1404 "control request 0x%02x failed: %s\n",
1405 ucs->pending, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001406 del_timer(&ucs->timer_ctrl);
1407 ucs->pending = 0;
1408 }
1409 /* individual handling of specific request types */
1410 switch (ucs->pending) {
1411 case HD_DEVICE_INIT_ACK: /* no reply expected */
1412 ucs->pending = 0;
1413 break;
1414 }
1415 spin_unlock_irqrestore(&ucs->lock, flags);
1416}
1417
1418/* req_submit
1419 * submit a control output request without message buffer to the Gigaset base
1420 * and optionally start a timeout
1421 * parameters:
1422 * bcs B channel control structure
1423 * req control request code (HD_*)
1424 * val control request parameter value (set to 0 if unused)
1425 * timeout timeout in seconds (0: no timeout)
1426 * return value:
1427 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001428 * -EBUSY if another request is pending
1429 * any URB submission error code
1430 */
1431static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1432{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001433 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001434 int ret;
1435 unsigned long flags;
1436
Tilman Schmidt784d5852006-04-10 22:55:04 -07001437 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001438
1439 spin_lock_irqsave(&ucs->lock, flags);
1440 if (ucs->pending) {
1441 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001442 dev_err(bcs->cs->dev,
1443 "submission of request 0x%02x failed: "
1444 "request 0x%02x still pending\n",
1445 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001446 return -EBUSY;
1447 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001448
1449 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1450 ucs->dr_ctrl.bRequest = req;
1451 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1452 ucs->dr_ctrl.wIndex = 0;
1453 ucs->dr_ctrl.wLength = 0;
1454 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001455 usb_sndctrlpipe(ucs->udev, 0),
1456 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1457 write_ctrl_callback, ucs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001458 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001459 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1460 req, get_usb_statmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001461 spin_unlock_irqrestore(&ucs->lock, flags);
1462 return ret;
1463 }
1464 ucs->pending = req;
1465
1466 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001467 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001468 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1469 ucs->timer_ctrl.data = (unsigned long) bcs;
1470 ucs->timer_ctrl.function = req_timeout;
1471 add_timer(&ucs->timer_ctrl);
1472 }
1473
1474 spin_unlock_irqrestore(&ucs->lock, flags);
1475 return 0;
1476}
1477
1478/* gigaset_init_bchannel
1479 * called by common.c to connect a B channel
1480 * initialize isochronous I/O and tell the Gigaset base to open the channel
1481 * argument:
1482 * B channel control structure
1483 * return value:
1484 * 0 on success, error code < 0 on error
1485 */
1486static int gigaset_init_bchannel(struct bc_state *bcs)
1487{
1488 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001489 unsigned long flags;
1490
1491 spin_lock_irqsave(&bcs->cs->lock, flags);
1492 if (unlikely(!bcs->cs->connected)) {
1493 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1494 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1495 return -ENODEV;
1496 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001497
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001498 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001499 dev_err(bcs->cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001500 "could not start isochronous I/O for channel B%d: %s\n",
1501 bcs->channel + 1,
1502 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1503 if (ret != -ENODEV)
1504 error_hangup(bcs);
1505 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001506 return ret;
1507 }
1508
1509 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1510 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001511 dev_err(bcs->cs->dev, "could not open channel B%d\n",
1512 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001513 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001514 if (ret != -ENODEV)
1515 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001516 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001517
1518 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001519 return ret;
1520}
1521
1522/* gigaset_close_bchannel
1523 * called by common.c to disconnect a B channel
1524 * tell the Gigaset base to close the channel
1525 * stopping isochronous I/O and LL notification will be done when the
1526 * acknowledgement for the close arrives
1527 * argument:
1528 * B channel control structure
1529 * return value:
1530 * 0 on success, error code < 0 on error
1531 */
1532static int gigaset_close_bchannel(struct bc_state *bcs)
1533{
1534 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001535 unsigned long flags;
1536
1537 spin_lock_irqsave(&bcs->cs->lock, flags);
1538 if (unlikely(!bcs->cs->connected)) {
1539 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1540 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1541 return -ENODEV;
1542 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001543
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001544 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1545 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1546 /* channel not running: just signal common.c */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001547 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001548 gigaset_bchannel_down(bcs);
1549 return 0;
1550 }
1551
Tilman Schmidt73a88812006-04-22 02:35:30 -07001552 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001553 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1554 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidt73a88812006-04-22 02:35:30 -07001555 dev_err(bcs->cs->dev, "closing channel B%d failed\n",
1556 bcs->channel + 1);
1557
1558 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001559 return ret;
1560}
1561
1562/* Device Operations */
1563/* ================= */
1564
1565/* complete_cb
1566 * unqueue first command buffer from queue, waking any sleepers
1567 * must be called with cs->cmdlock held
1568 * parameter:
1569 * cs controller state structure
1570 */
1571static void complete_cb(struct cardstate *cs)
1572{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001573 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001574
1575 /* unqueue completed buffer */
1576 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001577 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1578 "write_command: sent %u bytes, %u left",
1579 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001580 if ((cs->cmdbuf = cb->next) != NULL) {
1581 cs->cmdbuf->prev = NULL;
1582 cs->curlen = cs->cmdbuf->len;
1583 } else {
1584 cs->lastcmdbuf = NULL;
1585 cs->curlen = 0;
1586 }
1587
1588 if (cb->wake_tasklet)
1589 tasklet_schedule(cb->wake_tasklet);
1590
1591 kfree(cb);
1592}
1593
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001594/* write_command_callback
1595 * USB completion handler for AT command transmission
1596 * called by the USB subsystem in interrupt context
1597 * parameter:
1598 * urb USB request block of completed request
1599 * urb->context = controller state structure
1600 */
1601static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1602{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001603 struct cardstate *cs = urb->context;
1604 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001605 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001606
Tilman Schmidt73a88812006-04-22 02:35:30 -07001607 update_basstate(ucs, 0, BS_ATWRPEND);
1608
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001609 /* check status */
1610 switch (urb->status) {
1611 case 0: /* normal completion */
1612 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001613 case -ENOENT: /* cancelled */
1614 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001615 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001616 case -ENODEV: /* device removed */
1617 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001618 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001619 gig_dbg(DEBUG_USBREQ, "%s: %s",
1620 __func__, get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001621 return;
1622 default: /* any failure */
1623 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001624 dev_warn(cs->dev,
1625 "command write: %s, "
1626 "giving up after %d retries\n",
1627 get_usb_statmsg(urb->status),
1628 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001629 break;
1630 }
1631 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001632 dev_warn(cs->dev,
1633 "command write: %s, "
1634 "cannot retry - cmdbuf gone\n",
1635 get_usb_statmsg(urb->status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001636 break;
1637 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001638 dev_notice(cs->dev, "command write: %s, retry %d\n",
1639 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001640 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1641 /* resubmitted - bypass regular exit block */
1642 return;
1643 /* command send failed, assume base still waiting */
1644 update_basstate(ucs, BS_ATREADY, 0);
1645 }
1646
1647 spin_lock_irqsave(&cs->cmdlock, flags);
1648 if (cs->cmdbuf != NULL)
1649 complete_cb(cs);
1650 spin_unlock_irqrestore(&cs->cmdlock, flags);
1651}
1652
1653/* atrdy_timeout
1654 * timeout routine for AT command transmission
1655 * argument:
1656 * controller state structure
1657 */
1658static void atrdy_timeout(unsigned long data)
1659{
1660 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001661 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001662
Tilman Schmidt784d5852006-04-10 22:55:04 -07001663 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001664
1665 /* fake the missing signal - what else can I do? */
1666 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1667 start_cbsend(cs);
1668}
1669
1670/* atwrite_submit
1671 * submit an HD_WRITE_ATMESSAGE command URB
1672 * parameters:
1673 * cs controller state structure
1674 * buf buffer containing command to send
1675 * len length of command to send
1676 * return value:
1677 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001678 * -EBUSY if another request is pending
1679 * any URB submission error code
1680 */
1681static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1682{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001683 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001684 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001685
Tilman Schmidt784d5852006-04-10 22:55:04 -07001686 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001687
Tilman Schmidt73a88812006-04-22 02:35:30 -07001688 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001689 dev_err(cs->dev,
1690 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001691 return -EBUSY;
1692 }
1693
1694 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1695 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1696 ucs->dr_cmd_out.wValue = 0;
1697 ucs->dr_cmd_out.wIndex = 0;
1698 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1699 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1700 usb_sndctrlpipe(ucs->udev, 0),
1701 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1702 write_command_callback, cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001703 rc = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC);
1704 if (unlikely(rc)) {
1705 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001706 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001707 get_usb_rcmsg(rc));
1708 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001709 }
1710
Tilman Schmidt73a88812006-04-22 02:35:30 -07001711 /* submitted successfully, start timeout if necessary */
1712 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001713 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1714 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001715 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1716 ucs->timer_atrdy.data = (unsigned long) cs;
1717 ucs->timer_atrdy.function = atrdy_timeout;
1718 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001719 }
1720 return 0;
1721}
1722
1723/* start_cbsend
1724 * start transmission of AT command queue if necessary
1725 * parameter:
1726 * cs controller state structure
1727 * return value:
1728 * 0 on success
1729 * error code < 0 on error
1730 */
1731static int start_cbsend(struct cardstate *cs)
1732{
1733 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001734 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001735 unsigned long flags;
1736 int rc;
1737 int retval = 0;
1738
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001739 /* check if AT channel is open */
1740 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001741 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001742 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1743 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001744 /* flush command queue */
1745 spin_lock_irqsave(&cs->cmdlock, flags);
1746 while (cs->cmdbuf != NULL)
1747 complete_cb(cs);
1748 spin_unlock_irqrestore(&cs->cmdlock, flags);
1749 }
1750 return rc;
1751 }
1752
1753 /* try to send first command in queue */
1754 spin_lock_irqsave(&cs->cmdlock, flags);
1755
1756 while ((cb = cs->cmdbuf) != NULL &&
1757 atomic_read(&ucs->basstate) & BS_ATREADY) {
1758 ucs->retry_cmd_out = 0;
1759 rc = atwrite_submit(cs, cb->buf, cb->len);
1760 if (unlikely(rc)) {
1761 retval = rc;
1762 complete_cb(cs);
1763 }
1764 }
1765
1766 spin_unlock_irqrestore(&cs->cmdlock, flags);
1767 return retval;
1768}
1769
1770/* gigaset_write_cmd
1771 * This function is called by the device independent part of the driver
1772 * to transmit an AT command string to the Gigaset device.
1773 * It encapsulates the device specific method for transmission over the
1774 * direct USB connection to the base.
1775 * The command string is added to the queue of commands to send, and
1776 * USB transmission is started if necessary.
1777 * parameters:
1778 * cs controller state structure
1779 * buf command string to send
1780 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001781 * wake_tasklet tasklet to run when transmission is completed
1782 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001783 * return value:
1784 * number of bytes queued on success
1785 * error code < 0 on error
1786 */
1787static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001788 const unsigned char *buf, int len,
1789 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001790{
1791 struct cmdbuf_t *cb;
1792 unsigned long flags;
1793 int status;
1794
1795 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001796 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001797 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001798
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001799 if (len <= 0)
1800 return 0; /* nothing to do */
1801
1802 if (len > IF_WRITEBUF)
1803 len = IF_WRITEBUF;
1804 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001805 dev_err(cs->dev, "%s: out of memory\n", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001806 return -ENOMEM;
1807 }
1808
1809 memcpy(cb->buf, buf, len);
1810 cb->len = len;
1811 cb->offset = 0;
1812 cb->next = NULL;
1813 cb->wake_tasklet = wake_tasklet;
1814
1815 spin_lock_irqsave(&cs->cmdlock, flags);
1816 cb->prev = cs->lastcmdbuf;
1817 if (cs->lastcmdbuf)
1818 cs->lastcmdbuf->next = cb;
1819 else {
1820 cs->cmdbuf = cb;
1821 cs->curlen = len;
1822 }
1823 cs->cmdbytes += len;
1824 cs->lastcmdbuf = cb;
1825 spin_unlock_irqrestore(&cs->cmdlock, flags);
1826
Tilman Schmidt73a88812006-04-22 02:35:30 -07001827 spin_lock_irqsave(&cs->lock, flags);
1828 if (unlikely(!cs->connected)) {
1829 spin_unlock_irqrestore(&cs->lock, flags);
1830 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1831 return -ENODEV;
1832 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001833 status = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001834 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001835 return status < 0 ? status : len;
1836}
1837
1838/* gigaset_write_room
1839 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001840 * return number of characters the driver will accept to be written via
1841 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001842 * parameter:
1843 * controller state structure
1844 * return value:
1845 * number of characters
1846 */
1847static int gigaset_write_room(struct cardstate *cs)
1848{
1849 return IF_WRITEBUF;
1850}
1851
1852/* gigaset_chars_in_buffer
1853 * tty_driver.chars_in_buffer interface routine
1854 * return number of characters waiting to be sent
1855 * parameter:
1856 * controller state structure
1857 * return value:
1858 * number of characters
1859 */
1860static int gigaset_chars_in_buffer(struct cardstate *cs)
1861{
1862 unsigned long flags;
1863 unsigned bytes;
1864
1865 spin_lock_irqsave(&cs->cmdlock, flags);
1866 bytes = cs->cmdbytes;
1867 spin_unlock_irqrestore(&cs->cmdlock, flags);
1868
1869 return bytes;
1870}
1871
1872/* gigaset_brkchars
1873 * implementation of ioctl(GIGASET_BRKCHARS)
1874 * parameter:
1875 * controller state structure
1876 * return value:
1877 * -EINVAL (unimplemented function)
1878 */
1879static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1880{
1881 return -EINVAL;
1882}
1883
1884
1885/* Device Initialization/Shutdown */
1886/* ============================== */
1887
1888/* Free hardware dependent part of the B channel structure
1889 * parameter:
1890 * bcs B channel structure
1891 * return value:
1892 * !=0 on success
1893 */
1894static int gigaset_freebcshw(struct bc_state *bcs)
1895{
Tilman Schmidt73a88812006-04-22 02:35:30 -07001896 struct bas_bc_state *ubc = bcs->hw.bas;
1897 int i;
1898
1899 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001900 return 0;
1901
Tilman Schmidt73a88812006-04-22 02:35:30 -07001902 /* kill URBs and tasklets before freeing - better safe than sorry */
1903 atomic_set(&ubc->running, 0);
1904 for (i = 0; i < BAS_OUTURBS; ++i)
1905 if (ubc->isoouturbs[i].urb) {
1906 gig_dbg(DEBUG_INIT, "%s: killing iso out URB %d",
1907 __func__, i);
1908 usb_kill_urb(ubc->isoouturbs[i].urb);
1909 usb_free_urb(ubc->isoouturbs[i].urb);
1910 }
1911 for (i = 0; i < BAS_INURBS; ++i)
1912 if (ubc->isoinurbs[i]) {
1913 gig_dbg(DEBUG_INIT, "%s: killing iso in URB %d",
1914 __func__, i);
1915 usb_kill_urb(ubc->isoinurbs[i]);
1916 usb_free_urb(ubc->isoinurbs[i]);
1917 }
1918 tasklet_kill(&ubc->sent_tasklet);
1919 tasklet_kill(&ubc->rcvd_tasklet);
1920 kfree(ubc->isooutbuf);
1921 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001922 bcs->hw.bas = NULL;
1923 return 1;
1924}
1925
1926/* Initialize hardware dependent part of the B channel structure
1927 * parameter:
1928 * bcs B channel structure
1929 * return value:
1930 * !=0 on success
1931 */
1932static int gigaset_initbcshw(struct bc_state *bcs)
1933{
1934 int i;
1935 struct bas_bc_state *ubc;
1936
1937 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1938 if (!ubc) {
1939 err("could not allocate bas_bc_state");
1940 return 0;
1941 }
1942
1943 atomic_set(&ubc->running, 0);
1944 atomic_set(&ubc->corrbytes, 0);
1945 spin_lock_init(&ubc->isooutlock);
1946 for (i = 0; i < BAS_OUTURBS; ++i) {
1947 ubc->isoouturbs[i].urb = NULL;
1948 ubc->isoouturbs[i].bcs = bcs;
1949 }
1950 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1951 ubc->numsub = 0;
1952 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
1953 err("could not allocate isochronous output buffer");
1954 kfree(ubc);
1955 bcs->hw.bas = NULL;
1956 return 0;
1957 }
1958 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001959 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001960
1961 spin_lock_init(&ubc->isoinlock);
1962 for (i = 0; i < BAS_INURBS; ++i)
1963 ubc->isoinurbs[i] = NULL;
1964 ubc->isoindone = NULL;
1965 ubc->loststatus = -EINPROGRESS;
1966 ubc->isoinlost = 0;
1967 ubc->seqlen = 0;
1968 ubc->inbyte = 0;
1969 ubc->inbits = 0;
1970 ubc->goodbytes = 0;
1971 ubc->alignerrs = 0;
1972 ubc->fcserrs = 0;
1973 ubc->frameerrs = 0;
1974 ubc->giants = 0;
1975 ubc->runts = 0;
1976 ubc->aborts = 0;
1977 ubc->shared0s = 0;
1978 ubc->stolen0s = 0;
1979 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001980 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001981 return 1;
1982}
1983
1984static void gigaset_reinitbcshw(struct bc_state *bcs)
1985{
1986 struct bas_bc_state *ubc = bcs->hw.bas;
1987
1988 atomic_set(&bcs->hw.bas->running, 0);
1989 atomic_set(&bcs->hw.bas->corrbytes, 0);
1990 bcs->hw.bas->numsub = 0;
1991 spin_lock_init(&ubc->isooutlock);
1992 spin_lock_init(&ubc->isoinlock);
1993 ubc->loststatus = -EINPROGRESS;
1994}
1995
1996static void gigaset_freecshw(struct cardstate *cs)
1997{
Tilman Schmidt73a88812006-04-22 02:35:30 -07001998 /* timers, URBs and rcvbuf are disposed of in disconnect */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001999 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002000 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002001}
2002
2003static int gigaset_initcshw(struct cardstate *cs)
2004{
2005 struct bas_cardstate *ucs;
2006
2007 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2008 if (!ucs)
2009 return 0;
2010
2011 ucs->urb_cmd_in = NULL;
2012 ucs->urb_cmd_out = NULL;
2013 ucs->rcvbuf = NULL;
2014 ucs->rcvbuf_size = 0;
2015
2016 spin_lock_init(&ucs->lock);
2017 ucs->pending = 0;
2018
2019 atomic_set(&ucs->basstate, 0);
2020 init_timer(&ucs->timer_ctrl);
2021 init_timer(&ucs->timer_atrdy);
2022 init_timer(&ucs->timer_cmd_in);
2023
2024 return 1;
2025}
2026
2027/* freeurbs
2028 * unlink and deallocate all URBs unconditionally
2029 * caller must make sure that no commands are still in progress
2030 * parameter:
2031 * cs controller state structure
2032 */
2033static void freeurbs(struct cardstate *cs)
2034{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002035 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002036 struct bas_bc_state *ubc;
2037 int i, j;
2038
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002039 for (j = 0; j < 2; ++j) {
2040 ubc = cs->bcs[j].hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002041 for (i = 0; i < BAS_OUTURBS; ++i)
2042 if (ubc->isoouturbs[i].urb) {
2043 usb_kill_urb(ubc->isoouturbs[i].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002044 gig_dbg(DEBUG_INIT,
2045 "%s: isoc output URB %d/%d unlinked",
2046 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002047 usb_free_urb(ubc->isoouturbs[i].urb);
2048 ubc->isoouturbs[i].urb = NULL;
2049 }
2050 for (i = 0; i < BAS_INURBS; ++i)
2051 if (ubc->isoinurbs[i]) {
2052 usb_kill_urb(ubc->isoinurbs[i]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002053 gig_dbg(DEBUG_INIT,
2054 "%s: isoc input URB %d/%d unlinked",
2055 __func__, j, i);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002056 usb_free_urb(ubc->isoinurbs[i]);
2057 ubc->isoinurbs[i] = NULL;
2058 }
2059 }
2060 if (ucs->urb_int_in) {
2061 usb_kill_urb(ucs->urb_int_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002062 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2063 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002064 usb_free_urb(ucs->urb_int_in);
2065 ucs->urb_int_in = NULL;
2066 }
2067 if (ucs->urb_cmd_out) {
2068 usb_kill_urb(ucs->urb_cmd_out);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002069 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2070 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002071 usb_free_urb(ucs->urb_cmd_out);
2072 ucs->urb_cmd_out = NULL;
2073 }
2074 if (ucs->urb_cmd_in) {
2075 usb_kill_urb(ucs->urb_cmd_in);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002076 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2077 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002078 usb_free_urb(ucs->urb_cmd_in);
2079 ucs->urb_cmd_in = NULL;
2080 }
2081 if (ucs->urb_ctrl) {
2082 usb_kill_urb(ucs->urb_ctrl);
Tilman Schmidt784d5852006-04-10 22:55:04 -07002083 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2084 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002085 usb_free_urb(ucs->urb_ctrl);
2086 ucs->urb_ctrl = NULL;
2087 }
2088}
2089
2090/* gigaset_probe
2091 * This function is called when a new USB device is connected.
2092 * It checks whether the new device is handled by this driver.
2093 */
2094static int gigaset_probe(struct usb_interface *interface,
2095 const struct usb_device_id *id)
2096{
2097 struct usb_host_interface *hostif;
2098 struct usb_device *udev = interface_to_usbdev(interface);
2099 struct cardstate *cs = NULL;
2100 struct bas_cardstate *ucs = NULL;
2101 struct bas_bc_state *ubc;
2102 struct usb_endpoint_descriptor *endpoint;
2103 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002104 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002105
Tilman Schmidt784d5852006-04-10 22:55:04 -07002106 gig_dbg(DEBUG_ANY,
2107 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2108 __func__, le16_to_cpu(udev->descriptor.idVendor),
2109 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002110
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002111 /* set required alternate setting */
2112 hostif = interface->cur_altsetting;
2113 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002114 gig_dbg(DEBUG_ANY,
2115 "%s: wrong alternate setting %d - trying to switch",
2116 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002117 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002118 dev_warn(&udev->dev, "usb_set_interface failed, "
2119 "device %d interface %d altsetting %d\n",
2120 udev->devnum, hostif->desc.bInterfaceNumber,
2121 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002122 return -ENODEV;
2123 }
2124 hostif = interface->cur_altsetting;
2125 }
2126
2127 /* Reject application specific interfaces
2128 */
2129 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002130 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2131 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002132 return -ENODEV;
2133 }
2134
Tilman Schmidt784d5852006-04-10 22:55:04 -07002135 dev_info(&udev->dev,
2136 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2137 __func__, le16_to_cpu(udev->descriptor.idVendor),
2138 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002139
2140 cs = gigaset_getunassignedcs(driver);
2141 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002142 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002143 return -ENODEV;
2144 }
2145 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002146
2147 /* save off device structure ptrs for later use */
2148 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002149 ucs->udev = udev;
2150 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002151 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002152
2153 /* allocate URBs:
2154 * - one for the interrupt pipe
2155 * - three for the different uses of the default control pipe
2156 * - three for each isochronous pipe
2157 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002158 if (!(ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL)) ||
2159 !(ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL)) ||
2160 !(ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL)) ||
2161 !(ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL)))
2162 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002163
2164 for (j = 0; j < 2; ++j) {
2165 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002166 for (i = 0; i < BAS_OUTURBS; ++i)
2167 if (!(ubc->isoouturbs[i].urb =
2168 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL)))
2169 goto allocerr;
2170 for (i = 0; i < BAS_INURBS; ++i)
2171 if (!(ubc->isoinurbs[i] =
2172 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL)))
2173 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002174 }
2175
2176 ucs->rcvbuf = NULL;
2177 ucs->rcvbuf_size = 0;
2178
2179 /* Fill the interrupt urb and send it to the core */
2180 endpoint = &hostif->endpoint[0].desc;
2181 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002182 usb_rcvintpipe(udev,
2183 (endpoint->bEndpointAddress) & 0x0f),
2184 ucs->int_in_buf, 3, read_int_callback, cs,
2185 endpoint->bInterval);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002186 if ((rc = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002187 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002188 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002189 goto error;
2190 }
2191
2192 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002193 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002194 goto error;
2195
2196 /* tell common part that the device is ready */
2197 if (startmode == SM_LOCKED)
2198 atomic_set(&cs->mstate, MS_LOCKED);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002199
2200 /* save address of controller structure */
2201 usb_set_intfdata(interface, cs);
2202
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002203 if (!gigaset_start(cs))
2204 goto error;
2205
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002206 return 0;
2207
Tilman Schmidt73a88812006-04-22 02:35:30 -07002208allocerr:
2209 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002210error:
2211 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002212 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002213 gigaset_unassign(cs);
2214 return -ENODEV;
2215}
2216
2217/* gigaset_disconnect
2218 * This function is called when the Gigaset base is unplugged.
2219 */
2220static void gigaset_disconnect(struct usb_interface *interface)
2221{
2222 struct cardstate *cs;
2223 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002224 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002225
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002226 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002227
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002228 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002229
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002230 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002231
2232 /* mark base as not ready, all channels disconnected */
2233 atomic_set(&ucs->basstate, 0);
2234
2235 /* tell LL all channels are down */
2236 //FIXME shouldn't gigaset_stop() do this?
2237 for (j = 0; j < 2; ++j)
2238 gigaset_bchannel_down(cs->bcs + j);
2239
2240 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002241 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002242
2243 /* stop timers and URBs, free ressources */
2244 del_timer_sync(&ucs->timer_ctrl);
2245 del_timer_sync(&ucs->timer_atrdy);
2246 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002247 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002248 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002249 kfree(ucs->rcvbuf);
2250 ucs->rcvbuf = NULL;
2251 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002252 usb_put_dev(ucs->udev);
2253 ucs->interface = NULL;
2254 ucs->udev = NULL;
2255 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002256 gigaset_unassign(cs);
2257}
2258
2259static struct gigaset_ops gigops = {
2260 gigaset_write_cmd,
2261 gigaset_write_room,
2262 gigaset_chars_in_buffer,
2263 gigaset_brkchars,
2264 gigaset_init_bchannel,
2265 gigaset_close_bchannel,
2266 gigaset_initbcshw,
2267 gigaset_freebcshw,
2268 gigaset_reinitbcshw,
2269 gigaset_initcshw,
2270 gigaset_freecshw,
2271 gigaset_set_modem_ctrl,
2272 gigaset_baud_rate,
2273 gigaset_set_line_ctrl,
2274 gigaset_isoc_send_skb,
2275 gigaset_isoc_input,
2276};
2277
2278/* bas_gigaset_init
2279 * This function is called after the kernel module is loaded.
2280 */
2281static int __init bas_gigaset_init(void)
2282{
2283 int result;
2284
2285 /* allocate memory for our driver state and intialize it */
2286 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002287 GIGASET_MODULENAME, GIGASET_DEVNAME,
2288 GIGASET_DEVFSNAME, &gigops,
2289 THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002290 goto error;
2291
2292 /* allocate memory for our device state and intialize it */
Tilman Schmidt917f5082006-04-10 22:55:00 -07002293 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2294 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002295 if (!cardstate)
2296 goto error;
2297
2298 /* register this driver with the USB subsystem */
2299 result = usb_register(&gigaset_usb_driver);
2300 if (result < 0) {
2301 err("usb_register failed (error %d)", -result);
2302 goto error;
2303 }
2304
2305 info(DRIVER_AUTHOR);
2306 info(DRIVER_DESC);
2307 return 0;
2308
2309error: if (cardstate)
2310 gigaset_freecs(cardstate);
2311 cardstate = NULL;
2312 if (driver)
2313 gigaset_freedriver(driver);
2314 driver = NULL;
2315 return -1;
2316}
2317
2318/* bas_gigaset_exit
2319 * This function is called before the kernel module is unloaded.
2320 */
2321static void __exit bas_gigaset_exit(void)
2322{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002323 struct bas_cardstate *ucs = cardstate->hw.bas;
2324
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002325 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002326 * => no gigaset_start any more
2327 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002328
2329 gigaset_shutdown(cardstate);
2330 /* from now on, no isdn callback should be possible */
2331
Tilman Schmidt73a88812006-04-22 02:35:30 -07002332 /* close all still open channels */
2333 if (atomic_read(&ucs->basstate) & BS_B1OPEN) {
2334 gig_dbg(DEBUG_INIT, "closing B1 channel");
2335 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2336 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ, 0, 0,
2337 NULL, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002338 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002339 if (atomic_read(&ucs->basstate) & BS_B2OPEN) {
2340 gig_dbg(DEBUG_INIT, "closing B2 channel");
2341 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2342 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ, 0, 0,
2343 NULL, 0, BAS_TIMEOUT);
2344 }
2345 if (atomic_read(&ucs->basstate) & BS_ATOPEN) {
2346 gig_dbg(DEBUG_INIT, "closing AT channel");
2347 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2348 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ, 0, 0,
2349 NULL, 0, BAS_TIMEOUT);
2350 }
2351 atomic_set(&ucs->basstate, 0);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002352
2353 /* deregister this driver with the USB subsystem */
2354 usb_deregister(&gigaset_usb_driver);
2355 /* this will call the disconnect-callback */
2356 /* from now on, no disconnect/probe callback should be running */
2357
2358 gigaset_freecs(cardstate);
2359 cardstate = NULL;
2360 gigaset_freedriver(driver);
2361 driver = NULL;
2362}
2363
2364
2365module_init(bas_gigaset_init);
2366module_exit(bas_gigaset_exit);
2367
2368MODULE_AUTHOR(DRIVER_AUTHOR);
2369MODULE_DESCRIPTION(DRIVER_DESC);
2370MODULE_LICENSE("GPL");