blob: 0302c40a27ea39c7e126c0024630ba641d5e21ea [file] [log] [blame]
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001/*
2 * USB driver for Gigaset 307x base via direct USB connection.
3 *
4 * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
5 * Tilman Schmidt <tilman@imap.cc>,
Tilman Schmidt70440cf2006-04-10 22:55:14 -07006 * Stefan Eilers.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08007 *
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08008 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080014 */
15
16#include "gigaset.h"
17
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/timer.h>
22#include <linux/usb.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25
26/* Version Information */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070027#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080028#define DRIVER_DESC "USB Driver for Gigaset 307x"
29
30
31/* Module parameters */
32
33static int startmode = SM_ISDN;
34static int cidmode = 1;
35
36module_param(startmode, int, S_IRUGO);
37module_param(cidmode, int, S_IRUGO);
38MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
39MODULE_PARM_DESC(cidmode, "Call-ID mode");
40
41#define GIGASET_MINORS 1
42#define GIGASET_MINOR 16
43#define GIGASET_MODULENAME "bas_gigaset"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080044#define GIGASET_DEVNAME "ttyGB"
45
Tilman Schmidt73a88812006-04-22 02:35:30 -070046/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
47#define IF_WRITEBUF 264
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080048
49/* Values for the Gigaset 307x */
50#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070051#define USB_3070_PRODUCT_ID 0x0001
52#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080053#define USB_SX303_PRODUCT_ID 0x0021
54#define USB_SX353_PRODUCT_ID 0x0022
55
56/* table of devices that work with this driver */
Tilman Schmidt35dc8452007-03-29 01:20:34 -070057static const struct usb_device_id gigaset_table [] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070058 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080060 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
62 { } /* Terminating entry */
63};
64
65MODULE_DEVICE_TABLE(usb, gigaset_table);
66
Tilman Schmidt06163f82006-06-26 00:25:33 -070067/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080068
Tilman Schmidt06163f82006-06-26 00:25:33 -070069/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080070static int gigaset_probe(struct usb_interface *interface,
71 const struct usb_device_id *id);
72
73/* Function will be called if the device is unplugged */
74static void gigaset_disconnect(struct usb_interface *interface);
75
Tilman Schmidt06163f82006-06-26 00:25:33 -070076static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070077static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070078static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070079static int atwrite_submit(struct cardstate *, unsigned char *, int);
80static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080081
Tilman Schmidt06163f82006-06-26 00:25:33 -070082/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080083
84struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070085 struct usb_device *udev; /* USB device pointer */
86 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080087 unsigned char minor; /* starting minor number */
88
Tilman Schmidt784d5852006-04-10 22:55:04 -070089 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080090 struct usb_ctrlrequest dr_ctrl;
91 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -070092 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080093
94 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -070095 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080096 struct usb_ctrlrequest dr_cmd_out;
97 int retry_cmd_out;
98
Tilman Schmidt784d5852006-04-10 22:55:04 -070099 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800100 struct usb_ctrlrequest dr_cmd_in;
101 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700102 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800103
Tilman Schmidt784d5852006-04-10 22:55:04 -0700104 struct urb *urb_int_in; /* URB for interrupt pipe */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800105 unsigned char int_in_buf[3];
106
107 spinlock_t lock; /* locks all following */
108 atomic_t basstate; /* bitmap (BS_*) */
109 int pending; /* uncompleted base request */
110 int rcvbuf_size; /* size of AT receive buffer */
111 /* 0: no receive in progress */
112 int retry_cmd_in; /* receive req retry count */
113};
114
115/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700116#define BS_ATOPEN 0x001 /* AT channel open */
117#define BS_B1OPEN 0x002 /* B channel 1 open */
118#define BS_B2OPEN 0x004 /* B channel 2 open */
119#define BS_ATREADY 0x008 /* base ready for AT command */
120#define BS_INIT 0x010 /* base has signalled INIT_OK */
121#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
122#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
123#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800124
125
126static struct gigaset_driver *driver = NULL;
127static struct cardstate *cardstate = NULL;
128
129/* usb specific object needed to register this driver with the usb subsystem */
130static struct usb_driver gigaset_usb_driver = {
131 .name = GIGASET_MODULENAME,
132 .probe = gigaset_probe,
133 .disconnect = gigaset_disconnect,
134 .id_table = gigaset_table,
135};
136
Tilman Schmidt73a88812006-04-22 02:35:30 -0700137/* get message text for usb_submit_urb return code
138 */
139static char *get_usb_rcmsg(int rc)
140{
141 static char unkmsg[28];
142
143 switch (rc) {
144 case 0:
145 return "success";
146 case -ENOMEM:
147 return "out of memory";
148 case -ENODEV:
149 return "device not present";
150 case -ENOENT:
151 return "endpoint not present";
152 case -ENXIO:
153 return "URB type not supported";
154 case -EINVAL:
155 return "invalid argument";
156 case -EAGAIN:
157 return "start frame too early or too much scheduled";
158 case -EFBIG:
159 return "too many isochronous frames requested";
160 case -EPIPE:
161 return "endpoint stalled";
162 case -EMSGSIZE:
163 return "invalid packet size";
164 case -ENOSPC:
165 return "would overcommit USB bandwidth";
166 case -ESHUTDOWN:
167 return "device shut down";
168 case -EPERM:
169 return "reject flag set";
170 case -EHOSTUNREACH:
171 return "device suspended";
172 default:
173 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
174 return unkmsg;
175 }
176}
177
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800178/* get message text for USB status code
179 */
180static char *get_usb_statmsg(int status)
181{
182 static char unkmsg[28];
183
184 switch (status) {
185 case 0:
186 return "success";
187 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700188 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800189 case -EINPROGRESS:
190 return "pending";
191 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700192 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800193 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700194 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700195 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800196 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700197 case -EPIPE:
198 return "endpoint stalled";
199 case -ECOMM:
200 return "IN buffer overrun";
201 case -ENOSR:
202 return "OUT buffer underrun";
203 case -EOVERFLOW:
204 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800205 case -EREMOTEIO:
206 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700207 case -ENODEV:
208 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800209 case -EXDEV:
210 return "partial isochronous transfer";
211 case -EINVAL:
212 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700213 case -ECONNRESET:
214 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800215 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700216 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800217 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700218 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800219 return unkmsg;
220 }
221}
222
223/* usb_pipetype_str
224 * retrieve string representation of USB pipe type
225 */
226static inline char *usb_pipetype_str(int pipe)
227{
228 if (usb_pipeisoc(pipe))
229 return "Isoc";
230 if (usb_pipeint(pipe))
231 return "Int";
232 if (usb_pipecontrol(pipe))
233 return "Ctrl";
234 if (usb_pipebulk(pipe))
235 return "Bulk";
236 return "?";
237}
238
239/* dump_urb
240 * write content of URB to syslog for debugging
241 */
242static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700243 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800244{
245#ifdef CONFIG_GIGASET_DEBUG
246 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700247 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800248 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700249 gig_dbg(level,
250 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800251 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700252 (unsigned long) urb->dev,
253 usb_pipetype_str(urb->pipe),
254 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
255 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800256 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700257 urb->transfer_flags);
258 gig_dbg(level,
259 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800260 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700261 (unsigned long) urb->transfer_buffer,
262 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800263 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700264 gig_dbg(level,
265 " start_frame=%d, number_of_packets=%d, interval=%d, "
266 "error_count=%d,",
267 urb->start_frame, urb->number_of_packets, urb->interval,
268 urb->error_count);
269 gig_dbg(level,
270 " context=0x%08lx, complete=0x%08lx, "
271 "iso_frame_desc[]={",
272 (unsigned long) urb->context,
273 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800274 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700275 struct usb_iso_packet_descriptor *pifd
276 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700277 gig_dbg(level,
278 " {offset=%u, length=%u, actual_length=%u, "
279 "status=%u}",
280 pifd->offset, pifd->length, pifd->actual_length,
281 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800282 }
283 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700284 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800285#endif
286}
287
288/* read/set modem control bits etc. (m10x only) */
289static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700290 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800291{
292 return -EINVAL;
293}
294
295static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
296{
297 return -EINVAL;
298}
299
300static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
301{
302 return -EINVAL;
303}
304
305/* error_hangup
306 * hang up any existing connection because of an unrecoverable error
307 * This function may be called from any context and takes care of scheduling
308 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700309 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800310 * argument:
311 * B channel control structure
312 */
313static inline void error_hangup(struct bc_state *bcs)
314{
315 struct cardstate *cs = bcs->cs;
316
Tilman Schmidt784d5852006-04-10 22:55:04 -0700317 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
318 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800319
Tilman Schmidt73a88812006-04-22 02:35:30 -0700320 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
321 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800322
323 gigaset_schedule_event(cs);
324}
325
326/* error_reset
327 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700328 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700329 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700330 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800331 * argument:
332 * controller state structure
333 */
334static inline void error_reset(struct cardstate *cs)
335{
Tilman Schmidt06163f82006-06-26 00:25:33 -0700336 /* close AT command channel to recover (ignore errors) */
337 req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
338
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800339 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700340 dev_err(cs->dev,
341 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800342}
343
344/* check_pending
345 * check for completion of pending control request
346 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700347 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800348 */
349static void check_pending(struct bas_cardstate *ucs)
350{
351 unsigned long flags;
352
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800353 spin_lock_irqsave(&ucs->lock, flags);
354 switch (ucs->pending) {
355 case 0:
356 break;
357 case HD_OPEN_ATCHANNEL:
358 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
359 ucs->pending = 0;
360 break;
361 case HD_OPEN_B1CHANNEL:
362 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
363 ucs->pending = 0;
364 break;
365 case HD_OPEN_B2CHANNEL:
366 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
367 ucs->pending = 0;
368 break;
369 case HD_CLOSE_ATCHANNEL:
370 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
371 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800372 break;
373 case HD_CLOSE_B1CHANNEL:
374 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
375 ucs->pending = 0;
376 break;
377 case HD_CLOSE_B2CHANNEL:
378 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
379 ucs->pending = 0;
380 break;
381 case HD_DEVICE_INIT_ACK: /* no reply expected */
382 ucs->pending = 0;
383 break;
384 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
385 * are handled separately and should never end up here
386 */
387 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700388 dev_warn(&ucs->interface->dev,
389 "unknown pending request 0x%02x cleared\n",
390 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800391 ucs->pending = 0;
392 }
393
394 if (!ucs->pending)
395 del_timer(&ucs->timer_ctrl);
396
397 spin_unlock_irqrestore(&ucs->lock, flags);
398}
399
400/* cmd_in_timeout
401 * timeout routine for command input request
402 * argument:
403 * controller state structure
404 */
405static void cmd_in_timeout(unsigned long data)
406{
407 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700408 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700409 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800410
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800411 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700412 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800413 return;
414 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800415
Tilman Schmidt06163f82006-06-26 00:25:33 -0700416 if (ucs->retry_cmd_in++ < BAS_RETRY) {
417 dev_notice(cs->dev, "control read: timeout, retry %d\n",
418 ucs->retry_cmd_in);
419 rc = atread_submit(cs, BAS_TIMEOUT);
420 if (rc >= 0 || rc == -ENODEV)
421 /* resubmitted or disconnected */
422 /* - bypass regular exit block */
423 return;
424 } else {
425 dev_err(cs->dev,
426 "control read: timeout, giving up after %d tries\n",
427 ucs->retry_cmd_in);
428 }
429 kfree(ucs->rcvbuf);
430 ucs->rcvbuf = NULL;
431 ucs->rcvbuf_size = 0;
432 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800433}
434
Tilman Schmidt73a88812006-04-22 02:35:30 -0700435/* set/clear bits in base connection state, return previous state
436 */
437inline static int update_basstate(struct bas_cardstate *ucs,
438 int set, int clear)
439{
440 unsigned long flags;
441 int state;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800442
Tilman Schmidt73a88812006-04-22 02:35:30 -0700443 spin_lock_irqsave(&ucs->lock, flags);
444 state = atomic_read(&ucs->basstate);
445 atomic_set(&ucs->basstate, (state & ~clear) | set);
446 spin_unlock_irqrestore(&ucs->lock, flags);
447 return state;
448}
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800449
Tilman Schmidt06163f82006-06-26 00:25:33 -0700450/* read_ctrl_callback
451 * USB completion handler for control pipe input
452 * called by the USB subsystem in interrupt context
453 * parameter:
454 * urb USB request block
455 * urb->context = inbuf structure for controller state
456 */
David Howells7d12e782006-10-05 14:55:46 +0100457static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700458{
459 struct inbuf_t *inbuf = urb->context;
460 struct cardstate *cs = inbuf->cs;
461 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800462 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700463 int have_data = 0;
464 unsigned numbytes;
465 int rc;
466
467 update_basstate(ucs, 0, BS_ATRDPEND);
468
469 if (!ucs->rcvbuf_size) {
470 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
471 return;
472 }
473
474 del_timer(&ucs->timer_cmd_in);
475
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800476 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700477 case 0: /* normal completion */
478 numbytes = urb->actual_length;
479 if (unlikely(numbytes != ucs->rcvbuf_size)) {
480 dev_warn(cs->dev,
481 "control read: received %d chars, expected %d\n",
482 numbytes, ucs->rcvbuf_size);
483 if (numbytes > ucs->rcvbuf_size)
484 numbytes = ucs->rcvbuf_size;
485 }
486
487 /* copy received bytes to inbuf */
488 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
489
490 if (unlikely(numbytes < ucs->rcvbuf_size)) {
491 /* incomplete - resubmit for remaining bytes */
492 ucs->rcvbuf_size -= numbytes;
493 ucs->retry_cmd_in = 0;
494 rc = atread_submit(cs, BAS_TIMEOUT);
495 if (rc >= 0 || rc == -ENODEV)
496 /* resubmitted or disconnected */
497 /* - bypass regular exit block */
498 return;
499 error_reset(cs);
500 }
501 break;
502
503 case -ENOENT: /* cancelled */
504 case -ECONNRESET: /* cancelled (async) */
505 case -EINPROGRESS: /* pending */
506 case -ENODEV: /* device removed */
507 case -ESHUTDOWN: /* device shut down */
508 /* no action necessary */
509 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800510 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700511 break;
512
513 default: /* severe trouble */
514 dev_warn(cs->dev, "control read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800515 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700516 if (ucs->retry_cmd_in++ < BAS_RETRY) {
517 dev_notice(cs->dev, "control read: retry %d\n",
518 ucs->retry_cmd_in);
519 rc = atread_submit(cs, BAS_TIMEOUT);
520 if (rc >= 0 || rc == -ENODEV)
521 /* resubmitted or disconnected */
522 /* - bypass regular exit block */
523 return;
524 } else {
525 dev_err(cs->dev,
526 "control read: giving up after %d tries\n",
527 ucs->retry_cmd_in);
528 }
529 error_reset(cs);
530 }
531
532 kfree(ucs->rcvbuf);
533 ucs->rcvbuf = NULL;
534 ucs->rcvbuf_size = 0;
535 if (have_data) {
536 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
537 gigaset_schedule_event(cs);
538 }
539}
540
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800541/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700542 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800543 * parameters:
544 * cs controller state structure
545 * timeout timeout in 1/10 sec., 0: none
546 * return value:
547 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800548 * -EBUSY if another request is pending
549 * any URB submission error code
550 */
551static int atread_submit(struct cardstate *cs, int timeout)
552{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700553 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800554 int ret;
555
Tilman Schmidt784d5852006-04-10 22:55:04 -0700556 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
557 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800558
Tilman Schmidt73a88812006-04-22 02:35:30 -0700559 if (update_basstate(ucs, BS_ATRDPEND, 0) & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700560 dev_err(cs->dev,
561 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800562 return -EBUSY;
563 }
564
565 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
566 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
567 ucs->dr_cmd_in.wValue = 0;
568 ucs->dr_cmd_in.wIndex = 0;
569 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
570 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700571 usb_rcvctrlpipe(ucs->udev, 0),
572 (unsigned char*) & ucs->dr_cmd_in,
573 ucs->rcvbuf, ucs->rcvbuf_size,
574 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800575
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800576 if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700577 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700579 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800580 return ret;
581 }
582
583 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700584 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800585 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
586 ucs->timer_cmd_in.data = (unsigned long) cs;
587 ucs->timer_cmd_in.function = cmd_in_timeout;
588 add_timer(&ucs->timer_cmd_in);
589 }
590 return 0;
591}
592
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800593/* read_int_callback
594 * USB completion handler for interrupt pipe input
595 * called by the USB subsystem in interrupt context
596 * parameter:
597 * urb USB request block
598 * urb->context = controller state structure
599 */
David Howells7d12e782006-10-05 14:55:46 +0100600static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800601{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700602 struct cardstate *cs = urb->context;
603 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800604 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800605 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800606 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700607 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800608 unsigned l;
609 int channel;
610
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800611 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800612 case 0: /* success */
613 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700614 case -ENOENT: /* cancelled */
615 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800616 case -EINPROGRESS: /* pending */
617 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700618 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800619 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800620 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700621 case -ENODEV: /* device removed */
622 case -ESHUTDOWN: /* device shut down */
623 //FIXME use this as disconnect indicator?
624 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
625 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800626 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700627 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800628 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800629 //FIXME corrective action? resubmission always ok?
630 goto resubmit;
631 }
632
Tilman Schmidt73a88812006-04-22 02:35:30 -0700633 /* drop incomplete packets even if the missing bytes wouldn't matter */
634 if (unlikely(urb->actual_length < 3)) {
635 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
636 urb->actual_length);
637 goto resubmit;
638 }
639
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800640 l = (unsigned) ucs->int_in_buf[1] +
641 (((unsigned) ucs->int_in_buf[2]) << 8);
642
Tilman Schmidt784d5852006-04-10 22:55:04 -0700643 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
644 urb->actual_length, (int)ucs->int_in_buf[0], l,
645 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800646
647 channel = 0;
648
649 switch (ucs->int_in_buf[0]) {
650 case HD_DEVICE_INIT_OK:
651 update_basstate(ucs, BS_INIT, 0);
652 break;
653
654 case HD_READY_SEND_ATDATA:
655 del_timer(&ucs->timer_atrdy);
656 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
657 start_cbsend(cs);
658 break;
659
660 case HD_OPEN_B2CHANNEL_ACK:
661 ++channel;
662 case HD_OPEN_B1CHANNEL_ACK:
663 bcs = cs->bcs + channel;
664 update_basstate(ucs, BS_B1OPEN << channel, 0);
665 gigaset_bchannel_up(bcs);
666 break;
667
668 case HD_OPEN_ATCHANNEL_ACK:
669 update_basstate(ucs, BS_ATOPEN, 0);
670 start_cbsend(cs);
671 break;
672
673 case HD_CLOSE_B2CHANNEL_ACK:
674 ++channel;
675 case HD_CLOSE_B1CHANNEL_ACK:
676 bcs = cs->bcs + channel;
677 update_basstate(ucs, 0, BS_B1OPEN << channel);
678 stopurbs(bcs->hw.bas);
679 gigaset_bchannel_down(bcs);
680 break;
681
682 case HD_CLOSE_ATCHANNEL_ACK:
683 update_basstate(ucs, 0, BS_ATOPEN);
684 break;
685
686 case HD_B2_FLOW_CONTROL:
687 ++channel;
688 case HD_B1_FLOW_CONTROL:
689 bcs = cs->bcs + channel;
690 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700691 &bcs->hw.bas->corrbytes);
692 gig_dbg(DEBUG_ISO,
693 "Flow control (channel %d, sub %d): 0x%02x => %d",
694 channel, bcs->hw.bas->numsub, l,
695 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800696 break;
697
698 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
699 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700700 dev_warn(cs->dev,
701 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800702 break;
703 }
704 spin_lock_irqsave(&cs->lock, flags);
705 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700706 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700707 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700708 "receive AT data overrun, %d bytes lost\n",
709 ucs->rcvbuf_size);
710 kfree(ucs->rcvbuf);
711 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800712 }
713 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
714 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700715 dev_err(cs->dev, "out of memory receiving AT data\n");
716 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800717 break;
718 }
719 ucs->rcvbuf_size = l;
720 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700721 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800722 kfree(ucs->rcvbuf);
723 ucs->rcvbuf = NULL;
724 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700725 if (rc != -ENODEV) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700726 //FIXME corrective action?
Tilman Schmidt06163f82006-06-26 00:25:33 -0700727 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700728 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700729 break;
730 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800731 }
732 spin_unlock_irqrestore(&cs->lock, flags);
733 break;
734
735 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700736 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800737 break;
738
739 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700740 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800741 break;
742
743 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700744 dev_warn(cs->dev,
745 "unknown Gigaset signal 0x%02x (%u) ignored\n",
746 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800747 }
748
749 check_pending(ucs);
750
751resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800752 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700753 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700754 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700755 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800756 error_reset(cs);
757 }
758}
759
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800760/* read_iso_callback
761 * USB completion handler for B channel isochronous input
762 * called by the USB subsystem in interrupt context
763 * parameter:
764 * urb USB request block of completed request
765 * urb->context = bc_state structure
766 */
David Howells7d12e782006-10-05 14:55:46 +0100767static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800768{
769 struct bc_state *bcs;
770 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800771 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800772 unsigned long flags;
773 int i, rc;
774
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800775 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800776 if (unlikely(status == -ENOENT ||
777 status == -ECONNRESET ||
778 status == -EINPROGRESS ||
779 status == -ENODEV ||
780 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700781 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800782 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800783 return;
784 }
785
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700786 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800787 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800788
789 spin_lock_irqsave(&ubc->isoinlock, flags);
790 if (likely(ubc->isoindone == NULL)) {
791 /* pass URB to tasklet */
792 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800793 ubc->isoinstatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800794 tasklet_schedule(&ubc->rcvd_tasklet);
795 } else {
796 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800797 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800798 for (i = 0; i < BAS_NUMFRAMES; i++) {
799 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
800 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700801 urb->iso_frame_desc[i].status !=
802 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800803 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800804 urb->iso_frame_desc[i].status = 0;
805 urb->iso_frame_desc[i].actual_length = 0;
806 }
807 if (likely(atomic_read(&ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700808 /* urb->dev is clobbered by USB subsystem */
809 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800810 urb->transfer_flags = URB_ISO_ASAP;
811 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700812 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
813 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800814 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700815 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700816 dev_err(bcs->cs->dev,
817 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700818 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800819 dump_urb(DEBUG_ISO, "isoc read", urb);
820 error_hangup(bcs);
821 }
822 }
823 }
824 spin_unlock_irqrestore(&ubc->isoinlock, flags);
825}
826
827/* write_iso_callback
828 * USB completion handler for B channel isochronous output
829 * called by the USB subsystem in interrupt context
830 * parameter:
831 * urb USB request block of completed request
832 * urb->context = isow_urbctx_t structure
833 */
David Howells7d12e782006-10-05 14:55:46 +0100834static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800835{
836 struct isow_urbctx_t *ucx;
837 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800838 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800839 unsigned long flags;
840
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800841 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800842 if (unlikely(status == -ENOENT ||
843 status == -ECONNRESET ||
844 status == -EINPROGRESS ||
845 status == -ENODEV ||
846 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700847 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800848 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800849 return;
850 }
851
852 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700853 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800854 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800855 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800856
857 spin_lock_irqsave(&ubc->isooutlock, flags);
858 ubc->isooutovfl = ubc->isooutdone;
859 ubc->isooutdone = ucx;
860 spin_unlock_irqrestore(&ubc->isooutlock, flags);
861 tasklet_schedule(&ubc->sent_tasklet);
862}
863
864/* starturbs
865 * prepare and submit USB request blocks for isochronous input and output
866 * argument:
867 * B channel control structure
868 * return value:
869 * 0 on success
870 * < 0 on error (no URBs submitted)
871 */
872static int starturbs(struct bc_state *bcs)
873{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700874 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800875 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800876 int j, k;
877 int rc;
878
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800879 /* initialize L2 reception */
880 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
881 bcs->inputstate |= INS_flag_hunt;
882
883 /* submit all isochronous input URBs */
884 atomic_set(&ubc->running, 1);
885 for (k = 0; k < BAS_INURBS; k++) {
886 urb = ubc->isoinurbs[k];
887 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800888 rc = -EFAULT;
889 goto error;
890 }
891
892 urb->dev = bcs->cs->hw.bas->udev;
893 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
894 urb->transfer_flags = URB_ISO_ASAP;
895 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
896 urb->transfer_buffer_length = BAS_INBUFSIZE;
897 urb->number_of_packets = BAS_NUMFRAMES;
898 urb->interval = BAS_FRAMETIME;
899 urb->complete = read_iso_callback;
900 urb->context = bcs;
901 for (j = 0; j < BAS_NUMFRAMES; j++) {
902 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
903 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
904 urb->iso_frame_desc[j].status = 0;
905 urb->iso_frame_desc[j].actual_length = 0;
906 }
907
908 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800909 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800910 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800911 }
912
913 /* initialize L2 transmission */
914 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
915
916 /* set up isochronous output URBs for flag idling */
917 for (k = 0; k < BAS_OUTURBS; ++k) {
918 urb = ubc->isoouturbs[k].urb;
919 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800920 rc = -EFAULT;
921 goto error;
922 }
923 urb->dev = bcs->cs->hw.bas->udev;
924 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
925 urb->transfer_flags = URB_ISO_ASAP;
926 urb->transfer_buffer = ubc->isooutbuf->data;
927 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
928 urb->number_of_packets = BAS_NUMFRAMES;
929 urb->interval = BAS_FRAMETIME;
930 urb->complete = write_iso_callback;
931 urb->context = &ubc->isoouturbs[k];
932 for (j = 0; j < BAS_NUMFRAMES; ++j) {
933 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
934 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
935 urb->iso_frame_desc[j].status = 0;
936 urb->iso_frame_desc[j].actual_length = 0;
937 }
938 ubc->isoouturbs[k].limit = -1;
939 }
940
941 /* submit two URBs, keep third one */
942 for (k = 0; k < 2; ++k) {
943 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800944 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700945 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800946 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800947 }
948 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
949 ubc->isooutfree = &ubc->isoouturbs[2];
950 ubc->isooutdone = ubc->isooutovfl = NULL;
951 return 0;
952 error:
953 stopurbs(ubc);
954 return rc;
955}
956
957/* stopurbs
958 * cancel the USB request blocks for isochronous input and output
959 * errors are silently ignored
960 * argument:
961 * B channel control structure
962 */
963static void stopurbs(struct bas_bc_state *ubc)
964{
965 int k, rc;
966
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800967 atomic_set(&ubc->running, 0);
968
969 for (k = 0; k < BAS_INURBS; ++k) {
970 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700971 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700972 "%s: isoc input URB %d unlinked, result = %s",
973 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800974 }
975
976 for (k = 0; k < BAS_OUTURBS; ++k) {
977 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700978 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700979 "%s: isoc output URB %d unlinked, result = %s",
980 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800981 }
982}
983
984/* Isochronous Write - Bottom Half */
985/* =============================== */
986
987/* submit_iso_write_urb
988 * fill and submit the next isochronous write URB
989 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700990 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800991 * return value:
992 * number of frames submitted in URB
993 * 0 if URB not submitted because no data available (isooutbuf busy)
994 * error code < 0 on error
995 */
996static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
997{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700998 struct urb *urb = ucx->urb;
999 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001000 struct usb_iso_packet_descriptor *ifd;
1001 int corrbytes, nframe, rc;
1002
Tilman Schmidt784d5852006-04-10 22:55:04 -07001003 /* urb->dev is clobbered by USB subsystem */
1004 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001005 urb->transfer_flags = URB_ISO_ASAP;
1006 urb->transfer_buffer = ubc->isooutbuf->data;
1007 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1008
1009 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1010 ifd = &urb->iso_frame_desc[nframe];
1011
1012 /* compute frame length according to flow control */
1013 ifd->length = BAS_NORMFRAME;
1014 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001015 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1016 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001017 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1018 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1019 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1020 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1021 ifd->length += corrbytes;
1022 atomic_add(-corrbytes, &ubc->corrbytes);
1023 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001024
1025 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -07001026 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1027 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001028 if (ifd->offset < 0) {
1029 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001030 gig_dbg(DEBUG_ISO,
1031 "%s: buffer busy at frame %d",
1032 __func__, nframe);
1033 /* tasklet will be restarted from
1034 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001035 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001036 dev_err(ucx->bcs->cs->dev,
1037 "%s: buffer error %d at frame %d\n",
1038 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001039 return ifd->offset;
1040 }
1041 break;
1042 }
1043 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1044 ifd->status = 0;
1045 ifd->actual_length = 0;
1046 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001047 if (unlikely(nframe == 0))
1048 return 0; /* no data to send */
1049 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001050
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001051 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001052 if (unlikely(rc)) {
1053 if (rc == -ENODEV)
1054 /* device removed - give up silently */
1055 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1056 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001057 dev_err(ucx->bcs->cs->dev,
1058 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001059 get_usb_rcmsg(rc));
1060 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001061 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001062 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001063 return nframe;
1064}
1065
1066/* write_iso_tasklet
1067 * tasklet scheduled when an isochronous output URB from the Gigaset device
1068 * has completed
1069 * parameter:
1070 * data B channel state structure
1071 */
1072static void write_iso_tasklet(unsigned long data)
1073{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001074 struct bc_state *bcs = (struct bc_state *) data;
1075 struct bas_bc_state *ubc = bcs->hw.bas;
1076 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001077 struct isow_urbctx_t *done, *next, *ovfl;
1078 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001079 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001080 struct usb_iso_packet_descriptor *ifd;
1081 int offset;
1082 unsigned long flags;
1083 int i;
1084 struct sk_buff *skb;
1085 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001086 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001087
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001088 /* loop while completed URBs arrive in time */
1089 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001090 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001091 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001092 return;
1093 }
1094
1095 /* retrieve completed URBs */
1096 spin_lock_irqsave(&ubc->isooutlock, flags);
1097 done = ubc->isooutdone;
1098 ubc->isooutdone = NULL;
1099 ovfl = ubc->isooutovfl;
1100 ubc->isooutovfl = NULL;
1101 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1102 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001103 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001104 error_hangup(bcs);
1105 break;
1106 }
1107 if (!done)
1108 break;
1109
1110 /* submit free URB if available */
1111 spin_lock_irqsave(&ubc->isooutlock, flags);
1112 next = ubc->isooutfree;
1113 ubc->isooutfree = NULL;
1114 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1115 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001116 rc = submit_iso_write_urb(next);
1117 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001118 /* could not submit URB, put it back */
1119 spin_lock_irqsave(&ubc->isooutlock, flags);
1120 if (ubc->isooutfree == NULL) {
1121 ubc->isooutfree = next;
1122 next = NULL;
1123 }
1124 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1125 if (next) {
1126 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001127 dev_err(cs->dev,
1128 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001129 error_hangup(bcs);
1130 }
1131 }
1132 }
1133
1134 /* process completed URB */
1135 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001136 status = done->status;
1137 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001138 case -EXDEV: /* partial completion */
1139 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1140 __func__);
1141 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001142 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001143 /* inspect individual frames
1144 * assumptions (for lack of documentation):
1145 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001146 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001147 * - all following frames are not sent at all
1148 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001149 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001150 for (i = 0; i < BAS_NUMFRAMES; i++) {
1151 ifd = &urb->iso_frame_desc[i];
1152 if (ifd->status ||
1153 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001154 dev_warn(cs->dev,
1155 "isochronous write: frame %d: %s, "
1156 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001157 i, get_usb_statmsg(ifd->status),
1158 ifd->actual_length, ifd->length);
1159 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001160 ifd->actual_length)
1161 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001162 break;
1163 }
1164 }
1165#ifdef CONFIG_GIGASET_DEBUG
1166 /* check assumption on remaining frames */
1167 for (; i < BAS_NUMFRAMES; i++) {
1168 ifd = &urb->iso_frame_desc[i];
1169 if (ifd->status != -EINPROGRESS
1170 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001171 dev_warn(cs->dev,
1172 "isochronous write: frame %d: %s, "
1173 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001174 i, get_usb_statmsg(ifd->status),
1175 ifd->actual_length, ifd->length);
1176 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001177 ifd->actual_length)
1178 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001179 break;
1180 }
1181 }
1182#endif
1183 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001184 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001185 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001186 error_hangup(bcs);
1187 break;
1188 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001189 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001190 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001191 }
1192
1193 /* mark the write buffer area covered by this URB as free */
1194 if (done->limit >= 0)
1195 atomic_set(&ubc->isooutbuf->read, done->limit);
1196
1197 /* mark URB as free */
1198 spin_lock_irqsave(&ubc->isooutlock, flags);
1199 next = ubc->isooutfree;
1200 ubc->isooutfree = done;
1201 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1202 if (next) {
1203 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001204 rc = submit_iso_write_urb(next);
1205 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001206 /* couldn't submit */
1207 error_hangup(bcs);
1208 }
1209 }
1210 }
1211
1212 /* process queued SKBs */
1213 while ((skb = skb_dequeue(&bcs->squeue))) {
1214 /* copy to output buffer, doing L2 encapsulation */
1215 len = skb->len;
1216 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1217 /* insufficient buffer space, push back onto queue */
1218 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001219 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1220 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001221 break;
1222 }
1223 skb_pull(skb, len);
1224 gigaset_skb_sent(bcs, skb);
1225 dev_kfree_skb_any(skb);
1226 }
1227}
1228
1229/* Isochronous Read - Bottom Half */
1230/* ============================== */
1231
1232/* read_iso_tasklet
1233 * tasklet scheduled when an isochronous input URB from the Gigaset device
1234 * has completed
1235 * parameter:
1236 * data B channel state structure
1237 */
1238static void read_iso_tasklet(unsigned long data)
1239{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001240 struct bc_state *bcs = (struct bc_state *) data;
1241 struct bas_bc_state *ubc = bcs->hw.bas;
1242 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001243 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001244 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001245 char *rcvbuf;
1246 unsigned long flags;
1247 int totleft, numbytes, offset, frame, rc;
1248
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001249 /* loop while more completed URBs arrive in the meantime */
1250 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001251 /* retrieve URB */
1252 spin_lock_irqsave(&ubc->isoinlock, flags);
1253 if (!(urb = ubc->isoindone)) {
1254 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1255 return;
1256 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001257 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001258 ubc->isoindone = NULL;
1259 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001260 dev_warn(cs->dev,
1261 "isochronous read overrun, "
1262 "dropped URB with status: %s, %d bytes lost\n",
1263 get_usb_statmsg(ubc->loststatus),
1264 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001265 ubc->loststatus = -EINPROGRESS;
1266 }
1267 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1268
1269 if (unlikely(!(atomic_read(&ubc->running)))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001270 gig_dbg(DEBUG_ISO,
1271 "%s: channel not running, "
1272 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001273 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001274 return;
1275 }
1276
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001277 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001278 case 0: /* normal completion */
1279 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001280 case -EXDEV: /* inspect individual frames
1281 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001282 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1283 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001284 break;
1285 case -ENOENT:
1286 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001287 case -EINPROGRESS:
1288 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001289 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001290 continue; /* -> skip */
1291 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001292 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001293 error_hangup(bcs);
1294 continue; /* -> skip */
1295 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001296 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001297 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001298 goto error;
1299 }
1300
1301 rcvbuf = urb->transfer_buffer;
1302 totleft = urb->actual_length;
1303 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1304 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001305 dev_warn(cs->dev,
1306 "isochronous read: frame %d: %s\n",
1307 frame,
1308 get_usb_statmsg(
1309 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001310 break;
1311 }
1312 numbytes = urb->iso_frame_desc[frame].actual_length;
1313 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001314 dev_warn(cs->dev,
1315 "isochronous read: frame %d: "
1316 "numbytes (%d) > BAS_MAXFRAME\n",
1317 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001318 break;
1319 }
1320 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001321 dev_warn(cs->dev,
1322 "isochronous read: frame %d: "
1323 "numbytes (%d) > totleft (%d)\n",
1324 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001325 break;
1326 }
1327 offset = urb->iso_frame_desc[frame].offset;
1328 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001329 dev_warn(cs->dev,
1330 "isochronous read: frame %d: "
1331 "offset (%d) + numbytes (%d) "
1332 "> BAS_INBUFSIZE\n",
1333 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001334 break;
1335 }
1336 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1337 totleft -= numbytes;
1338 }
1339 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001340 dev_warn(cs->dev,
1341 "isochronous read: %d data bytes missing\n",
1342 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001343
1344 error:
1345 /* URB processed, resubmit */
1346 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1347 urb->iso_frame_desc[frame].status = 0;
1348 urb->iso_frame_desc[frame].actual_length = 0;
1349 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001350 /* urb->dev is clobbered by USB subsystem */
1351 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001352 urb->transfer_flags = URB_ISO_ASAP;
1353 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001354 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001355 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001356 dev_err(cs->dev,
1357 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001358 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001359 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1360 error_hangup(bcs);
1361 }
1362 }
1363}
1364
1365/* Channel Operations */
1366/* ================== */
1367
1368/* req_timeout
1369 * timeout routine for control output request
1370 * argument:
1371 * B channel control structure
1372 */
1373static void req_timeout(unsigned long data)
1374{
1375 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001376 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001377 int pending;
1378 unsigned long flags;
1379
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001380 check_pending(ucs);
1381
1382 spin_lock_irqsave(&ucs->lock, flags);
1383 pending = ucs->pending;
1384 ucs->pending = 0;
1385 spin_unlock_irqrestore(&ucs->lock, flags);
1386
1387 switch (pending) {
1388 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001389 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001390 break;
1391
1392 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001393 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001394 error_reset(bcs->cs);
1395 break;
1396
1397 case HD_OPEN_B2CHANNEL:
1398 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001399 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1400 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001401 error_hangup(bcs);
1402 break;
1403
1404 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001405 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001406 break;
1407
1408 case HD_CLOSE_B2CHANNEL:
1409 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001410 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1411 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001412 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001413 break;
1414
1415 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001416 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1417 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001418 }
1419}
1420
1421/* write_ctrl_callback
1422 * USB completion handler for control pipe output
1423 * called by the USB subsystem in interrupt context
1424 * parameter:
1425 * urb USB request block of completed request
1426 * urb->context = hardware specific controller state structure
1427 */
David Howells7d12e782006-10-05 14:55:46 +01001428static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001429{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001430 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001431 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001432 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001433 unsigned long flags;
1434
Tilman Schmidt06163f82006-06-26 00:25:33 -07001435 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001436 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001437 case 0: /* normal completion */
1438 spin_lock_irqsave(&ucs->lock, flags);
1439 switch (ucs->pending) {
1440 case HD_DEVICE_INIT_ACK: /* no reply expected */
1441 del_timer(&ucs->timer_ctrl);
1442 ucs->pending = 0;
1443 break;
1444 }
1445 spin_unlock_irqrestore(&ucs->lock, flags);
1446 return;
1447
1448 case -ENOENT: /* cancelled */
1449 case -ECONNRESET: /* cancelled (async) */
1450 case -EINPROGRESS: /* pending */
1451 case -ENODEV: /* device removed */
1452 case -ESHUTDOWN: /* device shut down */
1453 /* ignore silently */
1454 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001455 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001456 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001457
1458 default: /* any failure */
1459 if (++ucs->retry_ctrl > BAS_RETRY) {
1460 dev_err(&ucs->interface->dev,
1461 "control request 0x%02x failed: %s\n",
1462 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001463 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001464 break; /* give up */
1465 }
1466 dev_notice(&ucs->interface->dev,
1467 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001468 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001469 ucs->retry_ctrl);
1470 /* urb->dev is clobbered by USB subsystem */
1471 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001472 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001473 if (unlikely(rc)) {
1474 dev_err(&ucs->interface->dev,
1475 "could not resubmit request 0x%02x: %s\n",
1476 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1477 break;
1478 }
1479 /* resubmitted */
1480 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001481 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001482
1483 /* failed, clear pending request */
1484 spin_lock_irqsave(&ucs->lock, flags);
1485 del_timer(&ucs->timer_ctrl);
1486 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001487 spin_unlock_irqrestore(&ucs->lock, flags);
1488}
1489
1490/* req_submit
1491 * submit a control output request without message buffer to the Gigaset base
1492 * and optionally start a timeout
1493 * parameters:
1494 * bcs B channel control structure
1495 * req control request code (HD_*)
1496 * val control request parameter value (set to 0 if unused)
1497 * timeout timeout in seconds (0: no timeout)
1498 * return value:
1499 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001500 * -EBUSY if another request is pending
1501 * any URB submission error code
1502 */
1503static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1504{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001505 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001506 int ret;
1507 unsigned long flags;
1508
Tilman Schmidt784d5852006-04-10 22:55:04 -07001509 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001510
1511 spin_lock_irqsave(&ucs->lock, flags);
1512 if (ucs->pending) {
1513 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001514 dev_err(bcs->cs->dev,
1515 "submission of request 0x%02x failed: "
1516 "request 0x%02x still pending\n",
1517 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001518 return -EBUSY;
1519 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001520
1521 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1522 ucs->dr_ctrl.bRequest = req;
1523 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1524 ucs->dr_ctrl.wIndex = 0;
1525 ucs->dr_ctrl.wLength = 0;
1526 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001527 usb_sndctrlpipe(ucs->udev, 0),
1528 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1529 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001530 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001531 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001532 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001533 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001534 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001535 spin_unlock_irqrestore(&ucs->lock, flags);
1536 return ret;
1537 }
1538 ucs->pending = req;
1539
1540 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001541 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001542 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1543 ucs->timer_ctrl.data = (unsigned long) bcs;
1544 ucs->timer_ctrl.function = req_timeout;
1545 add_timer(&ucs->timer_ctrl);
1546 }
1547
1548 spin_unlock_irqrestore(&ucs->lock, flags);
1549 return 0;
1550}
1551
1552/* gigaset_init_bchannel
1553 * called by common.c to connect a B channel
1554 * initialize isochronous I/O and tell the Gigaset base to open the channel
1555 * argument:
1556 * B channel control structure
1557 * return value:
1558 * 0 on success, error code < 0 on error
1559 */
1560static int gigaset_init_bchannel(struct bc_state *bcs)
1561{
1562 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001563 unsigned long flags;
1564
1565 spin_lock_irqsave(&bcs->cs->lock, flags);
1566 if (unlikely(!bcs->cs->connected)) {
1567 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1568 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1569 return -ENODEV;
1570 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001571
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001572 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001573 dev_err(bcs->cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001574 "could not start isochronous I/O for channel B%d: %s\n",
1575 bcs->channel + 1,
1576 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1577 if (ret != -ENODEV)
1578 error_hangup(bcs);
1579 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001580 return ret;
1581 }
1582
1583 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1584 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001585 dev_err(bcs->cs->dev, "could not open channel B%d\n",
1586 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001587 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001588 if (ret != -ENODEV)
1589 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001590 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001591
1592 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001593 return ret;
1594}
1595
1596/* gigaset_close_bchannel
1597 * called by common.c to disconnect a B channel
1598 * tell the Gigaset base to close the channel
1599 * stopping isochronous I/O and LL notification will be done when the
1600 * acknowledgement for the close arrives
1601 * argument:
1602 * B channel control structure
1603 * return value:
1604 * 0 on success, error code < 0 on error
1605 */
1606static int gigaset_close_bchannel(struct bc_state *bcs)
1607{
1608 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001609 unsigned long flags;
1610
1611 spin_lock_irqsave(&bcs->cs->lock, flags);
1612 if (unlikely(!bcs->cs->connected)) {
1613 spin_unlock_irqrestore(&bcs->cs->lock, flags);
1614 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1615 return -ENODEV;
1616 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001617
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001618 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1619 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1620 /* channel not running: just signal common.c */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001621 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001622 gigaset_bchannel_down(bcs);
1623 return 0;
1624 }
1625
Tilman Schmidt73a88812006-04-22 02:35:30 -07001626 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001627 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1628 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidt73a88812006-04-22 02:35:30 -07001629 dev_err(bcs->cs->dev, "closing channel B%d failed\n",
1630 bcs->channel + 1);
1631
1632 spin_unlock_irqrestore(&bcs->cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001633 return ret;
1634}
1635
1636/* Device Operations */
1637/* ================= */
1638
1639/* complete_cb
1640 * unqueue first command buffer from queue, waking any sleepers
1641 * must be called with cs->cmdlock held
1642 * parameter:
1643 * cs controller state structure
1644 */
1645static void complete_cb(struct cardstate *cs)
1646{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001647 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001648
1649 /* unqueue completed buffer */
1650 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001651 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1652 "write_command: sent %u bytes, %u left",
1653 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001654 if ((cs->cmdbuf = cb->next) != NULL) {
1655 cs->cmdbuf->prev = NULL;
1656 cs->curlen = cs->cmdbuf->len;
1657 } else {
1658 cs->lastcmdbuf = NULL;
1659 cs->curlen = 0;
1660 }
1661
1662 if (cb->wake_tasklet)
1663 tasklet_schedule(cb->wake_tasklet);
1664
1665 kfree(cb);
1666}
1667
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001668/* write_command_callback
1669 * USB completion handler for AT command transmission
1670 * called by the USB subsystem in interrupt context
1671 * parameter:
1672 * urb USB request block of completed request
1673 * urb->context = controller state structure
1674 */
David Howells7d12e782006-10-05 14:55:46 +01001675static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001676{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001677 struct cardstate *cs = urb->context;
1678 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001679 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001680 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001681
Tilman Schmidt73a88812006-04-22 02:35:30 -07001682 update_basstate(ucs, 0, BS_ATWRPEND);
1683
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001684 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001685 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001686 case 0: /* normal completion */
1687 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001688 case -ENOENT: /* cancelled */
1689 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001690 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001691 case -ENODEV: /* device removed */
1692 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001693 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001694 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001695 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001696 return;
1697 default: /* any failure */
1698 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001699 dev_warn(cs->dev,
1700 "command write: %s, "
1701 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001702 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001703 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001704 break;
1705 }
1706 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001707 dev_warn(cs->dev,
1708 "command write: %s, "
1709 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001710 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001711 break;
1712 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001713 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001714 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001715 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1716 /* resubmitted - bypass regular exit block */
1717 return;
1718 /* command send failed, assume base still waiting */
1719 update_basstate(ucs, BS_ATREADY, 0);
1720 }
1721
1722 spin_lock_irqsave(&cs->cmdlock, flags);
1723 if (cs->cmdbuf != NULL)
1724 complete_cb(cs);
1725 spin_unlock_irqrestore(&cs->cmdlock, flags);
1726}
1727
1728/* atrdy_timeout
1729 * timeout routine for AT command transmission
1730 * argument:
1731 * controller state structure
1732 */
1733static void atrdy_timeout(unsigned long data)
1734{
1735 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001736 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001737
Tilman Schmidt784d5852006-04-10 22:55:04 -07001738 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001739
1740 /* fake the missing signal - what else can I do? */
1741 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1742 start_cbsend(cs);
1743}
1744
1745/* atwrite_submit
1746 * submit an HD_WRITE_ATMESSAGE command URB
1747 * parameters:
1748 * cs controller state structure
1749 * buf buffer containing command to send
1750 * len length of command to send
1751 * return value:
1752 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001753 * -EBUSY if another request is pending
1754 * any URB submission error code
1755 */
1756static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1757{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001758 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001759 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001760
Tilman Schmidt784d5852006-04-10 22:55:04 -07001761 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001762
Tilman Schmidt73a88812006-04-22 02:35:30 -07001763 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001764 dev_err(cs->dev,
1765 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001766 return -EBUSY;
1767 }
1768
1769 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1770 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1771 ucs->dr_cmd_out.wValue = 0;
1772 ucs->dr_cmd_out.wIndex = 0;
1773 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1774 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1775 usb_sndctrlpipe(ucs->udev, 0),
1776 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1777 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001778 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001779 if (unlikely(rc)) {
1780 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001781 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001782 get_usb_rcmsg(rc));
1783 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001784 }
1785
Tilman Schmidt73a88812006-04-22 02:35:30 -07001786 /* submitted successfully, start timeout if necessary */
1787 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001788 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1789 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001790 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1791 ucs->timer_atrdy.data = (unsigned long) cs;
1792 ucs->timer_atrdy.function = atrdy_timeout;
1793 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001794 }
1795 return 0;
1796}
1797
1798/* start_cbsend
1799 * start transmission of AT command queue if necessary
1800 * parameter:
1801 * cs controller state structure
1802 * return value:
1803 * 0 on success
1804 * error code < 0 on error
1805 */
1806static int start_cbsend(struct cardstate *cs)
1807{
1808 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001809 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001810 unsigned long flags;
1811 int rc;
1812 int retval = 0;
1813
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001814 /* check if AT channel is open */
1815 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001816 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001817 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1818 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001819 /* flush command queue */
1820 spin_lock_irqsave(&cs->cmdlock, flags);
1821 while (cs->cmdbuf != NULL)
1822 complete_cb(cs);
1823 spin_unlock_irqrestore(&cs->cmdlock, flags);
1824 }
1825 return rc;
1826 }
1827
1828 /* try to send first command in queue */
1829 spin_lock_irqsave(&cs->cmdlock, flags);
1830
1831 while ((cb = cs->cmdbuf) != NULL &&
1832 atomic_read(&ucs->basstate) & BS_ATREADY) {
1833 ucs->retry_cmd_out = 0;
1834 rc = atwrite_submit(cs, cb->buf, cb->len);
1835 if (unlikely(rc)) {
1836 retval = rc;
1837 complete_cb(cs);
1838 }
1839 }
1840
1841 spin_unlock_irqrestore(&cs->cmdlock, flags);
1842 return retval;
1843}
1844
1845/* gigaset_write_cmd
1846 * This function is called by the device independent part of the driver
1847 * to transmit an AT command string to the Gigaset device.
1848 * It encapsulates the device specific method for transmission over the
1849 * direct USB connection to the base.
1850 * The command string is added to the queue of commands to send, and
1851 * USB transmission is started if necessary.
1852 * parameters:
1853 * cs controller state structure
1854 * buf command string to send
1855 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001856 * wake_tasklet tasklet to run when transmission is completed
1857 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001858 * return value:
1859 * number of bytes queued on success
1860 * error code < 0 on error
1861 */
1862static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001863 const unsigned char *buf, int len,
1864 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001865{
1866 struct cmdbuf_t *cb;
1867 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001868 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001869
1870 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001871 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001872 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001873
Tilman Schmidt39f07222006-12-13 00:33:52 -08001874 if (len <= 0) {
1875 /* nothing to do */
1876 rc = 0;
1877 goto notqueued;
1878 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001879
1880 if (len > IF_WRITEBUF)
1881 len = IF_WRITEBUF;
1882 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001883 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001884 rc = -ENOMEM;
1885 goto notqueued;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001886 }
1887
1888 memcpy(cb->buf, buf, len);
1889 cb->len = len;
1890 cb->offset = 0;
1891 cb->next = NULL;
1892 cb->wake_tasklet = wake_tasklet;
1893
1894 spin_lock_irqsave(&cs->cmdlock, flags);
1895 cb->prev = cs->lastcmdbuf;
1896 if (cs->lastcmdbuf)
1897 cs->lastcmdbuf->next = cb;
1898 else {
1899 cs->cmdbuf = cb;
1900 cs->curlen = len;
1901 }
1902 cs->cmdbytes += len;
1903 cs->lastcmdbuf = cb;
1904 spin_unlock_irqrestore(&cs->cmdlock, flags);
1905
Tilman Schmidt73a88812006-04-22 02:35:30 -07001906 spin_lock_irqsave(&cs->lock, flags);
1907 if (unlikely(!cs->connected)) {
1908 spin_unlock_irqrestore(&cs->lock, flags);
1909 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001910 /* flush command queue */
1911 spin_lock_irqsave(&cs->cmdlock, flags);
1912 while (cs->cmdbuf != NULL)
1913 complete_cb(cs);
1914 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001915 return -ENODEV;
1916 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001917 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001918 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001919 return rc < 0 ? rc : len;
1920
1921notqueued: /* request handled without queuing */
1922 if (wake_tasklet)
1923 tasklet_schedule(wake_tasklet);
1924 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001925}
1926
1927/* gigaset_write_room
1928 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001929 * return number of characters the driver will accept to be written via
1930 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001931 * parameter:
1932 * controller state structure
1933 * return value:
1934 * number of characters
1935 */
1936static int gigaset_write_room(struct cardstate *cs)
1937{
1938 return IF_WRITEBUF;
1939}
1940
1941/* gigaset_chars_in_buffer
1942 * tty_driver.chars_in_buffer interface routine
1943 * return number of characters waiting to be sent
1944 * parameter:
1945 * controller state structure
1946 * return value:
1947 * number of characters
1948 */
1949static int gigaset_chars_in_buffer(struct cardstate *cs)
1950{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07001951 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001952}
1953
1954/* gigaset_brkchars
1955 * implementation of ioctl(GIGASET_BRKCHARS)
1956 * parameter:
1957 * controller state structure
1958 * return value:
1959 * -EINVAL (unimplemented function)
1960 */
1961static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1962{
1963 return -EINVAL;
1964}
1965
1966
1967/* Device Initialization/Shutdown */
1968/* ============================== */
1969
1970/* Free hardware dependent part of the B channel structure
1971 * parameter:
1972 * bcs B channel structure
1973 * return value:
1974 * !=0 on success
1975 */
1976static int gigaset_freebcshw(struct bc_state *bcs)
1977{
Tilman Schmidt73a88812006-04-22 02:35:30 -07001978 struct bas_bc_state *ubc = bcs->hw.bas;
1979 int i;
1980
1981 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001982 return 0;
1983
Tilman Schmidt73a88812006-04-22 02:35:30 -07001984 /* kill URBs and tasklets before freeing - better safe than sorry */
1985 atomic_set(&ubc->running, 0);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001986 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
1987 for (i = 0; i < BAS_OUTURBS; ++i) {
1988 usb_kill_urb(ubc->isoouturbs[i].urb);
1989 usb_free_urb(ubc->isoouturbs[i].urb);
1990 }
1991 for (i = 0; i < BAS_INURBS; ++i) {
1992 usb_kill_urb(ubc->isoinurbs[i]);
1993 usb_free_urb(ubc->isoinurbs[i]);
1994 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001995 tasklet_kill(&ubc->sent_tasklet);
1996 tasklet_kill(&ubc->rcvd_tasklet);
1997 kfree(ubc->isooutbuf);
1998 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001999 bcs->hw.bas = NULL;
2000 return 1;
2001}
2002
2003/* Initialize hardware dependent part of the B channel structure
2004 * parameter:
2005 * bcs B channel structure
2006 * return value:
2007 * !=0 on success
2008 */
2009static int gigaset_initbcshw(struct bc_state *bcs)
2010{
2011 int i;
2012 struct bas_bc_state *ubc;
2013
2014 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2015 if (!ubc) {
2016 err("could not allocate bas_bc_state");
2017 return 0;
2018 }
2019
2020 atomic_set(&ubc->running, 0);
2021 atomic_set(&ubc->corrbytes, 0);
2022 spin_lock_init(&ubc->isooutlock);
2023 for (i = 0; i < BAS_OUTURBS; ++i) {
2024 ubc->isoouturbs[i].urb = NULL;
2025 ubc->isoouturbs[i].bcs = bcs;
2026 }
2027 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2028 ubc->numsub = 0;
2029 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2030 err("could not allocate isochronous output buffer");
2031 kfree(ubc);
2032 bcs->hw.bas = NULL;
2033 return 0;
2034 }
2035 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002036 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002037
2038 spin_lock_init(&ubc->isoinlock);
2039 for (i = 0; i < BAS_INURBS; ++i)
2040 ubc->isoinurbs[i] = NULL;
2041 ubc->isoindone = NULL;
2042 ubc->loststatus = -EINPROGRESS;
2043 ubc->isoinlost = 0;
2044 ubc->seqlen = 0;
2045 ubc->inbyte = 0;
2046 ubc->inbits = 0;
2047 ubc->goodbytes = 0;
2048 ubc->alignerrs = 0;
2049 ubc->fcserrs = 0;
2050 ubc->frameerrs = 0;
2051 ubc->giants = 0;
2052 ubc->runts = 0;
2053 ubc->aborts = 0;
2054 ubc->shared0s = 0;
2055 ubc->stolen0s = 0;
2056 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002057 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002058 return 1;
2059}
2060
2061static void gigaset_reinitbcshw(struct bc_state *bcs)
2062{
2063 struct bas_bc_state *ubc = bcs->hw.bas;
2064
2065 atomic_set(&bcs->hw.bas->running, 0);
2066 atomic_set(&bcs->hw.bas->corrbytes, 0);
2067 bcs->hw.bas->numsub = 0;
2068 spin_lock_init(&ubc->isooutlock);
2069 spin_lock_init(&ubc->isoinlock);
2070 ubc->loststatus = -EINPROGRESS;
2071}
2072
2073static void gigaset_freecshw(struct cardstate *cs)
2074{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002075 /* timers, URBs and rcvbuf are disposed of in disconnect */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002076 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002077 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002078}
2079
2080static int gigaset_initcshw(struct cardstate *cs)
2081{
2082 struct bas_cardstate *ucs;
2083
2084 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2085 if (!ucs)
2086 return 0;
2087
2088 ucs->urb_cmd_in = NULL;
2089 ucs->urb_cmd_out = NULL;
2090 ucs->rcvbuf = NULL;
2091 ucs->rcvbuf_size = 0;
2092
2093 spin_lock_init(&ucs->lock);
2094 ucs->pending = 0;
2095
2096 atomic_set(&ucs->basstate, 0);
2097 init_timer(&ucs->timer_ctrl);
2098 init_timer(&ucs->timer_atrdy);
2099 init_timer(&ucs->timer_cmd_in);
2100
2101 return 1;
2102}
2103
2104/* freeurbs
2105 * unlink and deallocate all URBs unconditionally
2106 * caller must make sure that no commands are still in progress
2107 * parameter:
2108 * cs controller state structure
2109 */
2110static void freeurbs(struct cardstate *cs)
2111{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002112 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002113 struct bas_bc_state *ubc;
2114 int i, j;
2115
Tilman Schmidt39f07222006-12-13 00:33:52 -08002116 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002117 for (j = 0; j < 2; ++j) {
2118 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002119 for (i = 0; i < BAS_OUTURBS; ++i) {
2120 usb_kill_urb(ubc->isoouturbs[i].urb);
2121 usb_free_urb(ubc->isoouturbs[i].urb);
2122 ubc->isoouturbs[i].urb = NULL;
2123 }
2124 for (i = 0; i < BAS_INURBS; ++i) {
2125 usb_kill_urb(ubc->isoinurbs[i]);
2126 usb_free_urb(ubc->isoinurbs[i]);
2127 ubc->isoinurbs[i] = NULL;
2128 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002129 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002130 usb_kill_urb(ucs->urb_int_in);
2131 usb_free_urb(ucs->urb_int_in);
2132 ucs->urb_int_in = NULL;
2133 usb_kill_urb(ucs->urb_cmd_out);
2134 usb_free_urb(ucs->urb_cmd_out);
2135 ucs->urb_cmd_out = NULL;
2136 usb_kill_urb(ucs->urb_cmd_in);
2137 usb_free_urb(ucs->urb_cmd_in);
2138 ucs->urb_cmd_in = NULL;
2139 usb_kill_urb(ucs->urb_ctrl);
2140 usb_free_urb(ucs->urb_ctrl);
2141 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002142}
2143
2144/* gigaset_probe
2145 * This function is called when a new USB device is connected.
2146 * It checks whether the new device is handled by this driver.
2147 */
2148static int gigaset_probe(struct usb_interface *interface,
2149 const struct usb_device_id *id)
2150{
2151 struct usb_host_interface *hostif;
2152 struct usb_device *udev = interface_to_usbdev(interface);
2153 struct cardstate *cs = NULL;
2154 struct bas_cardstate *ucs = NULL;
2155 struct bas_bc_state *ubc;
2156 struct usb_endpoint_descriptor *endpoint;
2157 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002158 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002159
Tilman Schmidt784d5852006-04-10 22:55:04 -07002160 gig_dbg(DEBUG_ANY,
2161 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2162 __func__, le16_to_cpu(udev->descriptor.idVendor),
2163 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002164
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002165 /* set required alternate setting */
2166 hostif = interface->cur_altsetting;
2167 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002168 gig_dbg(DEBUG_ANY,
2169 "%s: wrong alternate setting %d - trying to switch",
2170 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002171 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002172 dev_warn(&udev->dev, "usb_set_interface failed, "
2173 "device %d interface %d altsetting %d\n",
2174 udev->devnum, hostif->desc.bInterfaceNumber,
2175 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002176 return -ENODEV;
2177 }
2178 hostif = interface->cur_altsetting;
2179 }
2180
2181 /* Reject application specific interfaces
2182 */
2183 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002184 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2185 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002186 return -ENODEV;
2187 }
2188
Tilman Schmidt784d5852006-04-10 22:55:04 -07002189 dev_info(&udev->dev,
2190 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2191 __func__, le16_to_cpu(udev->descriptor.idVendor),
2192 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002193
2194 cs = gigaset_getunassignedcs(driver);
2195 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002196 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002197 return -ENODEV;
2198 }
2199 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002200
2201 /* save off device structure ptrs for later use */
2202 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002203 ucs->udev = udev;
2204 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002205 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002206
2207 /* allocate URBs:
2208 * - one for the interrupt pipe
2209 * - three for the different uses of the default control pipe
2210 * - three for each isochronous pipe
2211 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002212 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2213 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2214 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2215 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002216 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002217
2218 for (j = 0; j < 2; ++j) {
2219 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002220 for (i = 0; i < BAS_OUTURBS; ++i)
2221 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002222 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002223 goto allocerr;
2224 for (i = 0; i < BAS_INURBS; ++i)
2225 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002226 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002227 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002228 }
2229
2230 ucs->rcvbuf = NULL;
2231 ucs->rcvbuf_size = 0;
2232
2233 /* Fill the interrupt urb and send it to the core */
2234 endpoint = &hostif->endpoint[0].desc;
2235 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002236 usb_rcvintpipe(udev,
2237 (endpoint->bEndpointAddress) & 0x0f),
2238 ucs->int_in_buf, 3, read_int_callback, cs,
2239 endpoint->bInterval);
Christoph Lametere94b1762006-12-06 20:33:17 -08002240 if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002241 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002242 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002243 goto error;
2244 }
2245
2246 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002247 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002248 goto error;
2249
2250 /* tell common part that the device is ready */
2251 if (startmode == SM_LOCKED)
2252 atomic_set(&cs->mstate, MS_LOCKED);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002253
2254 /* save address of controller structure */
2255 usb_set_intfdata(interface, cs);
2256
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002257 if (!gigaset_start(cs))
2258 goto error;
2259
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002260 return 0;
2261
Tilman Schmidt73a88812006-04-22 02:35:30 -07002262allocerr:
2263 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002264error:
2265 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002266 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002267 gigaset_unassign(cs);
2268 return -ENODEV;
2269}
2270
2271/* gigaset_disconnect
2272 * This function is called when the Gigaset base is unplugged.
2273 */
2274static void gigaset_disconnect(struct usb_interface *interface)
2275{
2276 struct cardstate *cs;
2277 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002278 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002279
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002280 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002281
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002282 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002283
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002284 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002285
2286 /* mark base as not ready, all channels disconnected */
2287 atomic_set(&ucs->basstate, 0);
2288
2289 /* tell LL all channels are down */
2290 //FIXME shouldn't gigaset_stop() do this?
2291 for (j = 0; j < 2; ++j)
2292 gigaset_bchannel_down(cs->bcs + j);
2293
2294 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002295 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002296
2297 /* stop timers and URBs, free ressources */
2298 del_timer_sync(&ucs->timer_ctrl);
2299 del_timer_sync(&ucs->timer_atrdy);
2300 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002301 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002302 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002303 kfree(ucs->rcvbuf);
2304 ucs->rcvbuf = NULL;
2305 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002306 usb_put_dev(ucs->udev);
2307 ucs->interface = NULL;
2308 ucs->udev = NULL;
2309 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002310 gigaset_unassign(cs);
2311}
2312
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002313static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002314 gigaset_write_cmd,
2315 gigaset_write_room,
2316 gigaset_chars_in_buffer,
2317 gigaset_brkchars,
2318 gigaset_init_bchannel,
2319 gigaset_close_bchannel,
2320 gigaset_initbcshw,
2321 gigaset_freebcshw,
2322 gigaset_reinitbcshw,
2323 gigaset_initcshw,
2324 gigaset_freecshw,
2325 gigaset_set_modem_ctrl,
2326 gigaset_baud_rate,
2327 gigaset_set_line_ctrl,
2328 gigaset_isoc_send_skb,
2329 gigaset_isoc_input,
2330};
2331
2332/* bas_gigaset_init
2333 * This function is called after the kernel module is loaded.
2334 */
2335static int __init bas_gigaset_init(void)
2336{
2337 int result;
2338
2339 /* allocate memory for our driver state and intialize it */
2340 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002341 GIGASET_MODULENAME, GIGASET_DEVNAME,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07002342 &gigops, THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002343 goto error;
2344
2345 /* allocate memory for our device state and intialize it */
Tilman Schmidt917f5082006-04-10 22:55:00 -07002346 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2347 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002348 if (!cardstate)
2349 goto error;
2350
2351 /* register this driver with the USB subsystem */
2352 result = usb_register(&gigaset_usb_driver);
2353 if (result < 0) {
2354 err("usb_register failed (error %d)", -result);
2355 goto error;
2356 }
2357
2358 info(DRIVER_AUTHOR);
2359 info(DRIVER_DESC);
2360 return 0;
2361
2362error: if (cardstate)
2363 gigaset_freecs(cardstate);
2364 cardstate = NULL;
2365 if (driver)
2366 gigaset_freedriver(driver);
2367 driver = NULL;
2368 return -1;
2369}
2370
2371/* bas_gigaset_exit
2372 * This function is called before the kernel module is unloaded.
2373 */
2374static void __exit bas_gigaset_exit(void)
2375{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002376 struct bas_cardstate *ucs = cardstate->hw.bas;
2377
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002378 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002379 * => no gigaset_start any more
2380 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002381
2382 gigaset_shutdown(cardstate);
2383 /* from now on, no isdn callback should be possible */
2384
Tilman Schmidt73a88812006-04-22 02:35:30 -07002385 /* close all still open channels */
2386 if (atomic_read(&ucs->basstate) & BS_B1OPEN) {
2387 gig_dbg(DEBUG_INIT, "closing B1 channel");
2388 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2389 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ, 0, 0,
2390 NULL, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002391 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002392 if (atomic_read(&ucs->basstate) & BS_B2OPEN) {
2393 gig_dbg(DEBUG_INIT, "closing B2 channel");
2394 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2395 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ, 0, 0,
2396 NULL, 0, BAS_TIMEOUT);
2397 }
2398 if (atomic_read(&ucs->basstate) & BS_ATOPEN) {
2399 gig_dbg(DEBUG_INIT, "closing AT channel");
2400 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2401 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ, 0, 0,
2402 NULL, 0, BAS_TIMEOUT);
2403 }
2404 atomic_set(&ucs->basstate, 0);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002405
2406 /* deregister this driver with the USB subsystem */
2407 usb_deregister(&gigaset_usb_driver);
2408 /* this will call the disconnect-callback */
2409 /* from now on, no disconnect/probe callback should be running */
2410
2411 gigaset_freecs(cardstate);
2412 cardstate = NULL;
2413 gigaset_freedriver(driver);
2414 driver = NULL;
2415}
2416
2417
2418module_init(bas_gigaset_init);
2419module_exit(bas_gigaset_exit);
2420
2421MODULE_AUTHOR(DRIVER_AUTHOR);
2422MODULE_DESCRIPTION(DRIVER_DESC);
2423MODULE_LICENSE("GPL");