2 * Driver for ST5481 USB ISDN modem
5 * Copyright 2001 by Frode Isaksen <fisaksen@bewan.com>
6 * 2001 by Kai Germaschewski <kai.germaschewski@gmx.de>
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
13 #include <linux/init.h>
14 #include <linux/usb.h>
15 #include <linux/slab.h>
18 static int st5481_isoc_flatten(struct urb *urb);
20 /* ======================================================================
25 * Send the next endpoint 0 request stored in the FIFO.
26 * Called either by the completion or by usb_ctrl_msg.
28 static void usb_next_ctrl_msg(struct urb *urb,
29 struct st5481_adapter *adapter)
31 struct st5481_ctrl *ctrl = &adapter->ctrl;
34 if (test_and_set_bit(0, &ctrl->busy)) {
38 if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) {
39 test_and_clear_bit(0,&ctrl->busy);
43 (unsigned char *)&ctrl->msg_fifo.data[r_index];
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);
51 urb->dev = adapter->usb_dev;
53 SUBMIT_URB(urb, GFP_ATOMIC);
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.
60 static void usb_ctrl_msg(struct st5481_adapter *adapter,
61 u8 request, u8 requesttype, u16 value, u16 index,
62 ctrl_complete_t complete, void *context)
64 struct st5481_ctrl *ctrl = &adapter->ctrl;
66 struct ctrl_msg *ctrl_msg;
68 if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
69 WARN("control msg FIFO full");
72 ctrl_msg = &ctrl->msg_fifo.data[w_index];
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;
82 usb_next_ctrl_msg(ctrl->urb, adapter);
86 * Asynchronous endpoint 0 device request.
88 void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
89 u8 request, u16 value,
90 ctrl_complete_t complete, void *context)
92 usb_ctrl_msg(adapter, request,
93 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
94 value, 0, complete, context);
98 * Asynchronous pipe reset (async version of usb_clear_halt).
100 void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
102 ctrl_complete_t complete, void *context)
104 DBG(1,"pipe=%02x",pipe);
106 usb_ctrl_msg(adapter,
107 USB_REQ_CLEAR_FEATURE, USB_DIR_OUT | USB_RECIP_ENDPOINT,
108 0, pipe, complete, context);
113 Physical level functions
116 void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command)
118 DBG(8,"command=%s", ST5481_CMD_string(command));
120 st5481_usb_device_ctrl_msg(adapter, TXCI, command, NULL, NULL);
124 * The request on endpoint 0 has completed.
125 * Call the user provided completion routine and try
126 * to send the next request.
128 static void usb_ctrl_complete(struct urb *urb, struct pt_regs *regs)
130 struct st5481_adapter *adapter = urb->context;
131 struct st5481_ctrl *ctrl = &adapter->ctrl;
132 struct ctrl_msg *ctrl_msg;
134 if (unlikely(urb->status < 0)) {
135 if (urb->status != -ENOENT && urb->status != -ESHUTDOWN) {
136 WARN("urb status %d",urb->status);
143 ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
145 if (ctrl_msg->dr.bRequest == USB_REQ_CLEAR_FEATURE) {
146 /* Special case handling for pipe reset */
147 le16_to_cpus(&ctrl_msg->dr.wIndex);
149 /* toggle is reset on clear */
150 usb_settoggle(adapter->usb_dev,
151 ctrl_msg->dr.wIndex & ~USB_DIR_IN,
152 (ctrl_msg->dr.wIndex & USB_DIR_IN) == 0,
158 if (ctrl_msg->complete)
159 ctrl_msg->complete(ctrl_msg->context);
161 clear_bit(0, &ctrl->busy);
163 // Try to send next control message
164 usb_next_ctrl_msg(urb, adapter);
168 /* ======================================================================
173 * The interrupt endpoint will be called when any
174 * of the 6 registers changes state (depending on masks).
175 * Decode the register values and schedule a private event.
176 * Called at interrupt.
178 static void usb_int_complete(struct urb *urb, struct pt_regs *regs)
180 u8 *data = urb->transfer_buffer;
182 struct st5481_adapter *adapter = urb->context;
186 switch (urb->status) {
193 /* this urb is terminated, clean up */
194 DBG(1, "urb shutting down with status: %d", urb->status);
197 WARN("nonzero urb status received: %d", urb->status);
202 DBG_PACKET(1, data, INT_PKT_SIZE);
204 if (urb->actual_length == 0) {
208 irqbyte = data[MPINT];
209 if (irqbyte & DEN_INT)
210 FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
212 if (irqbyte & DCOLL_INT)
213 FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
215 irqbyte = data[FFINT_D];
216 if (irqbyte & OUT_UNDERRUN)
217 FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
219 if (irqbyte & OUT_DOWN)
220 ;// printk("OUT_DOWN\n");
222 irqbyte = data[MPINT];
223 if (irqbyte & RXCI_INT)
224 FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
226 for (j = 0; j < 2; j++)
227 adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
229 urb->actual_length = 0;
232 status = usb_submit_urb (urb, GFP_ATOMIC);
234 WARN("usb_submit_urb failed with result %d", status);
237 /* ======================================================================
241 int st5481_setup_usb(struct st5481_adapter *adapter)
243 struct usb_device *dev = adapter->usb_dev;
244 struct st5481_ctrl *ctrl = &adapter->ctrl;
245 struct st5481_intr *intr = &adapter->intr;
246 struct usb_interface *intf;
247 struct usb_host_interface *altsetting = NULL;
248 struct usb_host_endpoint *endpoint;
255 if ((status = usb_reset_configuration (dev)) < 0) {
256 WARN("reset_configuration failed,status=%d",status);
260 intf = usb_ifnum_to_if(dev, 0);
262 altsetting = usb_altnum_to_altsetting(intf, 3);
266 // Check if the config is sane
267 if ( altsetting->desc.bNumEndpoints != 7 ) {
268 WARN("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
272 // The descriptor is wrong for some early samples of the ST5481 chip
273 altsetting->endpoint[3].desc.wMaxPacketSize = __constant_cpu_to_le16(32);
274 altsetting->endpoint[4].desc.wMaxPacketSize = __constant_cpu_to_le16(32);
276 // Use alternative setting 3 on interface 0 to have 2B+D
277 if ((status = usb_set_interface (dev, 0, 3)) < 0) {
278 WARN("usb_set_interface failed,status=%d",status);
282 // Allocate URB for control endpoint
283 urb = usb_alloc_urb(0, GFP_KERNEL);
289 // Fill the control URB
290 usb_fill_control_urb (urb, dev,
291 usb_sndctrlpipe(dev, 0),
292 NULL, NULL, 0, usb_ctrl_complete, adapter);
295 fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
297 // Allocate URBs and buffers for interrupt endpoint
298 urb = usb_alloc_urb(0, GFP_KERNEL);
304 buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
309 endpoint = &altsetting->endpoint[EP_INT-1];
311 // Fill the interrupt URB
312 usb_fill_int_urb(urb, dev,
313 usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress),
315 usb_int_complete, adapter,
316 endpoint->desc.bInterval);
322 * Release buffers and URBs for the interrupt and control
325 void st5481_release_usb(struct st5481_adapter *adapter)
327 struct st5481_intr *intr = &adapter->intr;
328 struct st5481_ctrl *ctrl = &adapter->ctrl;
332 // Stop and free Control and Interrupt URBs
333 usb_unlink_urb(ctrl->urb);
334 if (ctrl->urb->transfer_buffer)
335 kfree(ctrl->urb->transfer_buffer);
336 usb_free_urb(ctrl->urb);
338 usb_unlink_urb(intr->urb);
339 if (intr->urb->transfer_buffer)
340 kfree(intr->urb->transfer_buffer);
341 usb_free_urb(intr->urb);
345 * Initialize the adapter.
347 void st5481_start(struct st5481_adapter *adapter)
349 static const u8 init_cmd_table[]={
363 MPMSK,RXCI_INT+DEN_INT+DCOLL_INT,
366 struct st5481_intr *intr = &adapter->intr;
372 adapter->leds = RED_LED;
374 // Start receiving on the interrupt endpoint
375 SUBMIT_URB(intr->urb, GFP_KERNEL);
377 while ((request = init_cmd_table[i++])) {
378 value = init_cmd_table[i++];
379 st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
381 st5481_ph_command(adapter, ST5481_CMD_PUP);
385 * Reset the adapter to default values.
387 void st5481_stop(struct st5481_adapter *adapter)
391 st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
394 /* ======================================================================
395 * isochronous USB helpers
399 fill_isoc_urb(struct urb *urb, struct usb_device *dev,
400 unsigned int pipe, void *buf, int num_packets,
401 int packet_size, usb_complete_t complete,
406 spin_lock_init(&urb->lock);
409 urb->transfer_buffer=buf;
410 urb->number_of_packets = num_packets;
411 urb->transfer_buffer_length=num_packets*packet_size;
412 urb->actual_length = 0;
413 urb->complete=complete;
414 urb->context=context;
415 urb->transfer_flags=URB_ISO_ASAP;
416 for (k = 0; k < num_packets; k++) {
417 urb->iso_frame_desc[k].offset = packet_size * k;
418 urb->iso_frame_desc[k].length = packet_size;
419 urb->iso_frame_desc[k].actual_length = 0;
424 st5481_setup_isocpipes(struct urb* urb[2], struct usb_device *dev,
425 unsigned int pipe, int num_packets,
426 int packet_size, int buf_size,
427 usb_complete_t complete, void *context)
432 for (j = 0; j < 2; j++) {
434 urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL);
438 // Allocate memory for 2000bytes/sec (16Kb/s)
439 buf = kmalloc(buf_size, GFP_KERNEL);
443 // Fill the isochronous URB
444 fill_isoc_urb(urb[j], dev, pipe, buf,
445 num_packets, packet_size, complete,
451 for (j = 0; j < 2; j++) {
453 if (urb[j]->transfer_buffer)
454 kfree(urb[j]->transfer_buffer);
455 usb_free_urb(urb[j]);
461 void st5481_release_isocpipes(struct urb* urb[2])
465 for (j = 0; j < 2; j++) {
466 usb_unlink_urb(urb[j]);
467 if (urb[j]->transfer_buffer)
468 kfree(urb[j]->transfer_buffer);
469 usb_free_urb(urb[j]);
474 * Decode frames received on the B/D channel.
475 * Note that this function will be called continously
476 * with 64Kbit/s / 16Kbit/s of data and hence it will be
477 * called 50 times per second with 20 ISOC descriptors.
478 * Called at interrupt.
480 static void usb_in_complete(struct urb *urb, struct pt_regs *regs)
482 struct st5481_in *in = urb->context;
485 int len, count, status;
487 if (unlikely(urb->status < 0)) {
488 if (urb->status != -ENOENT && urb->status != -ESHUTDOWN) {
489 WARN("urb status %d",urb->status);
496 DBG_ISO_PACKET(0x80,urb);
498 len = st5481_isoc_flatten(urb);
499 ptr = urb->transfer_buffer;
501 if (in->mode == L1_MODE_TRANS) {
502 memcpy(in->rcvbuf, ptr, len);
506 status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
507 in->rcvbuf, in->bufsize);
513 // Good frame received
514 DBG(4,"count=%d",status);
515 DBG_PACKET(0x400, in->rcvbuf, status);
516 if (!(skb = dev_alloc_skb(status))) {
517 WARN("receive out of memory\n");
520 memcpy(skb_put(skb, status), in->rcvbuf, status);
521 in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb);
522 } else if (status == -HDLC_CRC_ERROR) {
524 } else if (status == -HDLC_FRAMING_ERROR) {
525 INFO("framing error");
526 } else if (status == -HDLC_LENGTH_ERROR) {
527 INFO("length error");
531 // Prepare URB for next transfer
532 urb->dev = in->adapter->usb_dev;
533 urb->actual_length = 0;
535 SUBMIT_URB(urb, GFP_ATOMIC);
538 int st5481_setup_in(struct st5481_in *in)
540 struct usb_device *dev = in->adapter->usb_dev;
545 in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
550 retval = st5481_setup_isocpipes(in->urb, dev,
551 usb_rcvisocpipe(dev, in->ep),
552 in->num_packets, in->packet_size,
553 in->num_packets * in->packet_size,
554 usb_in_complete, in);
565 void st5481_release_in(struct st5481_in *in)
569 st5481_release_isocpipes(in->urb);
573 * Make the transfer_buffer contiguous by
574 * copying from the iso descriptors if necessary.
576 static int st5481_isoc_flatten(struct urb *urb)
578 struct usb_iso_packet_descriptor *pipd,*pend;
579 unsigned char *src,*dst;
582 if (urb->status < 0) {
585 for (pipd = &urb->iso_frame_desc[0],
586 pend = &urb->iso_frame_desc[urb->number_of_packets],
587 dst = urb->transfer_buffer;
591 if (pipd->status < 0) {
592 return (pipd->status);
595 len = pipd->actual_length;
596 pipd->actual_length = 0;
597 src = urb->transfer_buffer+pipd->offset;
600 // Need to copy since isoc buffers not full
605 // No need to copy, just update destination buffer
609 // Return size of flattened buffer
610 return (dst - (unsigned char *)urb->transfer_buffer);
613 static void st5481_start_rcv(void *context)
615 struct st5481_in *in = context;
616 struct st5481_adapter *adapter = in->adapter;
620 in->urb[0]->dev = adapter->usb_dev;
621 SUBMIT_URB(in->urb[0], GFP_KERNEL);
623 in->urb[1]->dev = adapter->usb_dev;
624 SUBMIT_URB(in->urb[1], GFP_KERNEL);
627 void st5481_in_mode(struct st5481_in *in, int mode)
629 if (in->mode == mode)
634 usb_unlink_urb(in->urb[0]);
635 usb_unlink_urb(in->urb[1]);
637 if (in->mode != L1_MODE_NULL) {
638 if (in->mode != L1_MODE_TRANS)
639 isdnhdlc_rcv_init(&in->hdlc_state,
640 in->mode == L1_MODE_HDLC_56K);
642 st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
643 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
646 st5481_start_rcv(in);
648 st5481_usb_device_ctrl_msg(in->adapter, in->counter,