[PATCH] drivers/isdn/act2000/capi.c: #if 0 an unused function
[linux-2.6] / drivers / isdn / hisax / st5481_b.c
1 /*
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 <linux/netdevice.h>
17 #include "st5481.h"
18
19 static inline void B_L1L2(struct st5481_bcs *bcs, int pr, void *arg)
20 {
21         struct hisax_if *ifc = (struct hisax_if *) &bcs->b_if;
22
23         ifc->l1l2(ifc, pr, arg);
24 }
25
26 /*
27  * Encode and transmit next frame.
28  */
29 static void usb_b_out(struct st5481_bcs *bcs,int buf_nr)
30 {
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;
38         
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;
42         }
43         urb = b_out->urb[buf_nr];
44
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;
57         }
58         b_out->flow_event = 0;
59
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);
65                         
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++];
76                                 }
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);
82                         }
83
84                         skb_pull(skb, bytes_sent);
85
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);
91
92 /*                              if (!(bcs->tx_skb = skb_dequeue(&bcs->sq))) { */
93 /*                                      st5481B_sched_event(bcs, B_XMTBUFREADY); */
94 /*                              } */
95                         }
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);
105                         }
106                 }       
107         }
108
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;
115         }
116         urb->transfer_buffer_length = len;
117         urb->number_of_packets = i;
118         urb->dev = adapter->usb_dev;
119
120         DBG_ISO_PACKET(0x200,urb);
121
122         SUBMIT_URB(urb, GFP_NOIO);
123 }
124
125 /*
126  * Start transfering (flags or data) on the B channel, since
127  * FIFO counters has been set to a non-zero value.
128  */
129 static void st5481B_start_xfer(void *context)
130 {
131         struct st5481_bcs *bcs = context;
132
133         DBG(4,"B%d",bcs->channel+1);
134
135         // Start transmitting (flags or data) on B channel
136
137         usb_b_out(bcs,0);
138         usb_b_out(bcs,1);
139 }
140
141 /*
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.
145  */
146 static void led_blink(struct st5481_adapter *adapter)
147 {
148         u_char leds = adapter->leds;
149
150         // 50 frames/sec for each channel
151         if (++adapter->led_counter % 50) {
152                 return;
153         }
154
155         if (adapter->led_counter % 100) {
156                 leds |= GREEN_LED;
157         } else {
158                 leds &= ~GREEN_LED;
159         }
160         
161         st5481_usb_device_ctrl_msg(adapter, GPIO_OUT, leds, NULL, NULL);
162 }
163
164 static void usb_b_out_complete(struct urb *urb, struct pt_regs *regs)
165 {
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;
170         
171         buf_nr = get_buf_nr(b_out->urb, urb);
172         test_and_clear_bit(buf_nr, &b_out->busy);
173
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);
179                         }
180                 } else {
181                         DBG(1,"urb killed"); 
182                         return; // Give up
183                 }
184         }
185
186         usb_b_out(bcs,buf_nr);
187
188         if (adapter->number_of_leds == 2)
189                 led_blink(adapter);
190 }
191
192 /*
193  * Start or stop the transfer on the B channel.
194  */
195 static void st5481B_mode(struct st5481_bcs *bcs, int mode)
196 {
197         struct st5481_b_out *b_out = &bcs->b_out;
198         struct st5481_adapter *adapter = bcs->adapter;
199
200         DBG(4,"B%d,mode=%d", bcs->channel + 1, mode);
201
202         if (bcs->mode == mode)
203                 return;
204
205         bcs->mode = mode;
206
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;
211
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);
217                 }
218                 st5481_usb_pipe_reset(adapter, (bcs->channel+1)*2, NULL, NULL);
219         
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);
223
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;
231                         }
232                 }
233         } else {
234                 // Disble B channel interrupts
235                 st5481_usb_device_ctrl_msg(adapter, FFMSK_B1+(bcs->channel*2), 0, NULL, NULL);
236
237                 // Disable B channel FIFOs
238                 st5481_usb_device_ctrl_msg(adapter, OUT_B1_COUNTER+(bcs->channel*2), 0, NULL, NULL);
239
240                 if (adapter->number_of_leds == 4) {
241                         if (bcs->channel == 0) {
242                                 adapter->leds &= ~B1_LED;
243                         } else {
244                                 adapter->leds &= ~B2_LED;
245                         }
246                 } else {
247                         st5481_usb_device_ctrl_msg(adapter, GPIO_OUT, adapter->leds, NULL, NULL);
248                 }
249                 if (b_out->tx_skb) {
250                         dev_kfree_skb_any(b_out->tx_skb);
251                         b_out->tx_skb = NULL;
252                 }
253                 
254         }
255 }
256
257 static int st5481_setup_b_out(struct st5481_bcs *bcs)
258 {
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;
264
265         DBG(4,"");
266
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;
272
273         // Allocate URBs and buffers for the B channel out
274         endpoint = &altsetting->endpoint[EP_B1_OUT - 1 + bcs->channel * 2];
275
276         DBG(4,"endpoint address=%02x,packet size=%d",
277             endpoint->desc.bEndpointAddress, le16_to_cpu(endpoint->desc.wMaxPacketSize));
278
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);
285 }
286
287 static void st5481_release_b_out(struct st5481_bcs *bcs)
288 {
289         struct st5481_b_out *b_out = &bcs->b_out;
290
291         DBG(4,"");
292
293         st5481_release_isocpipes(b_out->urb);
294 }
295
296 int st5481_setup_b(struct st5481_bcs *bcs)
297 {
298         int retval;
299
300         DBG(4,"");
301
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;
315
316
317         return 0;
318
319  err_b_out:
320         st5481_release_b_out(bcs);
321  err:
322         return retval;
323 }
324
325 /*
326  * Release buffers and URBs for the B channels
327  */
328 void st5481_release_b(struct st5481_bcs *bcs)
329 {
330         DBG(4,"");
331
332         st5481_release_in(&bcs->b_in);
333         st5481_release_b_out(bcs);
334 }
335
336 /*
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.
344  */
345 void st5481_b_l2l1(struct hisax_if *ifc, int pr, void *arg)
346 {
347         struct st5481_bcs *bcs = ifc->priv;
348         struct sk_buff *skb = arg;
349         int mode;
350
351         DBG(4, "");
352
353         switch (pr) {
354         case PH_DATA | REQUEST:
355                 if (bcs->b_out.tx_skb)
356                         BUG();
357                 
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);
373         }
374 }