[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / isdn / hisax / st5481_usb.c
blobab62223297a56d8cdd2a097341ccd49ade474f42
1 /*
2 * Driver for ST5481 USB ISDN modem
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.
13 #include <linux/init.h>
14 #include <linux/usb.h>
15 #include <linux/slab.h>
16 #include "st5481.h"
18 static int st5481_isoc_flatten(struct urb *urb);
20 /* ======================================================================
21 * control pipe
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;
32 int r_index;
34 if (test_and_set_bit(0, &ctrl->busy)) {
35 return;
38 if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) {
39 test_and_clear_bit(0,&ctrl->busy);
40 return;
42 urb->setup_packet =
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);
50 // Prepare the URB
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;
65 int w_index;
66 struct ctrl_msg *ctrl_msg;
68 if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
69 WARN("control msg FIFO full");
70 return;
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,
101 u_char pipe,
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);
137 } else {
138 DBG(1,"urb killed");
139 return; // Give up
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);
165 return;
168 /* ======================================================================
169 * interrupt pipe
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;
181 u8 irqbyte;
182 struct st5481_adapter *adapter = urb->context;
183 int j;
184 int status;
186 switch (urb->status) {
187 case 0:
188 /* success */
189 break;
190 case -ECONNRESET:
191 case -ENOENT:
192 case -ESHUTDOWN:
193 /* this urb is terminated, clean up */
194 DBG(1, "urb shutting down with status: %d", urb->status);
195 return;
196 default:
197 WARN("nonzero urb status received: %d", urb->status);
198 goto exit;
202 DBG_PACKET(1, data, INT_PKT_SIZE);
204 if (urb->actual_length == 0) {
205 goto exit;
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;
231 exit:
232 status = usb_submit_urb (urb, GFP_ATOMIC);
233 if (status)
234 WARN("usb_submit_urb failed with result %d", status);
237 /* ======================================================================
238 * initialization
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;
249 int status;
250 struct urb *urb;
251 u8 *buf;
253 DBG(1,"");
255 if ((status = usb_reset_configuration (dev)) < 0) {
256 WARN("reset_configuration failed,status=%d",status);
257 return status;
260 intf = usb_ifnum_to_if(dev, 0);
261 if (intf)
262 altsetting = usb_altnum_to_altsetting(intf, 3);
263 if (!altsetting)
264 return -ENXIO;
266 // Check if the config is sane
267 if ( altsetting->desc.bNumEndpoints != 7 ) {
268 WARN("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
269 return -EINVAL;
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);
279 return status;
282 // Allocate URB for control endpoint
283 urb = usb_alloc_urb(0, GFP_KERNEL);
284 if (!urb) {
285 return -ENOMEM;
287 ctrl->urb = urb;
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);
299 if (!urb) {
300 return -ENOMEM;
302 intr->urb = urb;
304 buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
305 if (!buf) {
306 return -ENOMEM;
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),
314 buf, INT_PKT_SIZE,
315 usb_int_complete, adapter,
316 endpoint->desc.bInterval);
318 return 0;
322 * Release buffers and URBs for the interrupt and control
323 * endpoint.
325 void st5481_release_usb(struct st5481_adapter *adapter)
327 struct st5481_intr *intr = &adapter->intr;
328 struct st5481_ctrl *ctrl = &adapter->ctrl;
330 DBG(1,"");
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[]={
350 SET_DEFAULT,0,
351 STT,0,
352 SDA_MIN,0x0d,
353 SDA_MAX,0x29,
354 SDELAY_VALUE,0x14,
355 GPIO_DIR,0x01,
356 GPIO_OUT,RED_LED,
357 // FFCTRL_OUT_D,4,
358 // FFCTRH_OUT_D,12,
359 FFCTRL_OUT_B1,6,
360 FFCTRH_OUT_B1,20,
361 FFCTRL_OUT_B2,6,
362 FFCTRH_OUT_B2,20,
363 MPMSK,RXCI_INT+DEN_INT+DCOLL_INT,
366 struct st5481_intr *intr = &adapter->intr;
367 int i = 0;
368 u8 request,value;
370 DBG(8,"");
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)
389 DBG(8,"");
391 st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
394 /* ======================================================================
395 * isochronous USB helpers
398 static void
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,
402 void *context)
404 int k;
406 spin_lock_init(&urb->lock);
407 urb->dev=dev;
408 urb->pipe=pipe;
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)
429 int j, retval;
430 unsigned char *buf;
432 for (j = 0; j < 2; j++) {
433 retval = -ENOMEM;
434 urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL);
435 if (!urb[j])
436 goto err;
438 // Allocate memory for 2000bytes/sec (16Kb/s)
439 buf = kmalloc(buf_size, GFP_KERNEL);
440 if (!buf)
441 goto err;
443 // Fill the isochronous URB
444 fill_isoc_urb(urb[j], dev, pipe, buf,
445 num_packets, packet_size, complete,
446 context);
448 return 0;
450 err:
451 for (j = 0; j < 2; j++) {
452 if (urb[j]) {
453 if (urb[j]->transfer_buffer)
454 kfree(urb[j]->transfer_buffer);
455 usb_free_urb(urb[j]);
458 return retval;
461 void st5481_release_isocpipes(struct urb* urb[2])
463 int j;
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;
483 unsigned char *ptr;
484 struct sk_buff *skb;
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);
490 } else {
491 DBG(1,"urb killed");
492 return; // Give up
496 DBG_ISO_PACKET(0x80,urb);
498 len = st5481_isoc_flatten(urb);
499 ptr = urb->transfer_buffer;
500 while (len > 0) {
501 if (in->mode == L1_MODE_TRANS) {
502 memcpy(in->rcvbuf, ptr, len);
503 status = len;
504 len = 0;
505 } else {
506 status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
507 in->rcvbuf, in->bufsize);
508 ptr += count;
509 len -= count;
512 if (status > 0) {
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");
518 break;
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) {
523 INFO("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;
541 int retval;
543 DBG(4,"");
545 in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
546 retval = -ENOMEM;
547 if (!in->rcvbuf)
548 goto err;
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);
555 if (retval)
556 goto err_free;
557 return 0;
559 err_free:
560 kfree(in->rcvbuf);
561 err:
562 return retval;
565 void st5481_release_in(struct st5481_in *in)
567 DBG(2,"");
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;
580 unsigned int len;
582 if (urb->status < 0) {
583 return urb->status;
585 for (pipd = &urb->iso_frame_desc[0],
586 pend = &urb->iso_frame_desc[urb->number_of_packets],
587 dst = urb->transfer_buffer;
588 pipd < pend;
589 pipd++) {
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;
599 if (src != dst) {
600 // Need to copy since isoc buffers not full
601 while (len--) {
602 *dst++ = *src++;
604 } else {
605 // No need to copy, just update destination buffer
606 dst += len;
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;
618 DBG(4,"");
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)
630 return;
632 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,
644 in->packet_size,
645 NULL, NULL);
646 st5481_start_rcv(in);
647 } else {
648 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
649 0, NULL, NULL);