1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "gigaset.h"
17#include <linux/usb.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20
21
22#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
23#define DRIVER_DESC "USB Driver for Gigaset 307x"
24
25
26
27
28static int startmode = SM_ISDN;
29static int cidmode = 1;
30
31module_param(startmode, int, S_IRUGO);
32module_param(cidmode, int, S_IRUGO);
33MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
34MODULE_PARM_DESC(cidmode, "Call-ID mode");
35
36#define GIGASET_MINORS 1
37#define GIGASET_MINOR 16
38#define GIGASET_MODULENAME "bas_gigaset"
39#define GIGASET_DEVNAME "ttyGB"
40
41
42#define IF_WRITEBUF 264
43
44
45#define IP_MSGSIZE 3
46
47
48#define USB_GIGA_VENDOR_ID 0x0681
49#define USB_3070_PRODUCT_ID 0x0001
50#define USB_3075_PRODUCT_ID 0x0002
51#define USB_SX303_PRODUCT_ID 0x0021
52#define USB_SX353_PRODUCT_ID 0x0022
53
54
55static const struct usb_device_id gigaset_table[] = {
56 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
57 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
58 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
60 { }
61};
62
63MODULE_DEVICE_TABLE(usb, gigaset_table);
64
65
66
67
68static int gigaset_probe(struct usb_interface *interface,
69 const struct usb_device_id *id);
70
71
72static void gigaset_disconnect(struct usb_interface *interface);
73
74
75static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
76static int gigaset_resume(struct usb_interface *intf);
77
78
79static int gigaset_pre_reset(struct usb_interface *intf);
80static int gigaset_post_reset(struct usb_interface *intf);
81
82static int atread_submit(struct cardstate *, int);
83static void stopurbs(struct bas_bc_state *);
84static int req_submit(struct bc_state *, int, int, int);
85static int atwrite_submit(struct cardstate *, unsigned char *, int);
86static int start_cbsend(struct cardstate *);
87
88
89
90struct bas_cardstate {
91 struct usb_device *udev;
92 struct cardstate *cs;
93 struct usb_interface *interface;
94 unsigned char minor;
95
96 struct urb *urb_ctrl;
97 struct usb_ctrlrequest dr_ctrl;
98 struct timer_list timer_ctrl;
99 int retry_ctrl;
100
101 struct timer_list timer_atrdy;
102 struct urb *urb_cmd_out;
103 struct usb_ctrlrequest dr_cmd_out;
104 int retry_cmd_out;
105
106 struct urb *urb_cmd_in;
107 struct usb_ctrlrequest dr_cmd_in;
108 struct timer_list timer_cmd_in;
109 unsigned char *rcvbuf;
110
111 struct urb *urb_int_in;
112 unsigned char *int_in_buf;
113 struct work_struct int_in_wq;
114 struct timer_list timer_int_in;
115 int retry_int_in;
116
117 spinlock_t lock;
118 int basstate;
119 int pending;
120 wait_queue_head_t waitqueue;
121 int rcvbuf_size;
122
123 int retry_cmd_in;
124};
125
126
127#define BS_ATOPEN 0x001
128#define BS_B1OPEN 0x002
129#define BS_B2OPEN 0x004
130#define BS_ATREADY 0x008
131#define BS_INIT 0x010
132#define BS_ATTIMER 0x020
133#define BS_ATRDPEND 0x040
134#define BS_ATWRPEND 0x080
135#define BS_SUSPEND 0x100
136#define BS_RESETTING 0x200
137
138
139static struct gigaset_driver *driver;
140
141
142static struct usb_driver gigaset_usb_driver = {
143 .name = GIGASET_MODULENAME,
144 .probe = gigaset_probe,
145 .disconnect = gigaset_disconnect,
146 .id_table = gigaset_table,
147 .suspend = gigaset_suspend,
148 .resume = gigaset_resume,
149 .reset_resume = gigaset_post_reset,
150 .pre_reset = gigaset_pre_reset,
151 .post_reset = gigaset_post_reset,
152 .disable_hub_initiated_lpm = 1,
153};
154
155
156
157static char *get_usb_rcmsg(int rc)
158{
159 static char unkmsg[28];
160
161 switch (rc) {
162 case 0:
163 return "success";
164 case -ENOMEM:
165 return "out of memory";
166 case -ENODEV:
167 return "device not present";
168 case -ENOENT:
169 return "endpoint not present";
170 case -ENXIO:
171 return "URB type not supported";
172 case -EINVAL:
173 return "invalid argument";
174 case -EAGAIN:
175 return "start frame too early or too much scheduled";
176 case -EFBIG:
177 return "too many isoc frames requested";
178 case -EPIPE:
179 return "endpoint stalled";
180 case -EMSGSIZE:
181 return "invalid packet size";
182 case -ENOSPC:
183 return "would overcommit USB bandwidth";
184 case -ESHUTDOWN:
185 return "device shut down";
186 case -EPERM:
187 return "reject flag set";
188 case -EHOSTUNREACH:
189 return "device suspended";
190 default:
191 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
192 return unkmsg;
193 }
194}
195
196
197
198static char *get_usb_statmsg(int status)
199{
200 static char unkmsg[28];
201
202 switch (status) {
203 case 0:
204 return "success";
205 case -ENOENT:
206 return "unlinked (sync)";
207 case -EINPROGRESS:
208 return "URB still pending";
209 case -EPROTO:
210 return "bitstuff error, timeout, or unknown USB error";
211 case -EILSEQ:
212 return "CRC mismatch, timeout, or unknown USB error";
213 case -ETIME:
214 return "USB response timeout";
215 case -EPIPE:
216 return "endpoint stalled";
217 case -ECOMM:
218 return "IN buffer overrun";
219 case -ENOSR:
220 return "OUT buffer underrun";
221 case -EOVERFLOW:
222 return "endpoint babble";
223 case -EREMOTEIO:
224 return "short packet";
225 case -ENODEV:
226 return "device removed";
227 case -EXDEV:
228 return "partial isoc transfer";
229 case -EINVAL:
230 return "ISO madness";
231 case -ECONNRESET:
232 return "unlinked (async)";
233 case -ESHUTDOWN:
234 return "device shut down";
235 default:
236 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
237 return unkmsg;
238 }
239}
240
241
242
243
244static inline char *usb_pipetype_str(int pipe)
245{
246 if (usb_pipeisoc(pipe))
247 return "Isoc";
248 if (usb_pipeint(pipe))
249 return "Int";
250 if (usb_pipecontrol(pipe))
251 return "Ctrl";
252 if (usb_pipebulk(pipe))
253 return "Bulk";
254 return "?";
255}
256
257
258
259
260static inline void dump_urb(enum debuglevel level, const char *tag,
261 struct urb *urb)
262{
263#ifdef CONFIG_GIGASET_DEBUG
264 int i;
265 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
266 if (urb) {
267 gig_dbg(level,
268 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
269 "hcpriv=0x%08lx, transfer_flags=0x%x,",
270 (unsigned long) urb->dev,
271 usb_pipetype_str(urb->pipe),
272 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
273 usb_pipein(urb->pipe) ? "in" : "out",
274 (unsigned long) urb->hcpriv,
275 urb->transfer_flags);
276 gig_dbg(level,
277 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
278 "setup_packet=0x%08lx,",
279 (unsigned long) urb->transfer_buffer,
280 urb->transfer_buffer_length, urb->actual_length,
281 (unsigned long) urb->setup_packet);
282 gig_dbg(level,
283 " start_frame=%d, number_of_packets=%d, interval=%d, "
284 "error_count=%d,",
285 urb->start_frame, urb->number_of_packets, urb->interval,
286 urb->error_count);
287 gig_dbg(level,
288 " context=0x%08lx, complete=0x%08lx, "
289 "iso_frame_desc[]={",
290 (unsigned long) urb->context,
291 (unsigned long) urb->complete);
292 for (i = 0; i < urb->number_of_packets; i++) {
293 struct usb_iso_packet_descriptor *pifd
294 = &urb->iso_frame_desc[i];
295 gig_dbg(level,
296 " {offset=%u, length=%u, actual_length=%u, "
297 "status=%u}",
298 pifd->offset, pifd->length, pifd->actual_length,
299 pifd->status);
300 }
301 }
302 gig_dbg(level, "}}");
303#endif
304}
305
306
307static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
308 unsigned new_state)
309{
310 return -EINVAL;
311}
312
313static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
314{
315 return -EINVAL;
316}
317
318static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
319{
320 return -EINVAL;
321}
322
323
324
325static inline int update_basstate(struct bas_cardstate *ucs,
326 int set, int clear)
327{
328 unsigned long flags;
329 int state;
330
331 spin_lock_irqsave(&ucs->lock, flags);
332 state = ucs->basstate;
333 ucs->basstate = (state & ~clear) | set;
334 spin_unlock_irqrestore(&ucs->lock, flags);
335 return state;
336}
337
338
339
340
341
342
343
344
345
346static inline void error_hangup(struct bc_state *bcs)
347{
348 struct cardstate *cs = bcs->cs;
349
350 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
351 gigaset_schedule_event(cs);
352}
353
354
355
356
357
358
359
360
361
362static inline void error_reset(struct cardstate *cs)
363{
364
365 update_basstate(cs->hw.bas, BS_RESETTING, 0);
366 if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))
367
368 usb_queue_reset_device(cs->hw.bas->interface);
369}
370
371
372
373
374
375
376static void check_pending(struct bas_cardstate *ucs)
377{
378 unsigned long flags;
379
380 spin_lock_irqsave(&ucs->lock, flags);
381 switch (ucs->pending) {
382 case 0:
383 break;
384 case HD_OPEN_ATCHANNEL:
385 if (ucs->basstate & BS_ATOPEN)
386 ucs->pending = 0;
387 break;
388 case HD_OPEN_B1CHANNEL:
389 if (ucs->basstate & BS_B1OPEN)
390 ucs->pending = 0;
391 break;
392 case HD_OPEN_B2CHANNEL:
393 if (ucs->basstate & BS_B2OPEN)
394 ucs->pending = 0;
395 break;
396 case HD_CLOSE_ATCHANNEL:
397 if (!(ucs->basstate & BS_ATOPEN))
398 ucs->pending = 0;
399 break;
400 case HD_CLOSE_B1CHANNEL:
401 if (!(ucs->basstate & BS_B1OPEN))
402 ucs->pending = 0;
403 break;
404 case HD_CLOSE_B2CHANNEL:
405 if (!(ucs->basstate & BS_B2OPEN))
406 ucs->pending = 0;
407 break;
408 case HD_DEVICE_INIT_ACK:
409 ucs->pending = 0;
410 break;
411 case HD_RESET_INTERRUPT_PIPE:
412 if (!(ucs->basstate & BS_RESETTING))
413 ucs->pending = 0;
414 break;
415
416
417
418
419 default:
420 dev_warn(&ucs->interface->dev,
421 "unknown pending request 0x%02x cleared\n",
422 ucs->pending);
423 ucs->pending = 0;
424 }
425
426 if (!ucs->pending)
427 del_timer(&ucs->timer_ctrl);
428
429 spin_unlock_irqrestore(&ucs->lock, flags);
430}
431
432
433
434
435
436
437static void cmd_in_timeout(struct timer_list *t)
438{
439 struct bas_cardstate *ucs = from_timer(ucs, t, timer_cmd_in);
440 struct cardstate *cs = ucs->cs;
441 int rc;
442
443 if (!ucs->rcvbuf_size) {
444 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
445 return;
446 }
447
448 if (ucs->retry_cmd_in++ >= BAS_RETRY) {
449 dev_err(cs->dev,
450 "control read: timeout, giving up after %d tries\n",
451 ucs->retry_cmd_in);
452 kfree(ucs->rcvbuf);
453 ucs->rcvbuf = NULL;
454 ucs->rcvbuf_size = 0;
455 error_reset(cs);
456 return;
457 }
458
459 gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
460 __func__, ucs->retry_cmd_in);
461 rc = atread_submit(cs, BAS_TIMEOUT);
462 if (rc < 0) {
463 kfree(ucs->rcvbuf);
464 ucs->rcvbuf = NULL;
465 ucs->rcvbuf_size = 0;
466 if (rc != -ENODEV)
467 error_reset(cs);
468 }
469}
470
471
472
473
474
475
476
477
478static void read_ctrl_callback(struct urb *urb)
479{
480 struct inbuf_t *inbuf = urb->context;
481 struct cardstate *cs = inbuf->cs;
482 struct bas_cardstate *ucs = cs->hw.bas;
483 int status = urb->status;
484 unsigned numbytes;
485 int rc;
486
487 update_basstate(ucs, 0, BS_ATRDPEND);
488 wake_up(&ucs->waitqueue);
489 del_timer(&ucs->timer_cmd_in);
490
491 switch (status) {
492 case 0:
493 numbytes = urb->actual_length;
494 if (unlikely(numbytes != ucs->rcvbuf_size)) {
495 dev_warn(cs->dev,
496 "control read: received %d chars, expected %d\n",
497 numbytes, ucs->rcvbuf_size);
498 if (numbytes > ucs->rcvbuf_size)
499 numbytes = ucs->rcvbuf_size;
500 }
501
502
503 if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
504 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
505 gigaset_schedule_event(cs);
506 }
507 break;
508
509 case -ENOENT:
510 case -ECONNRESET:
511 case -EINPROGRESS:
512 case -ENODEV:
513 case -ESHUTDOWN:
514
515 gig_dbg(DEBUG_USBREQ, "%s: %s",
516 __func__, get_usb_statmsg(status));
517 break;
518
519 default:
520 if (ucs->retry_cmd_in++ < BAS_RETRY) {
521 gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
522 get_usb_statmsg(status), ucs->retry_cmd_in);
523 rc = atread_submit(cs, BAS_TIMEOUT);
524 if (rc >= 0)
525
526 return;
527 if (rc == -ENODEV)
528
529 break;
530 }
531 dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
532 get_usb_statmsg(status), ucs->retry_cmd_in);
533 error_reset(cs);
534 }
535
536
537 kfree(ucs->rcvbuf);
538 ucs->rcvbuf = NULL;
539 ucs->rcvbuf_size = 0;
540}
541
542
543
544
545
546
547
548
549
550
551
552static int atread_submit(struct cardstate *cs, int timeout)
553{
554 struct bas_cardstate *ucs = cs->hw.bas;
555 int basstate;
556 int ret;
557
558 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
559 ucs->rcvbuf_size);
560
561 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
562 if (basstate & BS_ATRDPEND) {
563 dev_err(cs->dev,
564 "could not submit HD_READ_ATMESSAGE: URB busy\n");
565 return -EBUSY;
566 }
567
568 if (basstate & BS_SUSPEND) {
569 dev_notice(cs->dev,
570 "HD_READ_ATMESSAGE not submitted, "
571 "suspend in progress\n");
572 update_basstate(ucs, 0, BS_ATRDPEND);
573
574 return -ENODEV;
575 }
576
577 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
578 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
579 ucs->dr_cmd_in.wValue = 0;
580 ucs->dr_cmd_in.wIndex = 0;
581 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
582 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
583 usb_rcvctrlpipe(ucs->udev, 0),
584 (unsigned char *) &ucs->dr_cmd_in,
585 ucs->rcvbuf, ucs->rcvbuf_size,
586 read_ctrl_callback, cs->inbuf);
587
588 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
589 if (ret != 0) {
590 update_basstate(ucs, 0, BS_ATRDPEND);
591 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
592 get_usb_rcmsg(ret));
593 return ret;
594 }
595
596 if (timeout > 0) {
597 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
598 mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
599 }
600 return 0;
601}
602
603
604
605
606
607static void int_in_work(struct work_struct *work)
608{
609 struct bas_cardstate *ucs =
610 container_of(work, struct bas_cardstate, int_in_wq);
611 struct urb *urb = ucs->urb_int_in;
612 struct cardstate *cs = urb->context;
613 int rc;
614
615
616 rc = usb_clear_halt(ucs->udev, urb->pipe);
617 gig_dbg(DEBUG_USBREQ, "clear_halt: %s", get_usb_rcmsg(rc));
618 if (rc == 0)
619
620 rc = usb_submit_urb(urb, GFP_ATOMIC);
621
622 switch (rc) {
623 case 0:
624 case -ENODEV:
625 case -EINVAL:
626 break;
627 default:
628 dev_err(cs->dev, "clear halt failed: %s\n", get_usb_rcmsg(rc));
629 rc = usb_lock_device_for_reset(ucs->udev, ucs->interface);
630 if (rc == 0) {
631 rc = usb_reset_device(ucs->udev);
632 usb_unlock_device(ucs->udev);
633 }
634 }
635 ucs->retry_int_in = 0;
636}
637
638
639
640
641
642
643static void int_in_resubmit(struct timer_list *t)
644{
645 struct bas_cardstate *ucs = from_timer(ucs, t, timer_int_in);
646 struct cardstate *cs = ucs->cs;
647 int rc;
648
649 if (ucs->retry_int_in++ >= BAS_RETRY) {
650 dev_err(cs->dev, "interrupt read: giving up after %d tries\n",
651 ucs->retry_int_in);
652 usb_queue_reset_device(ucs->interface);
653 return;
654 }
655
656 gig_dbg(DEBUG_USBREQ, "%s: retry %d", __func__, ucs->retry_int_in);
657 rc = usb_submit_urb(ucs->urb_int_in, GFP_ATOMIC);
658 if (rc != 0 && rc != -ENODEV) {
659 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
660 get_usb_rcmsg(rc));
661 usb_queue_reset_device(ucs->interface);
662 }
663}
664
665
666
667
668
669
670
671
672static void read_int_callback(struct urb *urb)
673{
674 struct cardstate *cs = urb->context;
675 struct bas_cardstate *ucs = cs->hw.bas;
676 struct bc_state *bcs;
677 int status = urb->status;
678 unsigned long flags;
679 int rc;
680 unsigned l;
681 int channel;
682
683 switch (status) {
684 case 0:
685 ucs->retry_int_in = 0;
686 break;
687 case -EPIPE:
688 schedule_work(&ucs->int_in_wq);
689
690 case -ENOENT:
691 case -ECONNRESET:
692 case -EINPROGRESS:
693 case -ENODEV:
694 case -ESHUTDOWN:
695
696 gig_dbg(DEBUG_USBREQ, "%s: %s",
697 __func__, get_usb_statmsg(status));
698 return;
699 case -EPROTO:
700 case -EILSEQ:
701 case -ETIME:
702
703 gig_dbg(DEBUG_USBREQ, "%s: %s",
704 __func__, get_usb_statmsg(status));
705 mod_timer(&ucs->timer_int_in, jiffies + HZ / 10);
706 return;
707 default:
708 dev_warn(cs->dev, "interrupt read: %s\n",
709 get_usb_statmsg(status));
710 goto resubmit;
711 }
712
713
714 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
715 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
716 urb->actual_length);
717 goto resubmit;
718 }
719
720 l = (unsigned) ucs->int_in_buf[1] +
721 (((unsigned) ucs->int_in_buf[2]) << 8);
722
723 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
724 urb->actual_length, (int)ucs->int_in_buf[0], l,
725 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
726
727 channel = 0;
728
729 switch (ucs->int_in_buf[0]) {
730 case HD_DEVICE_INIT_OK:
731 update_basstate(ucs, BS_INIT, 0);
732 break;
733
734 case HD_READY_SEND_ATDATA:
735 del_timer(&ucs->timer_atrdy);
736 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
737 start_cbsend(cs);
738 break;
739
740 case HD_OPEN_B2CHANNEL_ACK:
741 ++channel;
742
743 case HD_OPEN_B1CHANNEL_ACK:
744 bcs = cs->bcs + channel;
745 update_basstate(ucs, BS_B1OPEN << channel, 0);
746 gigaset_bchannel_up(bcs);
747 break;
748
749 case HD_OPEN_ATCHANNEL_ACK:
750 update_basstate(ucs, BS_ATOPEN, 0);
751 start_cbsend(cs);
752 break;
753
754 case HD_CLOSE_B2CHANNEL_ACK:
755 ++channel;
756
757 case HD_CLOSE_B1CHANNEL_ACK:
758 bcs = cs->bcs + channel;
759 update_basstate(ucs, 0, BS_B1OPEN << channel);
760 stopurbs(bcs->hw.bas);
761 gigaset_bchannel_down(bcs);
762 break;
763
764 case HD_CLOSE_ATCHANNEL_ACK:
765 update_basstate(ucs, 0, BS_ATOPEN);
766 break;
767
768 case HD_B2_FLOW_CONTROL:
769 ++channel;
770
771 case HD_B1_FLOW_CONTROL:
772 bcs = cs->bcs + channel;
773 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
774 &bcs->hw.bas->corrbytes);
775 gig_dbg(DEBUG_ISO,
776 "Flow control (channel %d, sub %d): 0x%02x => %d",
777 channel, bcs->hw.bas->numsub, l,
778 atomic_read(&bcs->hw.bas->corrbytes));
779 break;
780
781 case HD_RECEIVEATDATA_ACK:
782 if (!l) {
783 dev_warn(cs->dev,
784 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
785 break;
786 }
787 spin_lock_irqsave(&cs->lock, flags);
788 if (ucs->basstate & BS_ATRDPEND) {
789 spin_unlock_irqrestore(&cs->lock, flags);
790 dev_warn(cs->dev,
791 "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
792 l, ucs->rcvbuf_size);
793 break;
794 }
795 if (ucs->rcvbuf_size) {
796
797 dev_err(cs->dev,
798 "receive AT data overrun, %d bytes lost\n",
799 ucs->rcvbuf_size);
800 kfree(ucs->rcvbuf);
801 ucs->rcvbuf_size = 0;
802 }
803 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
804 if (ucs->rcvbuf == NULL) {
805 spin_unlock_irqrestore(&cs->lock, flags);
806 dev_err(cs->dev, "out of memory receiving AT data\n");
807 break;
808 }
809 ucs->rcvbuf_size = l;
810 ucs->retry_cmd_in = 0;
811 rc = atread_submit(cs, BAS_TIMEOUT);
812 if (rc < 0) {
813 kfree(ucs->rcvbuf);
814 ucs->rcvbuf = NULL;
815 ucs->rcvbuf_size = 0;
816 }
817 spin_unlock_irqrestore(&cs->lock, flags);
818 if (rc < 0 && rc != -ENODEV)
819 error_reset(cs);
820 break;
821
822 case HD_RESET_INTERRUPT_PIPE_ACK:
823 update_basstate(ucs, 0, BS_RESETTING);
824 dev_notice(cs->dev, "interrupt pipe reset\n");
825 break;
826
827 case HD_SUSPEND_END:
828 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
829 break;
830
831 default:
832 dev_warn(cs->dev,
833 "unknown Gigaset signal 0x%02x (%u) ignored\n",
834 (int) ucs->int_in_buf[0], l);
835 }
836
837 check_pending(ucs);
838 wake_up(&ucs->waitqueue);
839
840resubmit:
841 rc = usb_submit_urb(urb, GFP_ATOMIC);
842 if (unlikely(rc != 0 && rc != -ENODEV)) {
843 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
844 get_usb_rcmsg(rc));
845 error_reset(cs);
846 }
847}
848
849
850
851
852
853
854
855
856static void read_iso_callback(struct urb *urb)
857{
858 struct bc_state *bcs;
859 struct bas_bc_state *ubc;
860 int status = urb->status;
861 unsigned long flags;
862 int i, rc;
863
864
865 if (unlikely(status == -ENOENT ||
866 status == -ECONNRESET ||
867 status == -EINPROGRESS ||
868 status == -ENODEV ||
869 status == -ESHUTDOWN)) {
870 gig_dbg(DEBUG_ISO, "%s: %s",
871 __func__, get_usb_statmsg(status));
872 return;
873 }
874
875 bcs = urb->context;
876 ubc = bcs->hw.bas;
877
878 spin_lock_irqsave(&ubc->isoinlock, flags);
879 if (likely(ubc->isoindone == NULL)) {
880
881 ubc->isoindone = urb;
882 ubc->isoinstatus = status;
883 tasklet_hi_schedule(&ubc->rcvd_tasklet);
884 } else {
885
886 gig_dbg(DEBUG_ISO, "%s: overrun", __func__);
887 ubc->loststatus = status;
888 for (i = 0; i < BAS_NUMFRAMES; i++) {
889 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
890 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
891 urb->iso_frame_desc[i].status != -EINPROGRESS))
892 ubc->loststatus = urb->iso_frame_desc[i].status;
893 urb->iso_frame_desc[i].status = 0;
894 urb->iso_frame_desc[i].actual_length = 0;
895 }
896 if (likely(ubc->running)) {
897
898 urb->dev = bcs->cs->hw.bas->udev;
899 urb->transfer_flags = URB_ISO_ASAP;
900 urb->number_of_packets = BAS_NUMFRAMES;
901 rc = usb_submit_urb(urb, GFP_ATOMIC);
902 if (unlikely(rc != 0 && rc != -ENODEV)) {
903 dev_err(bcs->cs->dev,
904 "could not resubmit isoc read URB: %s\n",
905 get_usb_rcmsg(rc));
906 dump_urb(DEBUG_ISO, "isoc read", urb);
907 error_hangup(bcs);
908 }
909 }
910 }
911 spin_unlock_irqrestore(&ubc->isoinlock, flags);
912}
913
914
915
916
917
918
919
920
921static void write_iso_callback(struct urb *urb)
922{
923 struct isow_urbctx_t *ucx;
924 struct bas_bc_state *ubc;
925 int status = urb->status;
926 unsigned long flags;
927
928
929 if (unlikely(status == -ENOENT ||
930 status == -ECONNRESET ||
931 status == -EINPROGRESS ||
932 status == -ENODEV ||
933 status == -ESHUTDOWN)) {
934 gig_dbg(DEBUG_ISO, "%s: %s",
935 __func__, get_usb_statmsg(status));
936 return;
937 }
938
939
940 ucx = urb->context;
941 ubc = ucx->bcs->hw.bas;
942 ucx->status = status;
943
944 spin_lock_irqsave(&ubc->isooutlock, flags);
945 ubc->isooutovfl = ubc->isooutdone;
946 ubc->isooutdone = ucx;
947 spin_unlock_irqrestore(&ubc->isooutlock, flags);
948 tasklet_hi_schedule(&ubc->sent_tasklet);
949}
950
951
952
953
954
955
956
957
958
959static int starturbs(struct bc_state *bcs)
960{
961 struct bas_bc_state *ubc = bcs->hw.bas;
962 struct urb *urb;
963 int j, k;
964 int rc;
965
966
967 if (bcs->proto2 == L2_HDLC)
968 bcs->inputstate |= INS_flag_hunt;
969
970
971 ubc->running = 1;
972 for (k = 0; k < BAS_INURBS; k++) {
973 urb = ubc->isoinurbs[k];
974 if (!urb) {
975 rc = -EFAULT;
976 goto error;
977 }
978 usb_fill_int_urb(urb, bcs->cs->hw.bas->udev,
979 usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel),
980 ubc->isoinbuf + k * BAS_INBUFSIZE,
981 BAS_INBUFSIZE, read_iso_callback, bcs,
982 BAS_FRAMETIME);
983
984 urb->transfer_flags = URB_ISO_ASAP;
985 urb->number_of_packets = BAS_NUMFRAMES;
986 for (j = 0; j < BAS_NUMFRAMES; j++) {
987 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
988 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
989 urb->iso_frame_desc[j].status = 0;
990 urb->iso_frame_desc[j].actual_length = 0;
991 }
992
993 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
994 rc = usb_submit_urb(urb, GFP_ATOMIC);
995 if (rc != 0)
996 goto error;
997 }
998
999
1000 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
1001
1002
1003 for (k = 0; k < BAS_OUTURBS; ++k) {
1004 urb = ubc->isoouturbs[k].urb;
1005 if (!urb) {
1006 rc = -EFAULT;
1007 goto error;
1008 }
1009 usb_fill_int_urb(urb, bcs->cs->hw.bas->udev,
1010 usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel),
1011 ubc->isooutbuf->data,
1012 sizeof(ubc->isooutbuf->data),
1013 write_iso_callback, &ubc->isoouturbs[k],
1014 BAS_FRAMETIME);
1015
1016 urb->transfer_flags = URB_ISO_ASAP;
1017 urb->number_of_packets = BAS_NUMFRAMES;
1018 for (j = 0; j < BAS_NUMFRAMES; ++j) {
1019 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
1020 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
1021 urb->iso_frame_desc[j].status = 0;
1022 urb->iso_frame_desc[j].actual_length = 0;
1023 }
1024 ubc->isoouturbs[k].limit = -1;
1025 }
1026
1027
1028 for (k = 0; k < BAS_OUTURBS - 1; ++k) {
1029 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
1030 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
1031 if (rc != 0)
1032 goto error;
1033 }
1034 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
1035 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS - 1];
1036 ubc->isooutdone = ubc->isooutovfl = NULL;
1037 return 0;
1038error:
1039 stopurbs(ubc);
1040 return rc;
1041}
1042
1043
1044
1045
1046
1047
1048
1049static void stopurbs(struct bas_bc_state *ubc)
1050{
1051 int k, rc;
1052
1053 ubc->running = 0;
1054
1055 for (k = 0; k < BAS_INURBS; ++k) {
1056 rc = usb_unlink_urb(ubc->isoinurbs[k]);
1057 gig_dbg(DEBUG_ISO,
1058 "%s: isoc input URB %d unlinked, result = %s",
1059 __func__, k, get_usb_rcmsg(rc));
1060 }
1061
1062 for (k = 0; k < BAS_OUTURBS; ++k) {
1063 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
1064 gig_dbg(DEBUG_ISO,
1065 "%s: isoc output URB %d unlinked, result = %s",
1066 __func__, k, get_usb_rcmsg(rc));
1067 }
1068}
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1083{
1084 struct urb *urb = ucx->urb;
1085 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
1086 struct usb_iso_packet_descriptor *ifd;
1087 int corrbytes, nframe, rc;
1088
1089
1090 urb->dev = ucx->bcs->cs->hw.bas->udev;
1091 urb->transfer_flags = URB_ISO_ASAP;
1092 urb->transfer_buffer = ubc->isooutbuf->data;
1093 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1094
1095 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1096 ifd = &urb->iso_frame_desc[nframe];
1097
1098
1099 ifd->length = BAS_NORMFRAME;
1100 corrbytes = atomic_read(&ubc->corrbytes);
1101 if (corrbytes != 0) {
1102 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1103 __func__, corrbytes);
1104 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1105 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1106 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1107 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1108 ifd->length += corrbytes;
1109 atomic_add(-corrbytes, &ubc->corrbytes);
1110 }
1111
1112
1113 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1114 if (rc < 0) {
1115 if (rc == -EBUSY) {
1116 gig_dbg(DEBUG_ISO,
1117 "%s: buffer busy at frame %d",
1118 __func__, nframe);
1119
1120
1121 } else {
1122 dev_err(ucx->bcs->cs->dev,
1123 "%s: buffer error %d at frame %d\n",
1124 __func__, rc, nframe);
1125 return rc;
1126 }
1127 break;
1128 }
1129 ifd->offset = rc;
1130 ucx->limit = ubc->isooutbuf->nextread;
1131 ifd->status = 0;
1132 ifd->actual_length = 0;
1133 }
1134 if (unlikely(nframe == 0))
1135 return 0;
1136 urb->number_of_packets = nframe;
1137
1138 rc = usb_submit_urb(urb, GFP_ATOMIC);
1139 if (unlikely(rc)) {
1140 if (rc == -ENODEV)
1141
1142 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1143 else
1144 dev_err(ucx->bcs->cs->dev,
1145 "could not submit isoc write URB: %s\n",
1146 get_usb_rcmsg(rc));
1147 return rc;
1148 }
1149 ++ubc->numsub;
1150 return nframe;
1151}
1152
1153
1154
1155
1156
1157
1158
1159static void write_iso_tasklet(unsigned long data)
1160{
1161 struct bc_state *bcs = (struct bc_state *) data;
1162 struct bas_bc_state *ubc = bcs->hw.bas;
1163 struct cardstate *cs = bcs->cs;
1164 struct isow_urbctx_t *done, *next, *ovfl;
1165 struct urb *urb;
1166 int status;
1167 struct usb_iso_packet_descriptor *ifd;
1168 unsigned long flags;
1169 int i;
1170 struct sk_buff *skb;
1171 int len;
1172 int rc;
1173
1174
1175 for (;;) {
1176 if (unlikely(!(ubc->running))) {
1177 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1178 return;
1179 }
1180
1181
1182 spin_lock_irqsave(&ubc->isooutlock, flags);
1183 done = ubc->isooutdone;
1184 ubc->isooutdone = NULL;
1185 ovfl = ubc->isooutovfl;
1186 ubc->isooutovfl = NULL;
1187 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1188 if (ovfl) {
1189 dev_err(cs->dev, "isoc write underrun\n");
1190 error_hangup(bcs);
1191 break;
1192 }
1193 if (!done)
1194 break;
1195
1196
1197 spin_lock_irqsave(&ubc->isooutlock, flags);
1198 next = ubc->isooutfree;
1199 ubc->isooutfree = NULL;
1200 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1201 if (next) {
1202 rc = submit_iso_write_urb(next);
1203 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1204
1205 spin_lock_irqsave(&ubc->isooutlock, flags);
1206 if (ubc->isooutfree == NULL) {
1207 ubc->isooutfree = next;
1208 next = NULL;
1209 }
1210 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1211 if (next) {
1212
1213 dev_err(cs->dev,
1214 "losing isoc write URB\n");
1215 error_hangup(bcs);
1216 }
1217 }
1218 }
1219
1220
1221 urb = done->urb;
1222 status = done->status;
1223 switch (status) {
1224 case -EXDEV:
1225 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1226 __func__);
1227
1228 case 0:
1229
1230
1231
1232
1233
1234
1235 for (i = 0; i < BAS_NUMFRAMES; i++) {
1236 ifd = &urb->iso_frame_desc[i];
1237 if (ifd->status ||
1238 ifd->actual_length != ifd->length) {
1239 dev_warn(cs->dev,
1240 "isoc write: frame %d[%d/%d]: %s\n",
1241 i, ifd->actual_length,
1242 ifd->length,
1243 get_usb_statmsg(ifd->status));
1244 break;
1245 }
1246 }
1247 break;
1248 case -EPIPE:
1249 dev_err(cs->dev, "isoc write: stalled\n");
1250 error_hangup(bcs);
1251 break;
1252 default:
1253 dev_warn(cs->dev, "isoc write: %s\n",
1254 get_usb_statmsg(status));
1255 }
1256
1257
1258 if (done->limit >= 0)
1259 ubc->isooutbuf->read = done->limit;
1260
1261
1262 spin_lock_irqsave(&ubc->isooutlock, flags);
1263 next = ubc->isooutfree;
1264 ubc->isooutfree = done;
1265 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1266 if (next) {
1267
1268 rc = submit_iso_write_urb(next);
1269 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1270
1271 error_hangup(bcs);
1272 }
1273 }
1274 }
1275
1276
1277 while ((skb = skb_dequeue(&bcs->squeue))) {
1278
1279 len = skb->len;
1280 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1281
1282 skb_queue_head(&bcs->squeue, skb);
1283 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1284 __func__, skb_queue_len(&bcs->squeue));
1285 break;
1286 }
1287 skb_pull(skb, len);
1288 gigaset_skb_sent(bcs, skb);
1289 dev_kfree_skb_any(skb);
1290 }
1291}
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302static void read_iso_tasklet(unsigned long data)
1303{
1304 struct bc_state *bcs = (struct bc_state *) data;
1305 struct bas_bc_state *ubc = bcs->hw.bas;
1306 struct cardstate *cs = bcs->cs;
1307 struct urb *urb;
1308 int status;
1309 struct usb_iso_packet_descriptor *ifd;
1310 char *rcvbuf;
1311 unsigned long flags;
1312 int totleft, numbytes, offset, frame, rc;
1313
1314
1315 for (;;) {
1316
1317 spin_lock_irqsave(&ubc->isoinlock, flags);
1318 urb = ubc->isoindone;
1319 if (!urb) {
1320 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1321 return;
1322 }
1323 status = ubc->isoinstatus;
1324 ubc->isoindone = NULL;
1325 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1326 dev_warn(cs->dev,
1327 "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
1328 get_usb_statmsg(ubc->loststatus),
1329 ubc->isoinlost);
1330 ubc->loststatus = -EINPROGRESS;
1331 }
1332 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1333
1334 if (unlikely(!(ubc->running))) {
1335 gig_dbg(DEBUG_ISO,
1336 "%s: channel not running, "
1337 "dropped URB with status: %s",
1338 __func__, get_usb_statmsg(status));
1339 return;
1340 }
1341
1342 switch (status) {
1343 case 0:
1344 break;
1345 case -EXDEV:
1346
1347 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1348 __func__);
1349 break;
1350 case -ENOENT:
1351 case -ECONNRESET:
1352 case -EINPROGRESS:
1353 gig_dbg(DEBUG_ISO, "%s: %s",
1354 __func__, get_usb_statmsg(status));
1355 continue;
1356 case -EPIPE:
1357 dev_err(cs->dev, "isoc read: stalled\n");
1358 error_hangup(bcs);
1359 continue;
1360 default:
1361 dev_warn(cs->dev, "isoc read: %s\n",
1362 get_usb_statmsg(status));
1363 goto error;
1364 }
1365
1366 rcvbuf = urb->transfer_buffer;
1367 totleft = urb->actual_length;
1368 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1369 ifd = &urb->iso_frame_desc[frame];
1370 numbytes = ifd->actual_length;
1371 switch (ifd->status) {
1372 case 0:
1373 break;
1374 case -EPROTO:
1375 case -EILSEQ:
1376 case -ETIME:
1377
1378 gig_dbg(DEBUG_ISO,
1379 "isoc read: frame %d[%d]: %s\n",
1380 frame, numbytes,
1381 get_usb_statmsg(ifd->status));
1382 break;
1383 default:
1384
1385 dev_warn(cs->dev,
1386 "isoc read: frame %d[%d]: %s\n",
1387 frame, numbytes,
1388 get_usb_statmsg(ifd->status));
1389 }
1390 if (unlikely(numbytes > BAS_MAXFRAME))
1391 dev_warn(cs->dev,
1392 "isoc read: frame %d[%d]: %s\n",
1393 frame, numbytes,
1394 "exceeds max frame size");
1395 if (unlikely(numbytes > totleft)) {
1396 dev_warn(cs->dev,
1397 "isoc read: frame %d[%d]: %s\n",
1398 frame, numbytes,
1399 "exceeds total transfer length");
1400 numbytes = totleft;
1401 }
1402 offset = ifd->offset;
1403 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1404 dev_warn(cs->dev,
1405 "isoc read: frame %d[%d]: %s\n",
1406 frame, numbytes,
1407 "exceeds end of buffer");
1408 numbytes = BAS_INBUFSIZE - offset;
1409 }
1410 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1411 totleft -= numbytes;
1412 }
1413 if (unlikely(totleft > 0))
1414 dev_warn(cs->dev, "isoc read: %d data bytes missing\n",
1415 totleft);
1416
1417error:
1418
1419 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1420 urb->iso_frame_desc[frame].status = 0;
1421 urb->iso_frame_desc[frame].actual_length = 0;
1422 }
1423
1424 urb->dev = bcs->cs->hw.bas->udev;
1425 urb->transfer_flags = URB_ISO_ASAP;
1426 urb->number_of_packets = BAS_NUMFRAMES;
1427 rc = usb_submit_urb(urb, GFP_ATOMIC);
1428 if (unlikely(rc != 0 && rc != -ENODEV)) {
1429 dev_err(cs->dev,
1430 "could not resubmit isoc read URB: %s\n",
1431 get_usb_rcmsg(rc));
1432 dump_urb(DEBUG_ISO, "resubmit isoc read", urb);
1433 error_hangup(bcs);
1434 }
1435 }
1436}
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446static void req_timeout(struct timer_list *t)
1447{
1448 struct bas_cardstate *ucs = from_timer(ucs, t, timer_ctrl);
1449 struct cardstate *cs = ucs->cs;
1450 int pending;
1451 unsigned long flags;
1452
1453 check_pending(ucs);
1454
1455 spin_lock_irqsave(&ucs->lock, flags);
1456 pending = ucs->pending;
1457 ucs->pending = 0;
1458 spin_unlock_irqrestore(&ucs->lock, flags);
1459
1460 switch (pending) {
1461 case 0:
1462 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1463 break;
1464
1465 case HD_OPEN_ATCHANNEL:
1466 dev_err(cs->dev, "timeout opening AT channel\n");
1467 error_reset(cs);
1468 break;
1469
1470 case HD_OPEN_B1CHANNEL:
1471 dev_err(cs->dev, "timeout opening channel 1\n");
1472 error_hangup(&cs->bcs[0]);
1473 break;
1474
1475 case HD_OPEN_B2CHANNEL:
1476 dev_err(cs->dev, "timeout opening channel 2\n");
1477 error_hangup(&cs->bcs[1]);
1478 break;
1479
1480 case HD_CLOSE_ATCHANNEL:
1481 dev_err(cs->dev, "timeout closing AT channel\n");
1482 error_reset(cs);
1483 break;
1484
1485 case HD_CLOSE_B1CHANNEL:
1486 dev_err(cs->dev, "timeout closing channel 1\n");
1487 error_reset(cs);
1488 break;
1489
1490 case HD_CLOSE_B2CHANNEL:
1491 dev_err(cs->dev, "timeout closing channel 2\n");
1492 error_reset(cs);
1493 break;
1494
1495 case HD_RESET_INTERRUPT_PIPE:
1496
1497 dev_err(cs->dev,
1498 "reset interrupt pipe timeout, attempting USB reset\n");
1499 usb_queue_reset_device(ucs->interface);
1500 break;
1501
1502 default:
1503 dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
1504 pending);
1505 }
1506
1507 wake_up(&ucs->waitqueue);
1508}
1509
1510
1511
1512
1513
1514
1515
1516
1517static void write_ctrl_callback(struct urb *urb)
1518{
1519 struct bas_cardstate *ucs = urb->context;
1520 int status = urb->status;
1521 int rc;
1522 unsigned long flags;
1523
1524
1525 switch (status) {
1526 case 0:
1527 spin_lock_irqsave(&ucs->lock, flags);
1528 switch (ucs->pending) {
1529 case HD_DEVICE_INIT_ACK:
1530 del_timer(&ucs->timer_ctrl);
1531 ucs->pending = 0;
1532 break;
1533 }
1534 spin_unlock_irqrestore(&ucs->lock, flags);
1535 return;
1536
1537 case -ENOENT:
1538 case -ECONNRESET:
1539 case -EINPROGRESS:
1540 case -ENODEV:
1541 case -ESHUTDOWN:
1542
1543 gig_dbg(DEBUG_USBREQ, "%s: %s",
1544 __func__, get_usb_statmsg(status));
1545 break;
1546
1547 default:
1548
1549 if (++ucs->retry_ctrl > BAS_RETRY ||
1550 (ucs->basstate & BS_SUSPEND)) {
1551 dev_err(&ucs->interface->dev,
1552 "control request 0x%02x failed: %s\n",
1553 ucs->dr_ctrl.bRequest,
1554 get_usb_statmsg(status));
1555 break;
1556 }
1557 dev_notice(&ucs->interface->dev,
1558 "control request 0x%02x: %s, retry %d\n",
1559 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
1560 ucs->retry_ctrl);
1561
1562 urb->dev = ucs->udev;
1563 rc = usb_submit_urb(urb, GFP_ATOMIC);
1564 if (unlikely(rc)) {
1565 dev_err(&ucs->interface->dev,
1566 "could not resubmit request 0x%02x: %s\n",
1567 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1568 break;
1569 }
1570
1571 return;
1572 }
1573
1574
1575 spin_lock_irqsave(&ucs->lock, flags);
1576 del_timer(&ucs->timer_ctrl);
1577 ucs->pending = 0;
1578 spin_unlock_irqrestore(&ucs->lock, flags);
1579 wake_up(&ucs->waitqueue);
1580}
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1596{
1597 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1598 int ret;
1599 unsigned long flags;
1600
1601 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1602
1603 spin_lock_irqsave(&ucs->lock, flags);
1604 if (ucs->pending) {
1605 spin_unlock_irqrestore(&ucs->lock, flags);
1606 dev_err(bcs->cs->dev,
1607 "submission of request 0x%02x failed: "
1608 "request 0x%02x still pending\n",
1609 req, ucs->pending);
1610 return -EBUSY;
1611 }
1612
1613 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1614 ucs->dr_ctrl.bRequest = req;
1615 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1616 ucs->dr_ctrl.wIndex = 0;
1617 ucs->dr_ctrl.wLength = 0;
1618 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1619 usb_sndctrlpipe(ucs->udev, 0),
1620 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
1621 write_ctrl_callback, ucs);
1622 ucs->retry_ctrl = 0;
1623 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
1624 if (unlikely(ret)) {
1625 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1626 req, get_usb_rcmsg(ret));
1627 spin_unlock_irqrestore(&ucs->lock, flags);
1628 return ret;
1629 }
1630 ucs->pending = req;
1631
1632 if (timeout > 0) {
1633 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1634 mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
1635 }
1636
1637 spin_unlock_irqrestore(&ucs->lock, flags);
1638 return 0;
1639}
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649static int gigaset_init_bchannel(struct bc_state *bcs)
1650{
1651 struct cardstate *cs = bcs->cs;
1652 int req, ret;
1653 unsigned long flags;
1654
1655 spin_lock_irqsave(&cs->lock, flags);
1656 if (unlikely(!cs->connected)) {
1657 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1658 spin_unlock_irqrestore(&cs->lock, flags);
1659 return -ENODEV;
1660 }
1661
1662 if (cs->hw.bas->basstate & BS_SUSPEND) {
1663 dev_notice(cs->dev,
1664 "not starting isoc I/O, suspend in progress\n");
1665 spin_unlock_irqrestore(&cs->lock, flags);
1666 return -EHOSTUNREACH;
1667 }
1668
1669 ret = starturbs(bcs);
1670 if (ret < 0) {
1671 spin_unlock_irqrestore(&cs->lock, flags);
1672 dev_err(cs->dev,
1673 "could not start isoc I/O for channel B%d: %s\n",
1674 bcs->channel + 1,
1675 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1676 if (ret != -ENODEV)
1677 error_hangup(bcs);
1678 return ret;
1679 }
1680
1681 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1682 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1683 if (ret < 0) {
1684 dev_err(cs->dev, "could not open channel B%d\n",
1685 bcs->channel + 1);
1686 stopurbs(bcs->hw.bas);
1687 }
1688
1689 spin_unlock_irqrestore(&cs->lock, flags);
1690 if (ret < 0 && ret != -ENODEV)
1691 error_hangup(bcs);
1692 return ret;
1693}
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705static int gigaset_close_bchannel(struct bc_state *bcs)
1706{
1707 struct cardstate *cs = bcs->cs;
1708 int req, ret;
1709 unsigned long flags;
1710
1711 spin_lock_irqsave(&cs->lock, flags);
1712 if (unlikely(!cs->connected)) {
1713 spin_unlock_irqrestore(&cs->lock, flags);
1714 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1715 return -ENODEV;
1716 }
1717
1718 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1719
1720 spin_unlock_irqrestore(&cs->lock, flags);
1721 gigaset_bchannel_down(bcs);
1722 return 0;
1723 }
1724
1725
1726 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1727 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1728 if (ret < 0)
1729 dev_err(cs->dev, "closing channel B%d failed\n",
1730 bcs->channel + 1);
1731
1732 spin_unlock_irqrestore(&cs->lock, flags);
1733 return ret;
1734}
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745static void complete_cb(struct cardstate *cs)
1746{
1747 struct cmdbuf_t *cb = cs->cmdbuf;
1748
1749
1750 cs->cmdbytes -= cs->curlen;
1751 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
1752 cs->curlen, cs->cmdbytes);
1753 if (cb->next != NULL) {
1754 cs->cmdbuf = cb->next;
1755 cs->cmdbuf->prev = NULL;
1756 cs->curlen = cs->cmdbuf->len;
1757 } else {
1758 cs->cmdbuf = NULL;
1759 cs->lastcmdbuf = NULL;
1760 cs->curlen = 0;
1761 }
1762
1763 if (cb->wake_tasklet)
1764 tasklet_schedule(cb->wake_tasklet);
1765
1766 kfree(cb);
1767}
1768
1769
1770
1771
1772
1773
1774
1775
1776static void write_command_callback(struct urb *urb)
1777{
1778 struct cardstate *cs = urb->context;
1779 struct bas_cardstate *ucs = cs->hw.bas;
1780 int status = urb->status;
1781 unsigned long flags;
1782
1783 update_basstate(ucs, 0, BS_ATWRPEND);
1784 wake_up(&ucs->waitqueue);
1785
1786
1787 switch (status) {
1788 case 0:
1789 break;
1790 case -ENOENT:
1791 case -ECONNRESET:
1792 case -EINPROGRESS:
1793 case -ENODEV:
1794 case -ESHUTDOWN:
1795
1796 gig_dbg(DEBUG_USBREQ, "%s: %s",
1797 __func__, get_usb_statmsg(status));
1798 return;
1799 default:
1800 if (++ucs->retry_cmd_out > BAS_RETRY) {
1801 dev_warn(cs->dev,
1802 "command write: %s, "
1803 "giving up after %d retries\n",
1804 get_usb_statmsg(status),
1805 ucs->retry_cmd_out);
1806 break;
1807 }
1808 if (ucs->basstate & BS_SUSPEND) {
1809 dev_warn(cs->dev,
1810 "command write: %s, "
1811 "won't retry - suspend requested\n",
1812 get_usb_statmsg(status));
1813 break;
1814 }
1815 if (cs->cmdbuf == NULL) {
1816 dev_warn(cs->dev,
1817 "command write: %s, "
1818 "cannot retry - cmdbuf gone\n",
1819 get_usb_statmsg(status));
1820 break;
1821 }
1822 dev_notice(cs->dev, "command write: %s, retry %d\n",
1823 get_usb_statmsg(status), ucs->retry_cmd_out);
1824 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1825
1826 return;
1827
1828 update_basstate(ucs, BS_ATREADY, 0);
1829 }
1830
1831 spin_lock_irqsave(&cs->cmdlock, flags);
1832 if (cs->cmdbuf != NULL)
1833 complete_cb(cs);
1834 spin_unlock_irqrestore(&cs->cmdlock, flags);
1835}
1836
1837
1838
1839
1840
1841
1842static void atrdy_timeout(struct timer_list *t)
1843{
1844 struct bas_cardstate *ucs = from_timer(ucs, t, timer_atrdy);
1845 struct cardstate *cs = ucs->cs;
1846
1847 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1848
1849
1850 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1851 start_cbsend(cs);
1852}
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1866{
1867 struct bas_cardstate *ucs = cs->hw.bas;
1868 int rc;
1869
1870 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1871
1872 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1873 dev_err(cs->dev,
1874 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1875 return -EBUSY;
1876 }
1877
1878 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1879 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1880 ucs->dr_cmd_out.wValue = 0;
1881 ucs->dr_cmd_out.wIndex = 0;
1882 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1883 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1884 usb_sndctrlpipe(ucs->udev, 0),
1885 (unsigned char *) &ucs->dr_cmd_out, buf, len,
1886 write_command_callback, cs);
1887 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
1888 if (unlikely(rc)) {
1889 update_basstate(ucs, 0, BS_ATWRPEND);
1890 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1891 get_usb_rcmsg(rc));
1892 return rc;
1893 }
1894
1895
1896 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1897 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1898 ATRDY_TIMEOUT);
1899 mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
1900 }
1901 return 0;
1902}
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912static int start_cbsend(struct cardstate *cs)
1913{
1914 struct cmdbuf_t *cb;
1915 struct bas_cardstate *ucs = cs->hw.bas;
1916 unsigned long flags;
1917 int rc;
1918 int retval = 0;
1919
1920
1921 if (ucs->basstate & BS_SUSPEND) {
1922 gig_dbg(DEBUG_OUTPUT, "suspending");
1923 return -EHOSTUNREACH;
1924 }
1925
1926
1927 if (!(ucs->basstate & BS_ATOPEN)) {
1928 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
1929 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1930 if (rc < 0) {
1931
1932 spin_lock_irqsave(&cs->cmdlock, flags);
1933 while (cs->cmdbuf != NULL)
1934 complete_cb(cs);
1935 spin_unlock_irqrestore(&cs->cmdlock, flags);
1936 }
1937 return rc;
1938 }
1939
1940
1941 spin_lock_irqsave(&cs->cmdlock, flags);
1942
1943 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
1944 ucs->retry_cmd_out = 0;
1945 rc = atwrite_submit(cs, cb->buf, cb->len);
1946 if (unlikely(rc)) {
1947 retval = rc;
1948 complete_cb(cs);
1949 }
1950 }
1951
1952 spin_unlock_irqrestore(&cs->cmdlock, flags);
1953 return retval;
1954}
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
1971{
1972 unsigned long flags;
1973 int rc;
1974
1975 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
1976 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1977 "CMD Transmit", cb->len, cb->buf);
1978
1979
1980
1981
1982
1983 if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996 spin_lock_irqsave(&cs->lock, flags);
1997 if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {
1998 kfree(cs->hw.bas->rcvbuf);
1999 cs->hw.bas->rcvbuf = NULL;
2000 cs->hw.bas->rcvbuf_size = 0;
2001 cs->hw.bas->retry_cmd_in = 0;
2002 atread_submit(cs, 0);
2003 }
2004 spin_unlock_irqrestore(&cs->lock, flags);
2005
2006 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
2007 if (cb->wake_tasklet)
2008 tasklet_schedule(cb->wake_tasklet);
2009 if (!rc)
2010 rc = cb->len;
2011 kfree(cb);
2012 return rc;
2013 }
2014
2015 spin_lock_irqsave(&cs->cmdlock, flags);
2016 cb->prev = cs->lastcmdbuf;
2017 if (cs->lastcmdbuf)
2018 cs->lastcmdbuf->next = cb;
2019 else {
2020 cs->cmdbuf = cb;
2021 cs->curlen = cb->len;
2022 }
2023 cs->cmdbytes += cb->len;
2024 cs->lastcmdbuf = cb;
2025 spin_unlock_irqrestore(&cs->cmdlock, flags);
2026
2027 spin_lock_irqsave(&cs->lock, flags);
2028 if (unlikely(!cs->connected)) {
2029 spin_unlock_irqrestore(&cs->lock, flags);
2030 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
2031
2032 spin_lock_irqsave(&cs->cmdlock, flags);
2033 while (cs->cmdbuf != NULL)
2034 complete_cb(cs);
2035 spin_unlock_irqrestore(&cs->cmdlock, flags);
2036 return -ENODEV;
2037 }
2038 rc = start_cbsend(cs);
2039 spin_unlock_irqrestore(&cs->lock, flags);
2040 return rc < 0 ? rc : cb->len;
2041}
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052static int gigaset_write_room(struct cardstate *cs)
2053{
2054 return IF_WRITEBUF;
2055}
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065static int gigaset_chars_in_buffer(struct cardstate *cs)
2066{
2067 return cs->cmdbytes;
2068}
2069
2070
2071
2072
2073
2074
2075
2076
2077static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2078{
2079 return -EINVAL;
2080}
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090static void gigaset_freebcshw(struct bc_state *bcs)
2091{
2092 struct bas_bc_state *ubc = bcs->hw.bas;
2093 int i;
2094
2095 if (!ubc)
2096 return;
2097
2098
2099 ubc->running = 0;
2100 gig_dbg(DEBUG_INIT, "%s: killing isoc URBs", __func__);
2101 for (i = 0; i < BAS_OUTURBS; ++i) {
2102 usb_kill_urb(ubc->isoouturbs[i].urb);
2103 usb_free_urb(ubc->isoouturbs[i].urb);
2104 }
2105 for (i = 0; i < BAS_INURBS; ++i) {
2106 usb_kill_urb(ubc->isoinurbs[i]);
2107 usb_free_urb(ubc->isoinurbs[i]);
2108 }
2109 tasklet_kill(&ubc->sent_tasklet);
2110 tasklet_kill(&ubc->rcvd_tasklet);
2111 kfree(ubc->isooutbuf);
2112 kfree(ubc);
2113 bcs->hw.bas = NULL;
2114}
2115
2116
2117
2118
2119
2120
2121
2122static int gigaset_initbcshw(struct bc_state *bcs)
2123{
2124 int i;
2125 struct bas_bc_state *ubc;
2126
2127 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2128 if (!ubc) {
2129 pr_err("out of memory\n");
2130 return -ENOMEM;
2131 }
2132
2133 ubc->running = 0;
2134 atomic_set(&ubc->corrbytes, 0);
2135 spin_lock_init(&ubc->isooutlock);
2136 for (i = 0; i < BAS_OUTURBS; ++i) {
2137 ubc->isoouturbs[i].urb = NULL;
2138 ubc->isoouturbs[i].bcs = bcs;
2139 }
2140 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2141 ubc->numsub = 0;
2142 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2143 if (!ubc->isooutbuf) {
2144 pr_err("out of memory\n");
2145 kfree(ubc);
2146 bcs->hw.bas = NULL;
2147 return -ENOMEM;
2148 }
2149 tasklet_init(&ubc->sent_tasklet,
2150 write_iso_tasklet, (unsigned long) bcs);
2151
2152 spin_lock_init(&ubc->isoinlock);
2153 for (i = 0; i < BAS_INURBS; ++i)
2154 ubc->isoinurbs[i] = NULL;
2155 ubc->isoindone = NULL;
2156 ubc->loststatus = -EINPROGRESS;
2157 ubc->isoinlost = 0;
2158 ubc->seqlen = 0;
2159 ubc->inbyte = 0;
2160 ubc->inbits = 0;
2161 ubc->goodbytes = 0;
2162 ubc->alignerrs = 0;
2163 ubc->fcserrs = 0;
2164 ubc->frameerrs = 0;
2165 ubc->giants = 0;
2166 ubc->runts = 0;
2167 ubc->aborts = 0;
2168 ubc->shared0s = 0;
2169 ubc->stolen0s = 0;
2170 tasklet_init(&ubc->rcvd_tasklet,
2171 read_iso_tasklet, (unsigned long) bcs);
2172 return 0;
2173}
2174
2175static void gigaset_reinitbcshw(struct bc_state *bcs)
2176{
2177 struct bas_bc_state *ubc = bcs->hw.bas;
2178
2179 bcs->hw.bas->running = 0;
2180 atomic_set(&bcs->hw.bas->corrbytes, 0);
2181 bcs->hw.bas->numsub = 0;
2182 spin_lock_init(&ubc->isooutlock);
2183 spin_lock_init(&ubc->isoinlock);
2184 ubc->loststatus = -EINPROGRESS;
2185}
2186
2187static void gigaset_freecshw(struct cardstate *cs)
2188{
2189
2190 kfree(cs->hw.bas->int_in_buf);
2191 kfree(cs->hw.bas);
2192 cs->hw.bas = NULL;
2193}
2194
2195
2196
2197
2198
2199
2200
2201static int gigaset_initcshw(struct cardstate *cs)
2202{
2203 struct bas_cardstate *ucs;
2204
2205 cs->hw.bas = ucs = kzalloc(sizeof(*ucs), GFP_KERNEL);
2206 if (!ucs) {
2207 pr_err("out of memory\n");
2208 return -ENOMEM;
2209 }
2210 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2211 if (!ucs->int_in_buf) {
2212 kfree(ucs);
2213 pr_err("out of memory\n");
2214 return -ENOMEM;
2215 }
2216
2217 spin_lock_init(&ucs->lock);
2218 ucs->cs = cs;
2219 timer_setup(&ucs->timer_ctrl, req_timeout, 0);
2220 timer_setup(&ucs->timer_atrdy, atrdy_timeout, 0);
2221 timer_setup(&ucs->timer_cmd_in, cmd_in_timeout, 0);
2222 timer_setup(&ucs->timer_int_in, int_in_resubmit, 0);
2223 init_waitqueue_head(&ucs->waitqueue);
2224 INIT_WORK(&ucs->int_in_wq, int_in_work);
2225
2226 return 0;
2227}
2228
2229
2230
2231
2232
2233
2234
2235static void freeurbs(struct cardstate *cs)
2236{
2237 struct bas_cardstate *ucs = cs->hw.bas;
2238 struct bas_bc_state *ubc;
2239 int i, j;
2240
2241 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
2242 for (j = 0; j < BAS_CHANNELS; ++j) {
2243 ubc = cs->bcs[j].hw.bas;
2244 for (i = 0; i < BAS_OUTURBS; ++i) {
2245 usb_kill_urb(ubc->isoouturbs[i].urb);
2246 usb_free_urb(ubc->isoouturbs[i].urb);
2247 ubc->isoouturbs[i].urb = NULL;
2248 }
2249 for (i = 0; i < BAS_INURBS; ++i) {
2250 usb_kill_urb(ubc->isoinurbs[i]);
2251 usb_free_urb(ubc->isoinurbs[i]);
2252 ubc->isoinurbs[i] = NULL;
2253 }
2254 }
2255 usb_kill_urb(ucs->urb_int_in);
2256 usb_free_urb(ucs->urb_int_in);
2257 ucs->urb_int_in = NULL;
2258 usb_kill_urb(ucs->urb_cmd_out);
2259 usb_free_urb(ucs->urb_cmd_out);
2260 ucs->urb_cmd_out = NULL;
2261 usb_kill_urb(ucs->urb_cmd_in);
2262 usb_free_urb(ucs->urb_cmd_in);
2263 ucs->urb_cmd_in = NULL;
2264 usb_kill_urb(ucs->urb_ctrl);
2265 usb_free_urb(ucs->urb_ctrl);
2266 ucs->urb_ctrl = NULL;
2267}
2268
2269
2270
2271
2272
2273static int gigaset_probe(struct usb_interface *interface,
2274 const struct usb_device_id *id)
2275{
2276 struct usb_host_interface *hostif;
2277 struct usb_device *udev = interface_to_usbdev(interface);
2278 struct cardstate *cs = NULL;
2279 struct bas_cardstate *ucs = NULL;
2280 struct bas_bc_state *ubc;
2281 struct usb_endpoint_descriptor *endpoint;
2282 int i, j;
2283 int rc;
2284
2285 gig_dbg(DEBUG_INIT,
2286 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2287 __func__, le16_to_cpu(udev->descriptor.idVendor),
2288 le16_to_cpu(udev->descriptor.idProduct));
2289
2290
2291 hostif = interface->cur_altsetting;
2292 if (hostif->desc.bAlternateSetting != 3) {
2293 gig_dbg(DEBUG_INIT,
2294 "%s: wrong alternate setting %d - trying to switch",
2295 __func__, hostif->desc.bAlternateSetting);
2296 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2297 < 0) {
2298 dev_warn(&udev->dev, "usb_set_interface failed, "
2299 "device %d interface %d altsetting %d\n",
2300 udev->devnum, hostif->desc.bInterfaceNumber,
2301 hostif->desc.bAlternateSetting);
2302 return -ENODEV;
2303 }
2304 hostif = interface->cur_altsetting;
2305 }
2306
2307
2308
2309 if (hostif->desc.bInterfaceClass != 255) {
2310 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2311 __func__, hostif->desc.bInterfaceClass);
2312 return -ENODEV;
2313 }
2314
2315 if (hostif->desc.bNumEndpoints < 1)
2316 return -ENODEV;
2317
2318 dev_info(&udev->dev,
2319 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2320 __func__, le16_to_cpu(udev->descriptor.idVendor),
2321 le16_to_cpu(udev->descriptor.idProduct));
2322
2323
2324 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2325 GIGASET_MODULENAME);
2326 if (!cs)
2327 return -ENODEV;
2328 ucs = cs->hw.bas;
2329
2330
2331 usb_get_dev(udev);
2332 ucs->udev = udev;
2333 ucs->interface = interface;
2334 cs->dev = &interface->dev;
2335
2336
2337
2338
2339
2340
2341 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2342 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2343 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2344 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
2345 goto allocerr;
2346
2347 for (j = 0; j < BAS_CHANNELS; ++j) {
2348 ubc = cs->bcs[j].hw.bas;
2349 for (i = 0; i < BAS_OUTURBS; ++i)
2350 if (!(ubc->isoouturbs[i].urb =
2351 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2352 goto allocerr;
2353 for (i = 0; i < BAS_INURBS; ++i)
2354 if (!(ubc->isoinurbs[i] =
2355 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2356 goto allocerr;
2357 }
2358
2359 ucs->rcvbuf = NULL;
2360 ucs->rcvbuf_size = 0;
2361
2362
2363 endpoint = &hostif->endpoint[0].desc;
2364 usb_fill_int_urb(ucs->urb_int_in, udev,
2365 usb_rcvintpipe(udev,
2366 usb_endpoint_num(endpoint)),
2367 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
2368 endpoint->bInterval);
2369 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2370 if (rc != 0) {
2371 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2372 get_usb_rcmsg(rc));
2373 goto error;
2374 }
2375 ucs->retry_int_in = 0;
2376
2377
2378 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2379 if (rc != 0)
2380 goto error;
2381
2382
2383 if (startmode == SM_LOCKED)
2384 cs->mstate = MS_LOCKED;
2385
2386
2387 usb_set_intfdata(interface, cs);
2388
2389 rc = gigaset_start(cs);
2390 if (rc < 0)
2391 goto error;
2392
2393 return 0;
2394
2395allocerr:
2396 dev_err(cs->dev, "could not allocate URBs\n");
2397 rc = -ENOMEM;
2398error:
2399 freeurbs(cs);
2400 usb_set_intfdata(interface, NULL);
2401 usb_put_dev(udev);
2402 gigaset_freecs(cs);
2403 return rc;
2404}
2405
2406
2407
2408
2409static void gigaset_disconnect(struct usb_interface *interface)
2410{
2411 struct cardstate *cs;
2412 struct bas_cardstate *ucs;
2413 int j;
2414
2415 cs = usb_get_intfdata(interface);
2416
2417 ucs = cs->hw.bas;
2418
2419 dev_info(cs->dev, "disconnecting Gigaset base\n");
2420
2421
2422 ucs->basstate = 0;
2423
2424
2425 for (j = 0; j < BAS_CHANNELS; ++j)
2426 gigaset_bchannel_down(cs->bcs + j);
2427
2428
2429 gigaset_stop(cs);
2430
2431
2432 del_timer_sync(&ucs->timer_ctrl);
2433 del_timer_sync(&ucs->timer_atrdy);
2434 del_timer_sync(&ucs->timer_cmd_in);
2435 del_timer_sync(&ucs->timer_int_in);
2436 cancel_work_sync(&ucs->int_in_wq);
2437 freeurbs(cs);
2438 usb_set_intfdata(interface, NULL);
2439 kfree(ucs->rcvbuf);
2440 ucs->rcvbuf = NULL;
2441 ucs->rcvbuf_size = 0;
2442 usb_put_dev(ucs->udev);
2443 ucs->interface = NULL;
2444 ucs->udev = NULL;
2445 cs->dev = NULL;
2446 gigaset_freecs(cs);
2447}
2448
2449
2450
2451
2452
2453
2454static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2455{
2456 struct cardstate *cs = usb_get_intfdata(intf);
2457 struct bas_cardstate *ucs = cs->hw.bas;
2458 int rc;
2459
2460
2461 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2462 gig_dbg(DEBUG_SUSPEND, "already suspended");
2463 return 0;
2464 }
2465
2466
2467 rc = wait_event_timeout(ucs->waitqueue,
2468 !(ucs->basstate &
2469 (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)),
2470 BAS_TIMEOUT * HZ / 10);
2471 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2472
2473
2474 if (ucs->basstate & (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)) {
2475 dev_warn(cs->dev, "cannot suspend:\n");
2476 if (ucs->basstate & BS_B1OPEN)
2477 dev_warn(cs->dev, " B channel 1 open\n");
2478 if (ucs->basstate & BS_B2OPEN)
2479 dev_warn(cs->dev, " B channel 2 open\n");
2480 if (ucs->basstate & BS_ATRDPEND)
2481 dev_warn(cs->dev, " receiving AT reply\n");
2482 if (ucs->basstate & BS_ATWRPEND)
2483 dev_warn(cs->dev, " sending AT command\n");
2484 update_basstate(ucs, 0, BS_SUSPEND);
2485 return -EBUSY;
2486 }
2487
2488
2489 if (ucs->basstate & BS_ATOPEN) {
2490 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2491 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2492 if (rc) {
2493 update_basstate(ucs, 0, BS_SUSPEND);
2494 return rc;
2495 }
2496 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2497 BAS_TIMEOUT * HZ / 10);
2498
2499 }
2500
2501
2502 usb_kill_urb(ucs->urb_ctrl);
2503 usb_kill_urb(ucs->urb_int_in);
2504 del_timer_sync(&ucs->timer_ctrl);
2505 del_timer_sync(&ucs->timer_atrdy);
2506 del_timer_sync(&ucs->timer_cmd_in);
2507 del_timer_sync(&ucs->timer_int_in);
2508
2509
2510
2511
2512 if (message.event != PM_EVENT_ON)
2513 cancel_work_sync(&ucs->int_in_wq);
2514
2515 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2516 return 0;
2517}
2518
2519
2520
2521
2522static int gigaset_resume(struct usb_interface *intf)
2523{
2524 struct cardstate *cs = usb_get_intfdata(intf);
2525 struct bas_cardstate *ucs = cs->hw.bas;
2526 int rc;
2527
2528
2529 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2530 if (rc) {
2531 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2532 get_usb_rcmsg(rc));
2533 return rc;
2534 }
2535 ucs->retry_int_in = 0;
2536
2537
2538 update_basstate(ucs, 0, BS_SUSPEND);
2539
2540 gig_dbg(DEBUG_SUSPEND, "resume complete");
2541 return 0;
2542}
2543
2544
2545
2546
2547static int gigaset_pre_reset(struct usb_interface *intf)
2548{
2549
2550 return gigaset_suspend(intf, PMSG_ON);
2551}
2552
2553
2554
2555
2556static int gigaset_post_reset(struct usb_interface *intf)
2557{
2558
2559
2560
2561 return gigaset_resume(intf);
2562}
2563
2564
2565static const struct gigaset_ops gigops = {
2566 .write_cmd = gigaset_write_cmd,
2567 .write_room = gigaset_write_room,
2568 .chars_in_buffer = gigaset_chars_in_buffer,
2569 .brkchars = gigaset_brkchars,
2570 .init_bchannel = gigaset_init_bchannel,
2571 .close_bchannel = gigaset_close_bchannel,
2572 .initbcshw = gigaset_initbcshw,
2573 .freebcshw = gigaset_freebcshw,
2574 .reinitbcshw = gigaset_reinitbcshw,
2575 .initcshw = gigaset_initcshw,
2576 .freecshw = gigaset_freecshw,
2577 .set_modem_ctrl = gigaset_set_modem_ctrl,
2578 .baud_rate = gigaset_baud_rate,
2579 .set_line_ctrl = gigaset_set_line_ctrl,
2580 .send_skb = gigaset_isoc_send_skb,
2581 .handle_input = gigaset_isoc_input,
2582};
2583
2584
2585
2586
2587static int __init bas_gigaset_init(void)
2588{
2589 int result;
2590
2591
2592 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2593 GIGASET_MODULENAME, GIGASET_DEVNAME,
2594 &gigops, THIS_MODULE);
2595 if (driver == NULL)
2596 goto error;
2597
2598
2599 result = usb_register(&gigaset_usb_driver);
2600 if (result < 0) {
2601 pr_err("error %d registering USB driver\n", -result);
2602 goto error;
2603 }
2604
2605 pr_info(DRIVER_DESC "\n");
2606 return 0;
2607
2608error:
2609 if (driver)
2610 gigaset_freedriver(driver);
2611 driver = NULL;
2612 return -1;
2613}
2614
2615
2616
2617
2618static void __exit bas_gigaset_exit(void)
2619{
2620 struct bas_cardstate *ucs;
2621 int i;
2622
2623 gigaset_blockdriver(driver);
2624
2625
2626
2627
2628 for (i = 0; i < driver->minors; i++) {
2629 if (gigaset_shutdown(driver->cs + i) < 0)
2630 continue;
2631
2632
2633
2634 ucs = driver->cs[i].hw.bas;
2635 if (ucs->basstate & BS_B1OPEN) {
2636 gig_dbg(DEBUG_INIT, "closing B1 channel");
2637 usb_control_msg(ucs->udev,
2638 usb_sndctrlpipe(ucs->udev, 0),
2639 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2640 0, 0, NULL, 0, BAS_TIMEOUT);
2641 }
2642 if (ucs->basstate & BS_B2OPEN) {
2643 gig_dbg(DEBUG_INIT, "closing B2 channel");
2644 usb_control_msg(ucs->udev,
2645 usb_sndctrlpipe(ucs->udev, 0),
2646 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2647 0, 0, NULL, 0, BAS_TIMEOUT);
2648 }
2649 if (ucs->basstate & BS_ATOPEN) {
2650 gig_dbg(DEBUG_INIT, "closing AT channel");
2651 usb_control_msg(ucs->udev,
2652 usb_sndctrlpipe(ucs->udev, 0),
2653 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2654 0, 0, NULL, 0, BAS_TIMEOUT);
2655 }
2656 ucs->basstate = 0;
2657 }
2658
2659
2660 usb_deregister(&gigaset_usb_driver);
2661
2662
2663
2664 gigaset_freedriver(driver);
2665 driver = NULL;
2666}
2667
2668
2669module_init(bas_gigaset_init);
2670module_exit(bas_gigaset_exit);
2671
2672MODULE_AUTHOR(DRIVER_AUTHOR);
2673MODULE_DESCRIPTION(DRIVER_DESC);
2674MODULE_LICENSE("GPL");
2675