v2.5.0.7 -> v2.5.0.8
[opensuse:kernel.git] / drivers / isdn / hisax / st5481_usb.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 "st5481.h"
17
18 /* ======================================================================
19  * control pipe
20  */
21
22 /*
23  * Send the next endpoint 0 request stored in the FIFO.
24  * Called either by the completion or by usb_ctrl_msg.
25  */
26 static void usb_next_ctrl_msg(struct urb *urb,
27                               struct st5481_adapter *adapter)
28 {
29         struct st5481_ctrl *ctrl = &adapter->ctrl;
30         int r_index;
31
32         if (test_and_set_bit(0, &ctrl->busy)) {
33                 return;
34         }
35
36         if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) {
37                 test_and_clear_bit(0,&ctrl->busy);
38                 return;
39         } 
40         urb->setup_packet = 
41                 (unsigned char *)&ctrl->msg_fifo.data[r_index];
42         
43         DBG(1,"request=0x%02x,value=0x%04x,index=%x",
44             ((struct ctrl_msg *)urb->setup_packet)->dr.request,
45             ((struct ctrl_msg *)urb->setup_packet)->dr.value,
46             ((struct ctrl_msg *)urb->setup_packet)->dr.index);
47
48         // Prepare the URB
49         urb->dev = adapter->usb_dev;
50
51         SUBMIT_URB(urb);
52 }
53
54 /*
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.
57  */
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)
61 {
62         struct st5481_ctrl *ctrl = &adapter->ctrl;
63         int w_index;
64         struct ctrl_msg *ctrl_msg;
65         
66         if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
67                 WARN("control msg FIFO full");
68                 return;
69         }
70         ctrl_msg = &ctrl->msg_fifo.data[w_index]; 
71    
72         ctrl_msg->dr.requesttype = requesttype;
73         ctrl_msg->dr.request = request;
74         ctrl_msg->dr.value = cpu_to_le16p(&value);
75         ctrl_msg->dr.index = cpu_to_le16p(&index);
76         ctrl_msg->dr.length = 0;
77         ctrl_msg->complete = complete;
78         ctrl_msg->context = context;
79
80         usb_next_ctrl_msg(ctrl->urb, adapter);
81 }
82
83 /*
84  * Asynchronous endpoint 0 device request.
85  */
86 void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
87                          u8 request, u16 value,
88                          ctrl_complete_t complete, void *context)
89 {
90         usb_ctrl_msg(adapter, request, 
91                      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 
92                      value, 0, complete, context);
93 }
94
95 /*
96  * Asynchronous pipe reset (async version of usb_clear_halt).
97  */
98 void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
99                     u_char pipe,
100                     ctrl_complete_t complete, void *context)
101 {
102         DBG(1,"pipe=%02x",pipe);
103
104         usb_ctrl_msg(adapter,
105                      USB_REQ_CLEAR_FEATURE, USB_DIR_OUT | USB_RECIP_ENDPOINT,
106                      0, pipe, complete, context);
107 }
108
109
110 /*
111   Physical level functions
112 */
113
114 void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command)
115 {
116         DBG(8,"command=%s", ST5481_CMD_string(command));
117
118         st5481_usb_device_ctrl_msg(adapter, TXCI, command, NULL, NULL);
119 }
120
121 /*
122  * The request on endpoint 0 has completed.
123  * Call the user provided completion routine and try
124  * to send the next request.
125  */
126 static void usb_ctrl_complete(struct urb *urb)
127 {
128         struct st5481_adapter *adapter = urb->context;
129         struct st5481_ctrl *ctrl = &adapter->ctrl;
130         struct ctrl_msg *ctrl_msg;
131         
132         if (urb->status < 0) {
133                 if (urb->status != -ENOENT) {
134                         WARN("urb status %d",urb->status);
135                 } else {
136                         DBG(1,"urb killed");
137                         return; // Give up
138                 }
139         }
140
141         ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
142         
143         if (ctrl_msg->dr.request == USB_REQ_CLEAR_FEATURE) {
144                 /* Special case handling for pipe reset */
145                 le16_to_cpus(&ctrl_msg->dr.index);
146                 usb_endpoint_running(adapter->usb_dev,
147                                      ctrl_msg->dr.index & ~USB_DIR_IN, 
148                                      (ctrl_msg->dr.index & USB_DIR_IN) == 0);
149
150                 /* toggle is reset on clear */
151                 usb_settoggle(adapter->usb_dev, 
152                               ctrl_msg->dr.index & ~USB_DIR_IN, 
153                               (ctrl_msg->dr.index & USB_DIR_IN) == 0,
154                               0);
155
156
157         }
158         
159         if (ctrl_msg->complete)
160                 ctrl_msg->complete(ctrl_msg->context);
161
162         clear_bit(0, &ctrl->busy);
163         
164         // Try to send next control message
165         usb_next_ctrl_msg(urb, adapter);
166         return;
167 }
168
169 /* ======================================================================
170  * interrupt pipe
171  */
172
173 /*
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.
178  */
179 static void usb_int_complete(struct urb *urb)
180 {
181         u_char *data = urb->transfer_buffer;
182         u_char irqbyte;
183         struct st5481_adapter *adapter = urb->context;
184         int j;
185
186         if (urb->status < 0) {
187                 if (urb->status != -ENOENT) {
188                         WARN("urb status %d",urb->status);
189                         urb->actual_length = 0;
190                 } else {
191                         DBG(1,"urb killed");
192                         return; // Give up
193                 }
194         }
195         
196         DBG_PACKET(1, data, INT_PKT_SIZE);
197                 
198         if (urb->actual_length == 0) {
199                 return;
200         }
201
202         irqbyte = data[MPINT];
203         if (irqbyte & DEN_INT)
204                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
205
206         if (irqbyte & DCOLL_INT)
207                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
208
209         irqbyte = data[FFINT_D];
210         if (irqbyte & OUT_UNDERRUN)
211                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
212
213         if (irqbyte & OUT_DOWN)
214 ;//             printk("OUT_DOWN\n");
215
216         irqbyte = data[MPINT];
217         if (irqbyte & RXCI_INT)
218                 FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
219
220         for (j = 0; j < 2; j++)
221                 adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
222
223         urb->actual_length = 0;
224 }
225
226 /* ======================================================================
227  * initialization
228  */
229
230 int __devinit st5481_setup_usb(struct st5481_adapter *adapter)
231 {
232         struct usb_device *dev = adapter->usb_dev;
233         struct st5481_ctrl *ctrl = &adapter->ctrl;
234         struct st5481_intr *intr = &adapter->intr;
235         struct usb_interface_descriptor *altsetting;
236         struct usb_endpoint_descriptor *endpoint;
237         int status;
238         urb_t *urb;
239         u_char *buf;
240         
241         DBG(1,"");
242         
243         if ((status = usb_set_configuration (dev,dev->config[0].bConfigurationValue)) < 0) {
244                 WARN("set_configuration failed,status=%d",status);
245                 return status;
246         }
247
248         
249         altsetting = &(dev->config->interface[0].altsetting[3]);        
250
251         // Check if the config is sane
252         if ( altsetting->bNumEndpoints != 7 ) {
253                 WARN("expecting 7 got %d endpoints!", altsetting->bNumEndpoints);
254                 return -EINVAL;
255         }
256
257         // The descriptor is wrong for some early samples of the ST5481 chip
258         altsetting->endpoint[3].wMaxPacketSize = 32;
259         altsetting->endpoint[4].wMaxPacketSize = 32;
260
261         // Use alternative setting 3 on interface 0 to have 2B+D
262         if ((status = usb_set_interface (dev, 0, 3)) < 0) {
263                 WARN("usb_set_interface failed,status=%d",status);
264                 return status;
265         }
266
267         // Allocate URB for control endpoint
268         urb = usb_alloc_urb(0);
269         if (!urb) {
270                 return -ENOMEM;
271         }
272         ctrl->urb = urb;
273         
274         // Fill the control URB
275         FILL_CONTROL_URB (urb, dev, 
276                           usb_sndctrlpipe(dev, 0),
277                           NULL, NULL, 0, usb_ctrl_complete, adapter);
278
279                 
280         fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
281
282         // Allocate URBs and buffers for interrupt endpoint
283         urb = usb_alloc_urb(0);
284         if (!urb) { 
285                 return -ENOMEM;
286         }
287         intr->urb = urb;
288         
289         buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
290         if (!buf) {
291                 return -ENOMEM;
292         }
293
294         endpoint = &altsetting->endpoint[EP_INT-1];
295                                 
296         // Fill the interrupt URB
297         FILL_INT_URB(urb, dev,
298                      usb_rcvintpipe(dev, endpoint->bEndpointAddress),
299                      buf, INT_PKT_SIZE,
300                      usb_int_complete, adapter,
301                      endpoint->bInterval);
302                 
303         return 0;
304 }
305
306 /*
307  * Release buffers and URBs for the interrupt and control
308  * endpoint.
309  */
310 void st5481_release_usb(struct st5481_adapter *adapter)
311 {
312         struct st5481_intr *intr = &adapter->intr;
313         struct st5481_ctrl *ctrl = &adapter->ctrl;
314
315         DBG(1,"");
316
317         // Stop and free Control and Interrupt URBs
318         usb_unlink_urb(ctrl->urb);
319         if (ctrl->urb->transfer_buffer)
320                 kfree(ctrl->urb->transfer_buffer);
321         usb_free_urb(ctrl->urb);
322
323         usb_unlink_urb(intr->urb);
324         if (intr->urb->transfer_buffer)
325                 kfree(intr->urb->transfer_buffer);
326         usb_free_urb(intr->urb);
327 }
328
329 /*
330  *  Initialize the adapter.
331  */
332 void __devinit st5481_start(struct st5481_adapter *adapter)
333 {
334         static const u8 init_cmd_table[]={
335                 SET_DEFAULT,0,
336                 STT,0,
337                 SDA_MIN,0x0d,
338                 SDA_MAX,0x29,
339                 SDELAY_VALUE,0x14,
340                 GPIO_DIR,0x01,          
341                 GPIO_OUT,RED_LED,
342 //              FFCTRL_OUT_D,4,
343 //              FFCTRH_OUT_D,12,
344                 FFCTRL_OUT_B1,6,
345                 FFCTRH_OUT_B1,20,
346                 FFCTRL_OUT_B2,6,
347                 FFCTRH_OUT_B2,20,
348                 MPMSK,RXCI_INT+DEN_INT+DCOLL_INT,
349                 0
350         };      
351         struct st5481_intr *intr = &adapter->intr;
352         int i = 0;
353         u8 request,value;
354
355         DBG(8,"");
356
357         adapter->leds = RED_LED; 
358
359         // Start receiving on the interrupt endpoint
360         SUBMIT_URB(intr->urb); 
361
362         while ((request = init_cmd_table[i++])) {
363                 value = init_cmd_table[i++];
364                 st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
365         }
366         st5481_ph_command(adapter, ST5481_CMD_PUP);
367 }
368
369 /*
370  * Reset the adapter to default values.
371  */
372 void __devexit st5481_stop(struct st5481_adapter *adapter)
373 {
374         DBG(8,"");
375
376         st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
377 }
378
379 /* ======================================================================
380  * isochronous USB  helpers
381  */
382
383 static void __devinit
384 fill_isoc_urb(struct urb *urb, struct usb_device *dev,
385               unsigned int pipe, void *buf, int num_packets, 
386               int packet_size, usb_complete_t complete,
387               void *context) 
388 {
389         int k;
390
391         spin_lock_init(&urb->lock);
392         urb->dev=dev;
393         urb->pipe=pipe;
394         urb->transfer_buffer=buf;
395         urb->number_of_packets = num_packets;
396         urb->transfer_buffer_length=num_packets*packet_size;
397         urb->actual_length = 0;
398         urb->complete=complete;
399         urb->context=context;
400         urb->transfer_flags=USB_ISO_ASAP;
401         for (k = 0; k < num_packets; k++) {
402                 urb->iso_frame_desc[k].offset = packet_size * k;
403                 urb->iso_frame_desc[k].length = packet_size;
404                 urb->iso_frame_desc[k].actual_length = 0;
405         }
406 }
407
408 int __devinit
409 st5481_setup_isocpipes(struct urb* urb[2], struct usb_device *dev, 
410                            unsigned int pipe, int num_packets,
411                            int packet_size, int buf_size,
412                            usb_complete_t complete, void *context)
413 {
414         int j, retval;
415         unsigned char *buf;
416
417         for (j = 0; j < 2; j++) {
418                 retval = -ENOMEM;
419                 urb[j] = usb_alloc_urb(num_packets);
420                 if (!urb[j])
421                         goto err;
422
423                 // Allocate memory for 2000bytes/sec (16Kb/s)
424                 buf = kmalloc(buf_size, GFP_KERNEL);
425                 if (!buf)
426                         goto err;
427                         
428                 // Fill the isochronous URB
429                 fill_isoc_urb(urb[j], dev, pipe, buf, 
430                               num_packets, packet_size, complete,
431                               context);
432         }
433         return 0;
434
435  err:
436         for (j = 0; j < 2; j++) {
437                 if (urb[j]) {
438                         if (urb[j]->transfer_buffer)
439                                 kfree(urb[j]->transfer_buffer);
440                         usb_free_urb(urb[j]);
441                 }
442         }
443         return retval;
444 }
445
446 void st5481_release_isocpipes(struct urb* urb[2])
447 {
448         int j;
449
450         for (j = 0; j < 2; j++) {
451                 usb_unlink_urb(urb[j]);
452                 if (urb[j]->transfer_buffer)
453                         kfree(urb[j]->transfer_buffer);                 
454                 usb_free_urb(urb[j]);
455         }
456 }
457
458 /*
459  * Decode frames received on the B/D channel.
460  * Note that this function will be called continously
461  * with 64Kbit/s / 16Kbit/s of data and hence it will be 
462  * called 50 times per second with 20 ISOC descriptors. 
463  * Called at interrupt.
464  */
465 static void usb_in_complete(struct urb *urb)
466 {
467         struct st5481_in *in = urb->context;
468         unsigned char *ptr;
469         struct sk_buff *skb;
470         int len, count, status;
471
472         if (urb->status < 0) {
473                 if (urb->status != -ENOENT) {
474                         WARN("urb status %d",urb->status);
475                 } else {
476                         DBG(1,"urb killed");
477                         return; // Give up
478                 }
479         }
480
481         DBG_ISO_PACKET(0x80,urb);
482
483         len = st5481_isoc_flatten(urb);
484         ptr = urb->transfer_buffer;
485         while (len > 0) {
486                 if (in->mode == L1_MODE_TRANS) {
487                         memcpy(in->rcvbuf, ptr, len);
488                         status = len;
489                         len = 0;
490                 } else {
491                         status = hdlc_decode(&in->hdlc_state, ptr, len, &count,
492                                              in->rcvbuf, in->bufsize);
493                         ptr += count;
494                         len -= count;
495                 }
496                 
497                 if (status > 0) {
498                         // Good frame received
499                         DBG(4,"count=%d",status);
500                         DBG_PACKET(0x400, in->rcvbuf, status);
501                         if (!(skb = dev_alloc_skb(status))) {
502                                 WARN("receive out of memory\n");
503                                 break;
504                         }
505                         memcpy(skb_put(skb, status), in->rcvbuf, status);
506                         in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb);
507                 } else if (status == -HDLC_CRC_ERROR) {
508                         INFO("CRC error");
509                 } else if (status == -HDLC_FRAMING_ERROR) {
510                         INFO("framing error");
511                 } else if (status == -HDLC_LENGTH_ERROR) {
512                         INFO("length error");
513                 }
514         }
515
516         // Prepare URB for next transfer
517         urb->dev = in->adapter->usb_dev;
518         urb->actual_length = 0;
519
520         SUBMIT_URB(urb);
521 }
522
523 int __devinit st5481_setup_in(struct st5481_in *in)
524 {
525         struct usb_device *dev = in->adapter->usb_dev;
526         int retval;
527
528         DBG(4,"");
529
530         in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
531         retval = -ENOMEM;
532         if (!in->rcvbuf)
533                 goto err;
534
535         retval = st5481_setup_isocpipes(in->urb, dev, 
536                                         usb_rcvisocpipe(dev, in->ep),
537                                         in->num_packets,  in->packet_size,
538                                         in->num_packets * in->packet_size,
539                                         usb_in_complete, in);
540         if (retval)
541                 goto err_free;
542         return 0;
543
544  err_free:
545         kfree(in->rcvbuf);
546  err:
547         return retval;
548 }
549
550 void st5481_release_in(struct st5481_in *in)
551 {
552         DBG(2,"");
553
554         st5481_release_isocpipes(in->urb);
555 }
556
557 /*
558  * Make the transfer_buffer contiguous by
559  * copying from the iso descriptors if necessary. 
560  */
561 int st5481_isoc_flatten(struct urb *urb)
562 {
563         piso_packet_descriptor_t pipd,pend;
564         unsigned char *src,*dst;
565         unsigned int len;
566         
567         if (urb->status < 0) {
568                 return urb->status;
569         }
570         for (pipd = &urb->iso_frame_desc[0],
571                      pend = &urb->iso_frame_desc[urb->number_of_packets],
572                      dst = urb->transfer_buffer; 
573              pipd < pend; 
574              pipd++) {
575                 
576                 if (pipd->status < 0) {
577                         return (pipd->status);
578                 }
579         
580                 len = pipd->actual_length;
581                 pipd->actual_length = 0;
582                 src = urb->transfer_buffer+pipd->offset;
583
584                 if (src != dst) {
585                         // Need to copy since isoc buffers not full
586                         while (len--) {
587                                 *dst++ = *src++;
588                         }                       
589                 } else {
590                         // No need to copy, just update destination buffer
591                         dst += len;
592                 }
593         }
594         // Return size of flattened buffer
595         return (dst - (unsigned char *)urb->transfer_buffer);
596 }
597
598 static void st5481_start_rcv(void *context)
599 {
600         struct st5481_in *in = context;
601         struct st5481_adapter *adapter = in->adapter;
602
603         DBG(4,"");
604
605         in->urb[0]->dev = adapter->usb_dev;
606         SUBMIT_URB(in->urb[0]);
607
608         in->urb[1]->dev = adapter->usb_dev;
609         SUBMIT_URB(in->urb[1]);
610 }
611
612 void st5481_in_mode(struct st5481_in *in, int mode)
613 {
614         if (in->mode == mode)
615                 return;
616
617         in->mode = mode;
618
619         usb_unlink_urb(in->urb[0]);
620         usb_unlink_urb(in->urb[1]);
621
622         if (in->mode != L1_MODE_NULL) {
623                 if (in->mode != L1_MODE_TRANS)
624                         hdlc_rcv_init(&in->hdlc_state,
625                                       in->mode == L1_MODE_HDLC_56K);
626                 
627                 st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
628                 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
629                                            in->packet_size,
630                                            NULL, NULL);
631                 st5481_start_rcv(in);
632         } else {
633                 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
634                                            0, NULL, NULL);
635         }
636 }
637