blob: ffd5b2d45552641c713ec8fde152fb9f946e6ab1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for ST5481 USB ISDN modem
3 *
4 * Author Frode Isaksen
5 * Copyright 2001 by Frode Isaksen <fisaksen@bewan.com>
6 * 2001 by Kai Germaschewski <kai.germaschewski@gmx.de>
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 */
12
13#include <linux/init.h>
14#include <linux/usb.h>
15#include <linux/slab.h>
16#include "st5481.h"
17
Adrian Bunk672c3fd2005-06-25 14:59:18 -070018static int st5481_isoc_flatten(struct urb *urb);
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* ======================================================================
21 * control pipe
22 */
23
24/*
25 * Send the next endpoint 0 request stored in the FIFO.
26 * Called either by the completion or by usb_ctrl_msg.
27 */
28static void usb_next_ctrl_msg(struct urb *urb,
29 struct st5481_adapter *adapter)
30{
31 struct st5481_ctrl *ctrl = &adapter->ctrl;
32 int r_index;
33
34 if (test_and_set_bit(0, &ctrl->busy)) {
35 return;
36 }
37
38 if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) {
39 test_and_clear_bit(0,&ctrl->busy);
40 return;
41 }
42 urb->setup_packet =
43 (unsigned char *)&ctrl->msg_fifo.data[r_index];
44
45 DBG(1,"request=0x%02x,value=0x%04x,index=%x",
46 ((struct ctrl_msg *)urb->setup_packet)->dr.bRequest,
47 ((struct ctrl_msg *)urb->setup_packet)->dr.wValue,
48 ((struct ctrl_msg *)urb->setup_packet)->dr.wIndex);
49
50 // Prepare the URB
51 urb->dev = adapter->usb_dev;
52
53 SUBMIT_URB(urb, GFP_ATOMIC);
54}
55
56/*
57 * Asynchronous endpoint 0 request (async version of usb_control_msg).
58 * The request will be queued up in a FIFO if the endpoint is busy.
59 */
Adrian Bunk672c3fd2005-06-25 14:59:18 -070060static void usb_ctrl_msg(struct st5481_adapter *adapter,
61 u8 request, u8 requesttype, u16 value, u16 index,
62 ctrl_complete_t complete, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct st5481_ctrl *ctrl = &adapter->ctrl;
65 int w_index;
66 struct ctrl_msg *ctrl_msg;
67
68 if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
69 WARN("control msg FIFO full");
70 return;
71 }
72 ctrl_msg = &ctrl->msg_fifo.data[w_index];
73
74 ctrl_msg->dr.bRequestType = requesttype;
75 ctrl_msg->dr.bRequest = request;
76 ctrl_msg->dr.wValue = cpu_to_le16p(&value);
77 ctrl_msg->dr.wIndex = cpu_to_le16p(&index);
78 ctrl_msg->dr.wLength = 0;
79 ctrl_msg->complete = complete;
80 ctrl_msg->context = context;
81
82 usb_next_ctrl_msg(ctrl->urb, adapter);
83}
84
85/*
86 * Asynchronous endpoint 0 device request.
87 */
88void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
89 u8 request, u16 value,
90 ctrl_complete_t complete, void *context)
91{
92 usb_ctrl_msg(adapter, request,
93 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
94 value, 0, complete, context);
95}
96
97/*
98 * Asynchronous pipe reset (async version of usb_clear_halt).
99 */
100void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
101 u_char pipe,
102 ctrl_complete_t complete, void *context)
103{
104 DBG(1,"pipe=%02x",pipe);
105
106 usb_ctrl_msg(adapter,
107 USB_REQ_CLEAR_FEATURE, USB_DIR_OUT | USB_RECIP_ENDPOINT,
108 0, pipe, complete, context);
109}
110
111
112/*
113 Physical level functions
114*/
115
116void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command)
117{
118 DBG(8,"command=%s", ST5481_CMD_string(command));
119
120 st5481_usb_device_ctrl_msg(adapter, TXCI, command, NULL, NULL);
121}
122
123/*
124 * The request on endpoint 0 has completed.
125 * Call the user provided completion routine and try
126 * to send the next request.
127 */
128static void usb_ctrl_complete(struct urb *urb, struct pt_regs *regs)
129{
130 struct st5481_adapter *adapter = urb->context;
131 struct st5481_ctrl *ctrl = &adapter->ctrl;
132 struct ctrl_msg *ctrl_msg;
133
134 if (unlikely(urb->status < 0)) {
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200135 switch (urb->status) {
136 case -ENOENT:
137 case -ESHUTDOWN:
138 case -ECONNRESET:
139 DBG(1,"urb killed status %d", urb->status);
140 return; // Give up
141 default:
142 WARN("urb status %d",urb->status);
143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 }
146
147 ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
148
149 if (ctrl_msg->dr.bRequest == USB_REQ_CLEAR_FEATURE) {
150 /* Special case handling for pipe reset */
151 le16_to_cpus(&ctrl_msg->dr.wIndex);
152
153 /* toggle is reset on clear */
154 usb_settoggle(adapter->usb_dev,
155 ctrl_msg->dr.wIndex & ~USB_DIR_IN,
156 (ctrl_msg->dr.wIndex & USB_DIR_IN) == 0,
157 0);
158
159
160 }
161
162 if (ctrl_msg->complete)
163 ctrl_msg->complete(ctrl_msg->context);
164
165 clear_bit(0, &ctrl->busy);
166
167 // Try to send next control message
168 usb_next_ctrl_msg(urb, adapter);
169 return;
170}
171
172/* ======================================================================
173 * interrupt pipe
174 */
175
176/*
177 * The interrupt endpoint will be called when any
178 * of the 6 registers changes state (depending on masks).
179 * Decode the register values and schedule a private event.
180 * Called at interrupt.
181 */
182static void usb_int_complete(struct urb *urb, struct pt_regs *regs)
183{
184 u8 *data = urb->transfer_buffer;
185 u8 irqbyte;
186 struct st5481_adapter *adapter = urb->context;
187 int j;
188 int status;
189
190 switch (urb->status) {
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200191 case 0:
192 /* success */
193 break;
194 case -ECONNRESET:
195 case -ENOENT:
196 case -ESHUTDOWN:
197 /* this urb is terminated, clean up */
198 DBG(2, "urb shutting down with status: %d", urb->status);
199 return;
200 default:
201 WARN("nonzero urb status received: %d", urb->status);
202 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
205
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200206 DBG_PACKET(2, data, INT_PKT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (urb->actual_length == 0) {
209 goto exit;
210 }
211
212 irqbyte = data[MPINT];
213 if (irqbyte & DEN_INT)
214 FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
215
216 if (irqbyte & DCOLL_INT)
217 FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
218
219 irqbyte = data[FFINT_D];
220 if (irqbyte & OUT_UNDERRUN)
221 FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
222
223 if (irqbyte & OUT_DOWN)
224;// printk("OUT_DOWN\n");
225
226 irqbyte = data[MPINT];
227 if (irqbyte & RXCI_INT)
228 FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
229
230 for (j = 0; j < 2; j++)
231 adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
232
233 urb->actual_length = 0;
234
235exit:
236 status = usb_submit_urb (urb, GFP_ATOMIC);
237 if (status)
238 WARN("usb_submit_urb failed with result %d", status);
239}
240
241/* ======================================================================
242 * initialization
243 */
244
245int st5481_setup_usb(struct st5481_adapter *adapter)
246{
247 struct usb_device *dev = adapter->usb_dev;
248 struct st5481_ctrl *ctrl = &adapter->ctrl;
249 struct st5481_intr *intr = &adapter->intr;
250 struct usb_interface *intf;
251 struct usb_host_interface *altsetting = NULL;
252 struct usb_host_endpoint *endpoint;
253 int status;
254 struct urb *urb;
255 u8 *buf;
256
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200257 DBG(2,"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if ((status = usb_reset_configuration (dev)) < 0) {
260 WARN("reset_configuration failed,status=%d",status);
261 return status;
262 }
263
264 intf = usb_ifnum_to_if(dev, 0);
265 if (intf)
266 altsetting = usb_altnum_to_altsetting(intf, 3);
267 if (!altsetting)
268 return -ENXIO;
269
270 // Check if the config is sane
271 if ( altsetting->desc.bNumEndpoints != 7 ) {
272 WARN("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
273 return -EINVAL;
274 }
275
276 // The descriptor is wrong for some early samples of the ST5481 chip
277 altsetting->endpoint[3].desc.wMaxPacketSize = __constant_cpu_to_le16(32);
278 altsetting->endpoint[4].desc.wMaxPacketSize = __constant_cpu_to_le16(32);
279
280 // Use alternative setting 3 on interface 0 to have 2B+D
281 if ((status = usb_set_interface (dev, 0, 3)) < 0) {
282 WARN("usb_set_interface failed,status=%d",status);
283 return status;
284 }
285
286 // Allocate URB for control endpoint
287 urb = usb_alloc_urb(0, GFP_KERNEL);
288 if (!urb) {
289 return -ENOMEM;
290 }
291 ctrl->urb = urb;
292
293 // Fill the control URB
294 usb_fill_control_urb (urb, dev,
295 usb_sndctrlpipe(dev, 0),
296 NULL, NULL, 0, usb_ctrl_complete, adapter);
297
298
299 fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
300
301 // Allocate URBs and buffers for interrupt endpoint
302 urb = usb_alloc_urb(0, GFP_KERNEL);
303 if (!urb) {
304 return -ENOMEM;
305 }
306 intr->urb = urb;
307
308 buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
309 if (!buf) {
310 return -ENOMEM;
311 }
312
313 endpoint = &altsetting->endpoint[EP_INT-1];
314
315 // Fill the interrupt URB
316 usb_fill_int_urb(urb, dev,
317 usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress),
318 buf, INT_PKT_SIZE,
319 usb_int_complete, adapter,
320 endpoint->desc.bInterval);
321
322 return 0;
323}
324
325/*
326 * Release buffers and URBs for the interrupt and control
327 * endpoint.
328 */
329void st5481_release_usb(struct st5481_adapter *adapter)
330{
331 struct st5481_intr *intr = &adapter->intr;
332 struct st5481_ctrl *ctrl = &adapter->ctrl;
333
334 DBG(1,"");
335
336 // Stop and free Control and Interrupt URBs
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200337 usb_kill_urb(ctrl->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (ctrl->urb->transfer_buffer)
339 kfree(ctrl->urb->transfer_buffer);
340 usb_free_urb(ctrl->urb);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200341 ctrl->urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200343 usb_kill_urb(intr->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (intr->urb->transfer_buffer)
345 kfree(intr->urb->transfer_buffer);
346 usb_free_urb(intr->urb);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200347 ctrl->urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/*
351 * Initialize the adapter.
352 */
353void st5481_start(struct st5481_adapter *adapter)
354{
355 static const u8 init_cmd_table[]={
356 SET_DEFAULT,0,
357 STT,0,
358 SDA_MIN,0x0d,
359 SDA_MAX,0x29,
360 SDELAY_VALUE,0x14,
361 GPIO_DIR,0x01,
362 GPIO_OUT,RED_LED,
363// FFCTRL_OUT_D,4,
364// FFCTRH_OUT_D,12,
365 FFCTRL_OUT_B1,6,
366 FFCTRH_OUT_B1,20,
367 FFCTRL_OUT_B2,6,
368 FFCTRH_OUT_B2,20,
369 MPMSK,RXCI_INT+DEN_INT+DCOLL_INT,
370 0
371 };
372 struct st5481_intr *intr = &adapter->intr;
373 int i = 0;
374 u8 request,value;
375
376 DBG(8,"");
377
378 adapter->leds = RED_LED;
379
380 // Start receiving on the interrupt endpoint
381 SUBMIT_URB(intr->urb, GFP_KERNEL);
382
383 while ((request = init_cmd_table[i++])) {
384 value = init_cmd_table[i++];
385 st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
386 }
387 st5481_ph_command(adapter, ST5481_CMD_PUP);
388}
389
390/*
391 * Reset the adapter to default values.
392 */
393void st5481_stop(struct st5481_adapter *adapter)
394{
395 DBG(8,"");
396
397 st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
398}
399
400/* ======================================================================
401 * isochronous USB helpers
402 */
403
404static void
405fill_isoc_urb(struct urb *urb, struct usb_device *dev,
406 unsigned int pipe, void *buf, int num_packets,
407 int packet_size, usb_complete_t complete,
408 void *context)
409{
410 int k;
411
412 spin_lock_init(&urb->lock);
413 urb->dev=dev;
414 urb->pipe=pipe;
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200415 urb->interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 urb->transfer_buffer=buf;
417 urb->number_of_packets = num_packets;
418 urb->transfer_buffer_length=num_packets*packet_size;
419 urb->actual_length = 0;
420 urb->complete=complete;
421 urb->context=context;
422 urb->transfer_flags=URB_ISO_ASAP;
423 for (k = 0; k < num_packets; k++) {
424 urb->iso_frame_desc[k].offset = packet_size * k;
425 urb->iso_frame_desc[k].length = packet_size;
426 urb->iso_frame_desc[k].actual_length = 0;
427 }
428}
429
430int
431st5481_setup_isocpipes(struct urb* urb[2], struct usb_device *dev,
432 unsigned int pipe, int num_packets,
433 int packet_size, int buf_size,
434 usb_complete_t complete, void *context)
435{
436 int j, retval;
437 unsigned char *buf;
438
439 for (j = 0; j < 2; j++) {
440 retval = -ENOMEM;
441 urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL);
442 if (!urb[j])
443 goto err;
444
445 // Allocate memory for 2000bytes/sec (16Kb/s)
446 buf = kmalloc(buf_size, GFP_KERNEL);
447 if (!buf)
448 goto err;
449
450 // Fill the isochronous URB
451 fill_isoc_urb(urb[j], dev, pipe, buf,
452 num_packets, packet_size, complete,
453 context);
454 }
455 return 0;
456
457 err:
458 for (j = 0; j < 2; j++) {
459 if (urb[j]) {
460 if (urb[j]->transfer_buffer)
461 kfree(urb[j]->transfer_buffer);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200462 urb[j]->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 usb_free_urb(urb[j]);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200464 urb[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 }
467 return retval;
468}
469
470void st5481_release_isocpipes(struct urb* urb[2])
471{
472 int j;
473
474 for (j = 0; j < 2; j++) {
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200475 usb_kill_urb(urb[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (urb[j]->transfer_buffer)
477 kfree(urb[j]->transfer_buffer);
478 usb_free_urb(urb[j]);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200479 urb[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481}
482
483/*
484 * Decode frames received on the B/D channel.
485 * Note that this function will be called continously
486 * with 64Kbit/s / 16Kbit/s of data and hence it will be
487 * called 50 times per second with 20 ISOC descriptors.
488 * Called at interrupt.
489 */
490static void usb_in_complete(struct urb *urb, struct pt_regs *regs)
491{
492 struct st5481_in *in = urb->context;
493 unsigned char *ptr;
494 struct sk_buff *skb;
495 int len, count, status;
496
497 if (unlikely(urb->status < 0)) {
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200498 switch (urb->status) {
499 case -ENOENT:
500 case -ESHUTDOWN:
501 case -ECONNRESET:
502 DBG(1,"urb killed status %d", urb->status);
503 return; // Give up
504 default:
505 WARN("urb status %d",urb->status);
506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 }
509
510 DBG_ISO_PACKET(0x80,urb);
511
512 len = st5481_isoc_flatten(urb);
513 ptr = urb->transfer_buffer;
514 while (len > 0) {
515 if (in->mode == L1_MODE_TRANS) {
516 memcpy(in->rcvbuf, ptr, len);
517 status = len;
518 len = 0;
519 } else {
520 status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
521 in->rcvbuf, in->bufsize);
522 ptr += count;
523 len -= count;
524 }
525
526 if (status > 0) {
527 // Good frame received
528 DBG(4,"count=%d",status);
529 DBG_PACKET(0x400, in->rcvbuf, status);
530 if (!(skb = dev_alloc_skb(status))) {
531 WARN("receive out of memory\n");
532 break;
533 }
534 memcpy(skb_put(skb, status), in->rcvbuf, status);
535 in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb);
536 } else if (status == -HDLC_CRC_ERROR) {
537 INFO("CRC error");
538 } else if (status == -HDLC_FRAMING_ERROR) {
539 INFO("framing error");
540 } else if (status == -HDLC_LENGTH_ERROR) {
541 INFO("length error");
542 }
543 }
544
545 // Prepare URB for next transfer
546 urb->dev = in->adapter->usb_dev;
547 urb->actual_length = 0;
548
549 SUBMIT_URB(urb, GFP_ATOMIC);
550}
551
552int st5481_setup_in(struct st5481_in *in)
553{
554 struct usb_device *dev = in->adapter->usb_dev;
555 int retval;
556
557 DBG(4,"");
558
559 in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
560 retval = -ENOMEM;
561 if (!in->rcvbuf)
562 goto err;
563
564 retval = st5481_setup_isocpipes(in->urb, dev,
565 usb_rcvisocpipe(dev, in->ep),
566 in->num_packets, in->packet_size,
567 in->num_packets * in->packet_size,
568 usb_in_complete, in);
569 if (retval)
570 goto err_free;
571 return 0;
572
573 err_free:
574 kfree(in->rcvbuf);
575 err:
576 return retval;
577}
578
579void st5481_release_in(struct st5481_in *in)
580{
581 DBG(2,"");
582
583 st5481_release_isocpipes(in->urb);
584}
585
586/*
587 * Make the transfer_buffer contiguous by
588 * copying from the iso descriptors if necessary.
589 */
Adrian Bunk672c3fd2005-06-25 14:59:18 -0700590static int st5481_isoc_flatten(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct usb_iso_packet_descriptor *pipd,*pend;
593 unsigned char *src,*dst;
594 unsigned int len;
595
596 if (urb->status < 0) {
597 return urb->status;
598 }
599 for (pipd = &urb->iso_frame_desc[0],
600 pend = &urb->iso_frame_desc[urb->number_of_packets],
601 dst = urb->transfer_buffer;
602 pipd < pend;
603 pipd++) {
604
605 if (pipd->status < 0) {
606 return (pipd->status);
607 }
608
609 len = pipd->actual_length;
610 pipd->actual_length = 0;
611 src = urb->transfer_buffer+pipd->offset;
612
613 if (src != dst) {
614 // Need to copy since isoc buffers not full
615 while (len--) {
616 *dst++ = *src++;
617 }
618 } else {
619 // No need to copy, just update destination buffer
620 dst += len;
621 }
622 }
623 // Return size of flattened buffer
624 return (dst - (unsigned char *)urb->transfer_buffer);
625}
626
627static void st5481_start_rcv(void *context)
628{
629 struct st5481_in *in = context;
630 struct st5481_adapter *adapter = in->adapter;
631
632 DBG(4,"");
633
634 in->urb[0]->dev = adapter->usb_dev;
635 SUBMIT_URB(in->urb[0], GFP_KERNEL);
636
637 in->urb[1]->dev = adapter->usb_dev;
638 SUBMIT_URB(in->urb[1], GFP_KERNEL);
639}
640
641void st5481_in_mode(struct st5481_in *in, int mode)
642{
643 if (in->mode == mode)
644 return;
645
646 in->mode = mode;
647
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200648 in->urb[0]->transfer_flags |= URB_ASYNC_UNLINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 usb_unlink_urb(in->urb[0]);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200650 in->urb[1]->transfer_flags |= URB_ASYNC_UNLINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 usb_unlink_urb(in->urb[1]);
652
653 if (in->mode != L1_MODE_NULL) {
654 if (in->mode != L1_MODE_TRANS)
655 isdnhdlc_rcv_init(&in->hdlc_state,
656 in->mode == L1_MODE_HDLC_56K);
657
658 st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
659 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
660 in->packet_size,
661 NULL, NULL);
662 st5481_start_rcv(in);
663 } else {
664 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
665 0, NULL, NULL);
666 }
667}
668