[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / isdn / hisax / st5481_b.c
blob2fcd093921d8c3337934918a3989880e5a2b82ef
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 <linux/netdevice.h>
17 #include "st5481.h"
19 static inline void B_L1L2(struct st5481_bcs *bcs, int pr, void *arg)
21 struct hisax_if *ifc = (struct hisax_if *) &bcs->b_if;
23 ifc->l1l2(ifc, pr, arg);
27 * Encode and transmit next frame.
29 static void usb_b_out(struct st5481_bcs *bcs,int buf_nr)
31 struct st5481_b_out *b_out = &bcs->b_out;
32 struct st5481_adapter *adapter = bcs->adapter;
33 struct urb *urb;
34 unsigned int packet_size,offset;
35 int len,buf_size,bytes_sent;
36 int i;
37 struct sk_buff *skb;
39 if (test_and_set_bit(buf_nr, &b_out->busy)) {
40 DBG(4,"ep %d urb %d busy",(bcs->channel+1)*2,buf_nr);
41 return;
43 urb = b_out->urb[buf_nr];
45 // Adjust isoc buffer size according to flow state
46 if(b_out->flow_event & (OUT_DOWN | OUT_UNDERRUN)) {
47 buf_size = NUM_ISO_PACKETS_B*SIZE_ISO_PACKETS_B_OUT + B_FLOW_ADJUST;
48 packet_size = SIZE_ISO_PACKETS_B_OUT + B_FLOW_ADJUST;
49 DBG(4,"B%d,adjust flow,add %d bytes",bcs->channel+1,B_FLOW_ADJUST);
50 } else if(b_out->flow_event & OUT_UP){
51 buf_size = NUM_ISO_PACKETS_B*SIZE_ISO_PACKETS_B_OUT - B_FLOW_ADJUST;
52 packet_size = SIZE_ISO_PACKETS_B_OUT - B_FLOW_ADJUST;
53 DBG(4,"B%d,adjust flow,remove %d bytes",bcs->channel+1,B_FLOW_ADJUST);
54 } else {
55 buf_size = NUM_ISO_PACKETS_B*SIZE_ISO_PACKETS_B_OUT;
56 packet_size = 8;
58 b_out->flow_event = 0;
60 len = 0;
61 while (len < buf_size) {
62 if ((skb = b_out->tx_skb)) {
63 DBG_SKB(0x100, skb);
64 DBG(4,"B%d,len=%d",bcs->channel+1,skb->len);
66 if (bcs->mode == L1_MODE_TRANS) {
67 bytes_sent = buf_size - len;
68 if (skb->len < bytes_sent)
69 bytes_sent = skb->len;
70 { /* swap tx bytes to get hearable audio data */
71 register unsigned char *src = skb->data;
72 register unsigned char *dest = urb->transfer_buffer+len;
73 register unsigned int count;
74 for (count = 0; count < bytes_sent; count++)
75 *dest++ = isdnhdlc_bit_rev_tab[*src++];
77 len += bytes_sent;
78 } else {
79 len += isdnhdlc_encode(&b_out->hdlc_state,
80 skb->data, skb->len, &bytes_sent,
81 urb->transfer_buffer+len, buf_size-len);
84 skb_pull(skb, bytes_sent);
86 if (!skb->len) {
87 // Frame sent
88 b_out->tx_skb = NULL;
89 B_L1L2(bcs, PH_DATA | CONFIRM, (void *) skb->truesize);
90 dev_kfree_skb_any(skb);
92 /* if (!(bcs->tx_skb = skb_dequeue(&bcs->sq))) { */
93 /* st5481B_sched_event(bcs, B_XMTBUFREADY); */
94 /* } */
96 } else {
97 if (bcs->mode == L1_MODE_TRANS) {
98 memset(urb->transfer_buffer+len, 0xff, buf_size-len);
99 len = buf_size;
100 } else {
101 // Send flags
102 len += isdnhdlc_encode(&b_out->hdlc_state,
103 NULL, 0, &bytes_sent,
104 urb->transfer_buffer+len, buf_size-len);
109 // Prepare the URB
110 for (i = 0, offset = 0; offset < len; i++) {
111 urb->iso_frame_desc[i].offset = offset;
112 urb->iso_frame_desc[i].length = packet_size;
113 offset += packet_size;
114 packet_size = SIZE_ISO_PACKETS_B_OUT;
116 urb->transfer_buffer_length = len;
117 urb->number_of_packets = i;
118 urb->dev = adapter->usb_dev;
120 DBG_ISO_PACKET(0x200,urb);
122 SUBMIT_URB(urb, GFP_NOIO);
126 * Start transfering (flags or data) on the B channel, since
127 * FIFO counters has been set to a non-zero value.
129 static void st5481B_start_xfer(void *context)
131 struct st5481_bcs *bcs = context;
133 DBG(4,"B%d",bcs->channel+1);
135 // Start transmitting (flags or data) on B channel
137 usb_b_out(bcs,0);
138 usb_b_out(bcs,1);
142 * If the adapter has only 2 LEDs, the green
143 * LED will blink with a rate depending
144 * on the number of channels opened.
146 static void led_blink(struct st5481_adapter *adapter)
148 u_char leds = adapter->leds;
150 // 50 frames/sec for each channel
151 if (++adapter->led_counter % 50) {
152 return;
155 if (adapter->led_counter % 100) {
156 leds |= GREEN_LED;
157 } else {
158 leds &= ~GREEN_LED;
161 st5481_usb_device_ctrl_msg(adapter, GPIO_OUT, leds, NULL, NULL);
164 static void usb_b_out_complete(struct urb *urb, struct pt_regs *regs)
166 struct st5481_bcs *bcs = urb->context;
167 struct st5481_b_out *b_out = &bcs->b_out;
168 struct st5481_adapter *adapter = bcs->adapter;
169 int buf_nr;
171 buf_nr = get_buf_nr(b_out->urb, urb);
172 test_and_clear_bit(buf_nr, &b_out->busy);
174 if (unlikely(urb->status < 0)) {
175 if (urb->status != -ENOENT && urb->status != -ESHUTDOWN) {
176 WARN("urb status %d",urb->status);
177 if (b_out->busy == 0) {
178 st5481_usb_pipe_reset(adapter, (bcs->channel+1)*2 | USB_DIR_OUT, NULL, NULL);
180 } else {
181 DBG(1,"urb killed");
182 return; // Give up
186 usb_b_out(bcs,buf_nr);
188 if (adapter->number_of_leds == 2)
189 led_blink(adapter);
193 * Start or stop the transfer on the B channel.
195 static void st5481B_mode(struct st5481_bcs *bcs, int mode)
197 struct st5481_b_out *b_out = &bcs->b_out;
198 struct st5481_adapter *adapter = bcs->adapter;
200 DBG(4,"B%d,mode=%d", bcs->channel + 1, mode);
202 if (bcs->mode == mode)
203 return;
205 bcs->mode = mode;
207 // Cancel all USB transfers on this B channel
208 usb_unlink_urb(b_out->urb[0]);
209 usb_unlink_urb(b_out->urb[1]);
210 b_out->busy = 0;
212 st5481_in_mode(&bcs->b_in, mode);
213 if (bcs->mode != L1_MODE_NULL) {
214 // Open the B channel
215 if (bcs->mode != L1_MODE_TRANS) {
216 isdnhdlc_out_init(&b_out->hdlc_state, 0, bcs->mode == L1_MODE_HDLC_56K);
218 st5481_usb_pipe_reset(adapter, (bcs->channel+1)*2, NULL, NULL);
220 // Enable B channel interrupts
221 st5481_usb_device_ctrl_msg(adapter, FFMSK_B1+(bcs->channel*2),
222 OUT_UP+OUT_DOWN+OUT_UNDERRUN, NULL, NULL);
224 // Enable B channel FIFOs
225 st5481_usb_device_ctrl_msg(adapter, OUT_B1_COUNTER+(bcs->channel*2), 32, st5481B_start_xfer, bcs);
226 if (adapter->number_of_leds == 4) {
227 if (bcs->channel == 0) {
228 adapter->leds |= B1_LED;
229 } else {
230 adapter->leds |= B2_LED;
233 } else {
234 // Disble B channel interrupts
235 st5481_usb_device_ctrl_msg(adapter, FFMSK_B1+(bcs->channel*2), 0, NULL, NULL);
237 // Disable B channel FIFOs
238 st5481_usb_device_ctrl_msg(adapter, OUT_B1_COUNTER+(bcs->channel*2), 0, NULL, NULL);
240 if (adapter->number_of_leds == 4) {
241 if (bcs->channel == 0) {
242 adapter->leds &= ~B1_LED;
243 } else {
244 adapter->leds &= ~B2_LED;
246 } else {
247 st5481_usb_device_ctrl_msg(adapter, GPIO_OUT, adapter->leds, NULL, NULL);
249 if (b_out->tx_skb) {
250 dev_kfree_skb_any(b_out->tx_skb);
251 b_out->tx_skb = NULL;
257 static int st5481_setup_b_out(struct st5481_bcs *bcs)
259 struct usb_device *dev = bcs->adapter->usb_dev;
260 struct usb_interface *intf;
261 struct usb_host_interface *altsetting = NULL;
262 struct usb_host_endpoint *endpoint;
263 struct st5481_b_out *b_out = &bcs->b_out;
265 DBG(4,"");
267 intf = usb_ifnum_to_if(dev, 0);
268 if (intf)
269 altsetting = usb_altnum_to_altsetting(intf, 3);
270 if (!altsetting)
271 return -ENXIO;
273 // Allocate URBs and buffers for the B channel out
274 endpoint = &altsetting->endpoint[EP_B1_OUT - 1 + bcs->channel * 2];
276 DBG(4,"endpoint address=%02x,packet size=%d",
277 endpoint->desc.bEndpointAddress, le16_to_cpu(endpoint->desc.wMaxPacketSize));
279 // Allocate memory for 8000bytes/sec + extra bytes if underrun
280 return st5481_setup_isocpipes(b_out->urb, dev,
281 usb_sndisocpipe(dev, endpoint->desc.bEndpointAddress),
282 NUM_ISO_PACKETS_B, SIZE_ISO_PACKETS_B_OUT,
283 NUM_ISO_PACKETS_B * SIZE_ISO_PACKETS_B_OUT + B_FLOW_ADJUST,
284 usb_b_out_complete, bcs);
287 static void st5481_release_b_out(struct st5481_bcs *bcs)
289 struct st5481_b_out *b_out = &bcs->b_out;
291 DBG(4,"");
293 st5481_release_isocpipes(b_out->urb);
296 int st5481_setup_b(struct st5481_bcs *bcs)
298 int retval;
300 DBG(4,"");
302 retval = st5481_setup_b_out(bcs);
303 if (retval)
304 goto err;
305 bcs->b_in.bufsize = HSCX_BUFMAX;
306 bcs->b_in.num_packets = NUM_ISO_PACKETS_B;
307 bcs->b_in.packet_size = SIZE_ISO_PACKETS_B_IN;
308 bcs->b_in.ep = (bcs->channel ? EP_B2_IN : EP_B1_IN) | USB_DIR_IN;
309 bcs->b_in.counter = bcs->channel ? IN_B2_COUNTER : IN_B1_COUNTER;
310 bcs->b_in.adapter = bcs->adapter;
311 bcs->b_in.hisax_if = &bcs->b_if.ifc;
312 retval = st5481_setup_in(&bcs->b_in);
313 if (retval)
314 goto err_b_out;
317 return 0;
319 err_b_out:
320 st5481_release_b_out(bcs);
321 err:
322 return retval;
326 * Release buffers and URBs for the B channels
328 void st5481_release_b(struct st5481_bcs *bcs)
330 DBG(4,"");
332 st5481_release_in(&bcs->b_in);
333 st5481_release_b_out(bcs);
337 * st5481_b_l2l1 is the entry point for upper layer routines that want to
338 * transmit on the B channel. PH_DATA | REQUEST is a normal packet that
339 * we either start transmitting (if idle) or queue (if busy).
340 * PH_PULL | REQUEST can be called to request a callback message
341 * (PH_PULL | CONFIRM)
342 * once the link is idle. After a "pull" callback, the upper layer
343 * routines can use PH_PULL | INDICATION to send data.
345 void st5481_b_l2l1(struct hisax_if *ifc, int pr, void *arg)
347 struct st5481_bcs *bcs = ifc->priv;
348 struct sk_buff *skb = arg;
349 int mode;
351 DBG(4, "");
353 switch (pr) {
354 case PH_DATA | REQUEST:
355 if (bcs->b_out.tx_skb)
356 BUG();
358 bcs->b_out.tx_skb = skb;
359 break;
360 case PH_ACTIVATE | REQUEST:
361 mode = (int) arg;
362 DBG(4,"B%d,PH_ACTIVATE_REQUEST %d", bcs->channel + 1, mode);
363 st5481B_mode(bcs, mode);
364 B_L1L2(bcs, PH_ACTIVATE | INDICATION, NULL);
365 break;
366 case PH_DEACTIVATE | REQUEST:
367 DBG(4,"B%d,PH_DEACTIVATE_REQUEST", bcs->channel + 1);
368 st5481B_mode(bcs, L1_MODE_NULL);
369 B_L1L2(bcs, PH_DEACTIVATE | INDICATION, NULL);
370 break;
371 default:
372 WARN("pr %#x\n", pr);