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 /* ======================================================================
23 * Send the next endpoint 0 request stored in the FIFO.
24 * Called either by the completion or by usb_ctrl_msg.
26 static void usb_next_ctrl_msg(struct urb *urb,
27 struct st5481_adapter *adapter)
29 struct st5481_ctrl *ctrl = &adapter->ctrl;
32 if (test_and_set_bit(0, &ctrl->busy)) {
36 if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) {
37 test_and_clear_bit(0,&ctrl->busy);
41 (unsigned char *)&ctrl->msg_fifo.data[r_index];
43 DBG(1,"request=0x%02x,value=0x%04x,index=%x",
44 ((struct ctrl_msg *)urb->setup_packet)->dr.bRequest,
45 ((struct ctrl_msg *)urb->setup_packet)->dr.wValue,
46 ((struct ctrl_msg *)urb->setup_packet)->dr.wIndex);
49 urb->dev = adapter->usb_dev;
51 SUBMIT_URB(urb, GFP_ATOMIC);
55 * Asynchronous endpoint 0 request (async version of usb_control_msg).
56 * The request will be queued up in a FIFO if the endpoint is busy.
58 void usb_ctrl_msg(struct st5481_adapter *adapter,
59 u8 request, u8 requesttype, u16 value, u16 index,
60 ctrl_complete_t complete, void *context)
62 struct st5481_ctrl *ctrl = &adapter->ctrl;
64 struct ctrl_msg *ctrl_msg;
66 if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
67 WARN("control msg FIFO full");
70 ctrl_msg = &ctrl->msg_fifo.data[w_index];
72 ctrl_msg->dr.bRequestType = requesttype;
73 ctrl_msg->dr.bRequest = request;
74 ctrl_msg->dr.wValue = cpu_to_le16p(&value);
75 ctrl_msg->dr.wIndex = cpu_to_le16p(&index);
76 ctrl_msg->dr.wLength = 0;
77 ctrl_msg->complete = complete;
78 ctrl_msg->context = context;
80 usb_next_ctrl_msg(ctrl->urb, adapter);
84 * Asynchronous endpoint 0 device request.
86 void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
87 u8 request, u16 value,
88 ctrl_complete_t complete, void *context)
90 usb_ctrl_msg(adapter, request,
91 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
92 value, 0, complete, context);
96 * Asynchronous pipe reset (async version of usb_clear_halt).
98 void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
100 ctrl_complete_t complete, void *context)
102 DBG(1,"pipe=%02x",pipe);
104 usb_ctrl_msg(adapter,
105 USB_REQ_CLEAR_FEATURE, USB_DIR_OUT | USB_RECIP_ENDPOINT,
106 0, pipe, complete, context);
111 Physical level functions
114 void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command)
116 DBG(8,"command=%s", ST5481_CMD_string(command));
118 st5481_usb_device_ctrl_msg(adapter, TXCI, command, NULL, NULL);
122 * The request on endpoint 0 has completed.
123 * Call the user provided completion routine and try
124 * to send the next request.
126 static void usb_ctrl_complete(struct urb *urb, struct pt_regs *regs)
128 struct st5481_adapter *adapter = urb->context;
129 struct st5481_ctrl *ctrl = &adapter->ctrl;
130 struct ctrl_msg *ctrl_msg;
132 if (unlikely(urb->status < 0)) {
133 if (urb->status != -ENOENT && urb->status != -ESHUTDOWN) {
134 WARN("urb status %d",urb->status);
141 ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
143 if (ctrl_msg->dr.bRequest == USB_REQ_CLEAR_FEATURE) {
144 /* Special case handling for pipe reset */
145 le16_to_cpus(&ctrl_msg->dr.wIndex);
146 usb_endpoint_running(adapter->usb_dev,
147 ctrl_msg->dr.wIndex & ~USB_DIR_IN,
148 (ctrl_msg->dr.wIndex & USB_DIR_IN) == 0);
150 /* toggle is reset on clear */
151 usb_settoggle(adapter->usb_dev,
152 ctrl_msg->dr.wIndex & ~USB_DIR_IN,
153 (ctrl_msg->dr.wIndex & USB_DIR_IN) == 0,
159 if (ctrl_msg->complete)
160 ctrl_msg->complete(ctrl_msg->context);
162 clear_bit(0, &ctrl->busy);
164 // Try to send next control message
165 usb_next_ctrl_msg(urb, adapter);
169 /* ======================================================================
174 * The interrupt endpoint will be called when any
175 * of the 6 registers changes state (depending on masks).
176 * Decode the register values and schedule a private event.
177 * Called at interrupt.
179 static void usb_int_complete(struct urb *urb, struct pt_regs *regs)
181 u8 *data = urb->transfer_buffer;
183 struct st5481_adapter *adapter = urb->context;
187 switch (urb->status) {
194 /* this urb is terminated, clean up */
195 DBG(1, "urb shutting down with status: %d", urb->status);
198 WARN("nonzero urb status received: %d", urb->status);
203 DBG_PACKET(1, data, INT_PKT_SIZE);
205 if (urb->actual_length == 0) {
209 irqbyte = data[MPINT];
210 if (irqbyte & DEN_INT)
211 FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
213 if (irqbyte & DCOLL_INT)
214 FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
216 irqbyte = data[FFINT_D];
217 if (irqbyte & OUT_UNDERRUN)
218 FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
220 if (irqbyte & OUT_DOWN)
221 ;// printk("OUT_DOWN\n");
223 irqbyte = data[MPINT];
224 if (irqbyte & RXCI_INT)
225 FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
227 for (j = 0; j < 2; j++)
228 adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
230 urb->actual_length = 0;
233 status = usb_submit_urb (urb, GFP_ATOMIC);
235 WARN("usb_submit_urb failed with result %d", status);
238 /* ======================================================================
242 int st5481_setup_usb(struct st5481_adapter *adapter)
244 struct usb_device *dev = adapter->usb_dev;
245 struct st5481_ctrl *ctrl = &adapter->ctrl;
246 struct st5481_intr *intr = &adapter->intr;
247 struct usb_host_interface *altsetting;
248 struct usb_host_endpoint *endpoint;
255 if ((status = usb_reset_configuration (dev)) < 0) {
256 WARN("reset_configuration failed,status=%d",status);
261 altsetting = &(dev->config->interface[0]->altsetting[3]);
263 // Check if the config is sane
264 if ( altsetting->desc.bNumEndpoints != 7 ) {
265 WARN("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
269 // The descriptor is wrong for some early samples of the ST5481 chip
270 altsetting->endpoint[3].desc.wMaxPacketSize = 32;
271 altsetting->endpoint[4].desc.wMaxPacketSize = 32;
273 // Use alternative setting 3 on interface 0 to have 2B+D
274 if ((status = usb_set_interface (dev, 0, 3)) < 0) {
275 WARN("usb_set_interface failed,status=%d",status);
279 // Allocate URB for control endpoint
280 urb = usb_alloc_urb(0, GFP_KERNEL);
286 // Fill the control URB
287 usb_fill_control_urb (urb, dev,
288 usb_sndctrlpipe(dev, 0),
289 NULL, NULL, 0, usb_ctrl_complete, adapter);
292 fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
294 // Allocate URBs and buffers for interrupt endpoint
295 urb = usb_alloc_urb(0, GFP_KERNEL);
301 buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
306 endpoint = &altsetting->endpoint[EP_INT-1];
308 // Fill the interrupt URB
309 usb_fill_int_urb(urb, dev,
310 usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress),
312 usb_int_complete, adapter,
313 endpoint->desc.bInterval);
319 * Release buffers and URBs for the interrupt and control
322 void st5481_release_usb(struct st5481_adapter *adapter)
324 struct st5481_intr *intr = &adapter->intr;
325 struct st5481_ctrl *ctrl = &adapter->ctrl;
329 // Stop and free Control and Interrupt URBs
330 usb_unlink_urb(ctrl->urb);
331 if (ctrl->urb->transfer_buffer)
332 kfree(ctrl->urb->transfer_buffer);
333 usb_free_urb(ctrl->urb);
335 usb_unlink_urb(intr->urb);
336 if (intr->urb->transfer_buffer)
337 kfree(intr->urb->transfer_buffer);
338 usb_free_urb(intr->urb);
342 * Initialize the adapter.
344 void st5481_start(struct st5481_adapter *adapter)
346 static const u8 init_cmd_table[]={
360 MPMSK,RXCI_INT+DEN_INT+DCOLL_INT,
363 struct st5481_intr *intr = &adapter->intr;
369 adapter->leds = RED_LED;
371 // Start receiving on the interrupt endpoint
372 SUBMIT_URB(intr->urb, GFP_KERNEL);
374 while ((request = init_cmd_table[i++])) {
375 value = init_cmd_table[i++];
376 st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
378 st5481_ph_command(adapter, ST5481_CMD_PUP);
382 * Reset the adapter to default values.
384 void st5481_stop(struct st5481_adapter *adapter)
388 st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
391 /* ======================================================================
392 * isochronous USB helpers
396 fill_isoc_urb(struct urb *urb, struct usb_device *dev,
397 unsigned int pipe, void *buf, int num_packets,
398 int packet_size, usb_complete_t complete,
403 spin_lock_init(&urb->lock);
406 urb->transfer_buffer=buf;
407 urb->number_of_packets = num_packets;
408 urb->transfer_buffer_length=num_packets*packet_size;
409 urb->actual_length = 0;
410 urb->complete=complete;
411 urb->context=context;
412 urb->transfer_flags=URB_ISO_ASAP;
413 for (k = 0; k < num_packets; k++) {
414 urb->iso_frame_desc[k].offset = packet_size * k;
415 urb->iso_frame_desc[k].length = packet_size;
416 urb->iso_frame_desc[k].actual_length = 0;
421 st5481_setup_isocpipes(struct urb* urb[2], struct usb_device *dev,
422 unsigned int pipe, int num_packets,
423 int packet_size, int buf_size,
424 usb_complete_t complete, void *context)
429 for (j = 0; j < 2; j++) {
431 urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL);
435 // Allocate memory for 2000bytes/sec (16Kb/s)
436 buf = kmalloc(buf_size, GFP_KERNEL);
440 // Fill the isochronous URB
441 fill_isoc_urb(urb[j], dev, pipe, buf,
442 num_packets, packet_size, complete,
448 for (j = 0; j < 2; j++) {
450 if (urb[j]->transfer_buffer)
451 kfree(urb[j]->transfer_buffer);
452 usb_free_urb(urb[j]);
458 void st5481_release_isocpipes(struct urb* urb[2])
462 for (j = 0; j < 2; j++) {
463 usb_unlink_urb(urb[j]);
464 if (urb[j]->transfer_buffer)
465 kfree(urb[j]->transfer_buffer);
466 usb_free_urb(urb[j]);
471 * Decode frames received on the B/D channel.
472 * Note that this function will be called continously
473 * with 64Kbit/s / 16Kbit/s of data and hence it will be
474 * called 50 times per second with 20 ISOC descriptors.
475 * Called at interrupt.
477 static void usb_in_complete(struct urb *urb, struct pt_regs *regs)
479 struct st5481_in *in = urb->context;
482 int len, count, status;
484 if (unlikely(urb->status < 0)) {
485 if (urb->status != -ENOENT && urb->status != -ESHUTDOWN) {
486 WARN("urb status %d",urb->status);
493 DBG_ISO_PACKET(0x80,urb);
495 len = st5481_isoc_flatten(urb);
496 ptr = urb->transfer_buffer;
498 if (in->mode == L1_MODE_TRANS) {
499 memcpy(in->rcvbuf, ptr, len);
503 status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
504 in->rcvbuf, in->bufsize);
510 // Good frame received
511 DBG(4,"count=%d",status);
512 DBG_PACKET(0x400, in->rcvbuf, status);
513 if (!(skb = dev_alloc_skb(status))) {
514 WARN("receive out of memory\n");
517 memcpy(skb_put(skb, status), in->rcvbuf, status);
518 in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb);
519 } else if (status == -HDLC_CRC_ERROR) {
521 } else if (status == -HDLC_FRAMING_ERROR) {
522 INFO("framing error");
523 } else if (status == -HDLC_LENGTH_ERROR) {
524 INFO("length error");
528 // Prepare URB for next transfer
529 urb->dev = in->adapter->usb_dev;
530 urb->actual_length = 0;
532 SUBMIT_URB(urb, GFP_ATOMIC);
535 int st5481_setup_in(struct st5481_in *in)
537 struct usb_device *dev = in->adapter->usb_dev;
542 in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
547 retval = st5481_setup_isocpipes(in->urb, dev,
548 usb_rcvisocpipe(dev, in->ep),
549 in->num_packets, in->packet_size,
550 in->num_packets * in->packet_size,
551 usb_in_complete, in);
562 void st5481_release_in(struct st5481_in *in)
566 st5481_release_isocpipes(in->urb);
570 * Make the transfer_buffer contiguous by
571 * copying from the iso descriptors if necessary.
573 int st5481_isoc_flatten(struct urb *urb)
575 struct usb_iso_packet_descriptor *pipd,*pend;
576 unsigned char *src,*dst;
579 if (urb->status < 0) {
582 for (pipd = &urb->iso_frame_desc[0],
583 pend = &urb->iso_frame_desc[urb->number_of_packets],
584 dst = urb->transfer_buffer;
588 if (pipd->status < 0) {
589 return (pipd->status);
592 len = pipd->actual_length;
593 pipd->actual_length = 0;
594 src = urb->transfer_buffer+pipd->offset;
597 // Need to copy since isoc buffers not full
602 // No need to copy, just update destination buffer
606 // Return size of flattened buffer
607 return (dst - (unsigned char *)urb->transfer_buffer);
610 static void st5481_start_rcv(void *context)
612 struct st5481_in *in = context;
613 struct st5481_adapter *adapter = in->adapter;
617 in->urb[0]->dev = adapter->usb_dev;
618 SUBMIT_URB(in->urb[0], GFP_KERNEL);
620 in->urb[1]->dev = adapter->usb_dev;
621 SUBMIT_URB(in->urb[1], GFP_KERNEL);
624 void st5481_in_mode(struct st5481_in *in, int mode)
626 if (in->mode == mode)
631 usb_unlink_urb(in->urb[0]);
632 usb_unlink_urb(in->urb[1]);
634 if (in->mode != L1_MODE_NULL) {
635 if (in->mode != L1_MODE_TRANS)
636 isdnhdlc_rcv_init(&in->hdlc_state,
637 in->mode == L1_MODE_HDLC_56K);
639 st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
640 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
643 st5481_start_rcv(in);
645 st5481_usb_device_ctrl_msg(in->adapter, in->counter,