patch-2_6_7-vs1_9_1_12
[linux-2.6.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.bRequest,
45             ((struct ctrl_msg *)urb->setup_packet)->dr.wValue,
46             ((struct ctrl_msg *)urb->setup_packet)->dr.wIndex);
47
48         // Prepare the URB
49         urb->dev = adapter->usb_dev;
50
51         SUBMIT_URB(urb, GFP_ATOMIC);
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.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;
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, struct pt_regs *regs)
127 {
128         struct st5481_adapter *adapter = urb->context;
129         struct st5481_ctrl *ctrl = &adapter->ctrl;
130         struct ctrl_msg *ctrl_msg;
131         
132         if (unlikely(urb->status < 0)) {
133                 if (urb->status != -ENOENT && urb->status != -ESHUTDOWN) {
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.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);
149
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,
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, struct pt_regs *regs)
180 {
181         u8 *data = urb->transfer_buffer;
182         u8 irqbyte;
183         struct st5481_adapter *adapter = urb->context;
184         int j;
185         int status;
186
187         switch (urb->status) {
188         case 0:
189                 /* success */
190                 break;
191         case -ECONNRESET:
192         case -ENOENT:
193         case -ESHUTDOWN:
194                 /* this urb is terminated, clean up */
195                 DBG(1, "urb shutting down with status: %d", urb->status);
196                 return;
197         default:
198                 WARN("nonzero urb status received: %d", urb->status);
199                 goto exit;
200         }
201
202         
203         DBG_PACKET(1, data, INT_PKT_SIZE);
204                 
205         if (urb->actual_length == 0) {
206                 goto exit;
207         }
208
209         irqbyte = data[MPINT];
210         if (irqbyte & DEN_INT)
211                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
212
213         if (irqbyte & DCOLL_INT)
214                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
215
216         irqbyte = data[FFINT_D];
217         if (irqbyte & OUT_UNDERRUN)
218                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
219
220         if (irqbyte & OUT_DOWN)
221 ;//             printk("OUT_DOWN\n");
222
223         irqbyte = data[MPINT];
224         if (irqbyte & RXCI_INT)
225                 FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
226
227         for (j = 0; j < 2; j++)
228                 adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
229
230         urb->actual_length = 0;
231
232 exit:
233         status = usb_submit_urb (urb, GFP_ATOMIC);
234         if (status)
235                 WARN("usb_submit_urb failed with result %d", status);
236 }
237
238 /* ======================================================================
239  * initialization
240  */
241
242 int st5481_setup_usb(struct st5481_adapter *adapter)
243 {
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_interface *intf;
248         struct usb_host_interface *altsetting = NULL;
249         struct usb_host_endpoint *endpoint;
250         int status;
251         struct urb *urb;
252         u8 *buf;
253         
254         DBG(1,"");
255         
256         if ((status = usb_reset_configuration (dev)) < 0) {
257                 WARN("reset_configuration failed,status=%d",status);
258                 return status;
259         }
260
261         intf = usb_ifnum_to_if(dev, 0);
262         if (intf)
263                 altsetting = usb_altnum_to_altsetting(intf, 3);
264         if (!altsetting)
265                 return -ENXIO;
266
267         // Check if the config is sane
268         if ( altsetting->desc.bNumEndpoints != 7 ) {
269                 WARN("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
270                 return -EINVAL;
271         }
272
273         // The descriptor is wrong for some early samples of the ST5481 chip
274         altsetting->endpoint[3].desc.wMaxPacketSize = 32;
275         altsetting->endpoint[4].desc.wMaxPacketSize = 32;
276
277         // Use alternative setting 3 on interface 0 to have 2B+D
278         if ((status = usb_set_interface (dev, 0, 3)) < 0) {
279                 WARN("usb_set_interface failed,status=%d",status);
280                 return status;
281         }
282
283         // Allocate URB for control endpoint
284         urb = usb_alloc_urb(0, GFP_KERNEL);
285         if (!urb) {
286                 return -ENOMEM;
287         }
288         ctrl->urb = urb;
289         
290         // Fill the control URB
291         usb_fill_control_urb (urb, dev, 
292                           usb_sndctrlpipe(dev, 0),
293                           NULL, NULL, 0, usb_ctrl_complete, adapter);
294
295                 
296         fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
297
298         // Allocate URBs and buffers for interrupt endpoint
299         urb = usb_alloc_urb(0, GFP_KERNEL);
300         if (!urb) { 
301                 return -ENOMEM;
302         }
303         intr->urb = urb;
304         
305         buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
306         if (!buf) {
307                 return -ENOMEM;
308         }
309
310         endpoint = &altsetting->endpoint[EP_INT-1];
311                                 
312         // Fill the interrupt URB
313         usb_fill_int_urb(urb, dev,
314                      usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress),
315                      buf, INT_PKT_SIZE,
316                      usb_int_complete, adapter,
317                      endpoint->desc.bInterval);
318                 
319         return 0;
320 }
321
322 /*
323  * Release buffers and URBs for the interrupt and control
324  * endpoint.
325  */
326 void st5481_release_usb(struct st5481_adapter *adapter)
327 {
328         struct st5481_intr *intr = &adapter->intr;
329         struct st5481_ctrl *ctrl = &adapter->ctrl;
330
331         DBG(1,"");
332
333         // Stop and free Control and Interrupt URBs
334         usb_unlink_urb(ctrl->urb);
335         if (ctrl->urb->transfer_buffer)
336                 kfree(ctrl->urb->transfer_buffer);
337         usb_free_urb(ctrl->urb);
338
339         usb_unlink_urb(intr->urb);
340         if (intr->urb->transfer_buffer)
341                 kfree(intr->urb->transfer_buffer);
342         usb_free_urb(intr->urb);
343 }
344
345 /*
346  *  Initialize the adapter.
347  */
348 void st5481_start(struct st5481_adapter *adapter)
349 {
350         static const u8 init_cmd_table[]={
351                 SET_DEFAULT,0,
352                 STT,0,
353                 SDA_MIN,0x0d,
354                 SDA_MAX,0x29,
355                 SDELAY_VALUE,0x14,
356                 GPIO_DIR,0x01,          
357                 GPIO_OUT,RED_LED,
358 //              FFCTRL_OUT_D,4,
359 //              FFCTRH_OUT_D,12,
360                 FFCTRL_OUT_B1,6,
361                 FFCTRH_OUT_B1,20,
362                 FFCTRL_OUT_B2,6,
363                 FFCTRH_OUT_B2,20,
364                 MPMSK,RXCI_INT+DEN_INT+DCOLL_INT,
365                 0
366         };      
367         struct st5481_intr *intr = &adapter->intr;
368         int i = 0;
369         u8 request,value;
370
371         DBG(8,"");
372
373         adapter->leds = RED_LED; 
374
375         // Start receiving on the interrupt endpoint
376         SUBMIT_URB(intr->urb, GFP_KERNEL); 
377
378         while ((request = init_cmd_table[i++])) {
379                 value = init_cmd_table[i++];
380                 st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
381         }
382         st5481_ph_command(adapter, ST5481_CMD_PUP);
383 }
384
385 /*
386  * Reset the adapter to default values.
387  */
388 void st5481_stop(struct st5481_adapter *adapter)
389 {
390         DBG(8,"");
391
392         st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
393 }
394
395 /* ======================================================================
396  * isochronous USB  helpers
397  */
398
399 static void
400 fill_isoc_urb(struct urb *urb, struct usb_device *dev,
401               unsigned int pipe, void *buf, int num_packets, 
402               int packet_size, usb_complete_t complete,
403               void *context) 
404 {
405         int k;
406
407         spin_lock_init(&urb->lock);
408         urb->dev=dev;
409         urb->pipe=pipe;
410         urb->transfer_buffer=buf;
411         urb->number_of_packets = num_packets;
412         urb->transfer_buffer_length=num_packets*packet_size;
413         urb->actual_length = 0;
414         urb->complete=complete;
415         urb->context=context;
416         urb->transfer_flags=URB_ISO_ASAP;
417         for (k = 0; k < num_packets; k++) {
418                 urb->iso_frame_desc[k].offset = packet_size * k;
419                 urb->iso_frame_desc[k].length = packet_size;
420                 urb->iso_frame_desc[k].actual_length = 0;
421         }
422 }
423
424 int
425 st5481_setup_isocpipes(struct urb* urb[2], struct usb_device *dev, 
426                            unsigned int pipe, int num_packets,
427                            int packet_size, int buf_size,
428                            usb_complete_t complete, void *context)
429 {
430         int j, retval;
431         unsigned char *buf;
432
433         for (j = 0; j < 2; j++) {
434                 retval = -ENOMEM;
435                 urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL);
436                 if (!urb[j])
437                         goto err;
438
439                 // Allocate memory for 2000bytes/sec (16Kb/s)
440                 buf = kmalloc(buf_size, GFP_KERNEL);
441                 if (!buf)
442                         goto err;
443                         
444                 // Fill the isochronous URB
445                 fill_isoc_urb(urb[j], dev, pipe, buf, 
446                               num_packets, packet_size, complete,
447                               context);
448         }
449         return 0;
450
451  err:
452         for (j = 0; j < 2; j++) {
453                 if (urb[j]) {
454                         if (urb[j]->transfer_buffer)
455                                 kfree(urb[j]->transfer_buffer);
456                         usb_free_urb(urb[j]);
457                 }
458         }
459         return retval;
460 }
461
462 void st5481_release_isocpipes(struct urb* urb[2])
463 {
464         int j;
465
466         for (j = 0; j < 2; j++) {
467                 usb_unlink_urb(urb[j]);
468                 if (urb[j]->transfer_buffer)
469                         kfree(urb[j]->transfer_buffer);                 
470                 usb_free_urb(urb[j]);
471         }
472 }
473
474 /*
475  * Decode frames received on the B/D channel.
476  * Note that this function will be called continously
477  * with 64Kbit/s / 16Kbit/s of data and hence it will be 
478  * called 50 times per second with 20 ISOC descriptors. 
479  * Called at interrupt.
480  */
481 static void usb_in_complete(struct urb *urb, struct pt_regs *regs)
482 {
483         struct st5481_in *in = urb->context;
484         unsigned char *ptr;
485         struct sk_buff *skb;
486         int len, count, status;
487
488         if (unlikely(urb->status < 0)) {
489                 if (urb->status != -ENOENT && urb->status != -ESHUTDOWN) {
490                         WARN("urb status %d",urb->status);
491                 } else {
492                         DBG(1,"urb killed");
493                         return; // Give up
494                 }
495         }
496
497         DBG_ISO_PACKET(0x80,urb);
498
499         len = st5481_isoc_flatten(urb);
500         ptr = urb->transfer_buffer;
501         while (len > 0) {
502                 if (in->mode == L1_MODE_TRANS) {
503                         memcpy(in->rcvbuf, ptr, len);
504                         status = len;
505                         len = 0;
506                 } else {
507                         status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
508                                 in->rcvbuf, in->bufsize);
509                         ptr += count;
510                         len -= count;
511                 }
512                 
513                 if (status > 0) {
514                         // Good frame received
515                         DBG(4,"count=%d",status);
516                         DBG_PACKET(0x400, in->rcvbuf, status);
517                         if (!(skb = dev_alloc_skb(status))) {
518                                 WARN("receive out of memory\n");
519                                 break;
520                         }
521                         memcpy(skb_put(skb, status), in->rcvbuf, status);
522                         in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb);
523                 } else if (status == -HDLC_CRC_ERROR) {
524                         INFO("CRC error");
525                 } else if (status == -HDLC_FRAMING_ERROR) {
526                         INFO("framing error");
527                 } else if (status == -HDLC_LENGTH_ERROR) {
528                         INFO("length error");
529                 }
530         }
531
532         // Prepare URB for next transfer
533         urb->dev = in->adapter->usb_dev;
534         urb->actual_length = 0;
535
536         SUBMIT_URB(urb, GFP_ATOMIC);
537 }
538
539 int st5481_setup_in(struct st5481_in *in)
540 {
541         struct usb_device *dev = in->adapter->usb_dev;
542         int retval;
543
544         DBG(4,"");
545
546         in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
547         retval = -ENOMEM;
548         if (!in->rcvbuf)
549                 goto err;
550
551         retval = st5481_setup_isocpipes(in->urb, dev, 
552                                         usb_rcvisocpipe(dev, in->ep),
553                                         in->num_packets,  in->packet_size,
554                                         in->num_packets * in->packet_size,
555                                         usb_in_complete, in);
556         if (retval)
557                 goto err_free;
558         return 0;
559
560  err_free:
561         kfree(in->rcvbuf);
562  err:
563         return retval;
564 }
565
566 void st5481_release_in(struct st5481_in *in)
567 {
568         DBG(2,"");
569
570         st5481_release_isocpipes(in->urb);
571 }
572
573 /*
574  * Make the transfer_buffer contiguous by
575  * copying from the iso descriptors if necessary. 
576  */
577 int st5481_isoc_flatten(struct urb *urb)
578 {
579         struct usb_iso_packet_descriptor *pipd,*pend;
580         unsigned char *src,*dst;
581         unsigned int len;
582         
583         if (urb->status < 0) {
584                 return urb->status;
585         }
586         for (pipd = &urb->iso_frame_desc[0],
587                      pend = &urb->iso_frame_desc[urb->number_of_packets],
588                      dst = urb->transfer_buffer; 
589              pipd < pend; 
590              pipd++) {
591                 
592                 if (pipd->status < 0) {
593                         return (pipd->status);
594                 }
595         
596                 len = pipd->actual_length;
597                 pipd->actual_length = 0;
598                 src = urb->transfer_buffer+pipd->offset;
599
600                 if (src != dst) {
601                         // Need to copy since isoc buffers not full
602                         while (len--) {
603                                 *dst++ = *src++;
604                         }                       
605                 } else {
606                         // No need to copy, just update destination buffer
607                         dst += len;
608                 }
609         }
610         // Return size of flattened buffer
611         return (dst - (unsigned char *)urb->transfer_buffer);
612 }
613
614 static void st5481_start_rcv(void *context)
615 {
616         struct st5481_in *in = context;
617         struct st5481_adapter *adapter = in->adapter;
618
619         DBG(4,"");
620
621         in->urb[0]->dev = adapter->usb_dev;
622         SUBMIT_URB(in->urb[0], GFP_KERNEL);
623
624         in->urb[1]->dev = adapter->usb_dev;
625         SUBMIT_URB(in->urb[1], GFP_KERNEL);
626 }
627
628 void st5481_in_mode(struct st5481_in *in, int mode)
629 {
630         if (in->mode == mode)
631                 return;
632
633         in->mode = mode;
634
635         usb_unlink_urb(in->urb[0]);
636         usb_unlink_urb(in->urb[1]);
637
638         if (in->mode != L1_MODE_NULL) {
639                 if (in->mode != L1_MODE_TRANS)
640                         isdnhdlc_rcv_init(&in->hdlc_state,
641                                 in->mode == L1_MODE_HDLC_56K);
642                 
643                 st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
644                 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
645                                            in->packet_size,
646                                            NULL, NULL);
647                 st5481_start_rcv(in);
648         } else {
649                 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
650                                            0, NULL, NULL);
651         }
652 }
653