Merge 'drm-fixes' branch of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/airlied...
[linux-2.6] / 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 static int st5481_isoc_flatten(struct urb *urb);
19
20 /* ======================================================================
21  * control pipe
22  */
23
24 /*
25  * Send the next endpoint 0 request stored in the FIFO.
26  * Called either by the completion or by usb_ctrl_msg.
27  */
28 static void usb_next_ctrl_msg(struct urb *urb,
29                               struct st5481_adapter *adapter)
30 {
31         struct st5481_ctrl *ctrl = &adapter->ctrl;
32         int r_index;
33
34         if (test_and_set_bit(0, &ctrl->busy)) {
35                 return;
36         }
37
38         if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) {
39                 test_and_clear_bit(0,&ctrl->busy);
40                 return;
41         } 
42         urb->setup_packet = 
43                 (unsigned char *)&ctrl->msg_fifo.data[r_index];
44         
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);
49
50         // Prepare the URB
51         urb->dev = adapter->usb_dev;
52
53         SUBMIT_URB(urb, GFP_ATOMIC);
54 }
55
56 /*
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.
59  */
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)
63 {
64         struct st5481_ctrl *ctrl = &adapter->ctrl;
65         int w_index;
66         struct ctrl_msg *ctrl_msg;
67         
68         if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
69                 WARN("control msg FIFO full");
70                 return;
71         }
72         ctrl_msg = &ctrl->msg_fifo.data[w_index]; 
73    
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;
81
82         usb_next_ctrl_msg(ctrl->urb, adapter);
83 }
84
85 /*
86  * Asynchronous endpoint 0 device request.
87  */
88 void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
89                          u8 request, u16 value,
90                          ctrl_complete_t complete, void *context)
91 {
92         usb_ctrl_msg(adapter, request, 
93                      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 
94                      value, 0, complete, context);
95 }
96
97 /*
98  * Asynchronous pipe reset (async version of usb_clear_halt).
99  */
100 void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
101                     u_char pipe,
102                     ctrl_complete_t complete, void *context)
103 {
104         DBG(1,"pipe=%02x",pipe);
105
106         usb_ctrl_msg(adapter,
107                      USB_REQ_CLEAR_FEATURE, USB_DIR_OUT | USB_RECIP_ENDPOINT,
108                      0, pipe, complete, context);
109 }
110
111
112 /*
113   Physical level functions
114 */
115
116 void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command)
117 {
118         DBG(8,"command=%s", ST5481_CMD_string(command));
119
120         st5481_usb_device_ctrl_msg(adapter, TXCI, command, NULL, NULL);
121 }
122
123 /*
124  * The request on endpoint 0 has completed.
125  * Call the user provided completion routine and try
126  * to send the next request.
127  */
128 static void usb_ctrl_complete(struct urb *urb, struct pt_regs *regs)
129 {
130         struct st5481_adapter *adapter = urb->context;
131         struct st5481_ctrl *ctrl = &adapter->ctrl;
132         struct ctrl_msg *ctrl_msg;
133         
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
140                 }
141         }
142
143         ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
144         
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);
148
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,
153                               0);
154
155
156         }
157         
158         if (ctrl_msg->complete)
159                 ctrl_msg->complete(ctrl_msg->context);
160
161         clear_bit(0, &ctrl->busy);
162         
163         // Try to send next control message
164         usb_next_ctrl_msg(urb, adapter);
165         return;
166 }
167
168 /* ======================================================================
169  * interrupt pipe
170  */
171
172 /*
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.
177  */
178 static void usb_int_complete(struct urb *urb, struct pt_regs *regs)
179 {
180         u8 *data = urb->transfer_buffer;
181         u8 irqbyte;
182         struct st5481_adapter *adapter = urb->context;
183         int j;
184         int status;
185
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;
199         }
200
201         
202         DBG_PACKET(1, data, INT_PKT_SIZE);
203                 
204         if (urb->actual_length == 0) {
205                 goto exit;
206         }
207
208         irqbyte = data[MPINT];
209         if (irqbyte & DEN_INT)
210                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
211
212         if (irqbyte & DCOLL_INT)
213                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
214
215         irqbyte = data[FFINT_D];
216         if (irqbyte & OUT_UNDERRUN)
217                 FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
218
219         if (irqbyte & OUT_DOWN)
220 ;//             printk("OUT_DOWN\n");
221
222         irqbyte = data[MPINT];
223         if (irqbyte & RXCI_INT)
224                 FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
225
226         for (j = 0; j < 2; j++)
227                 adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
228
229         urb->actual_length = 0;
230
231 exit:
232         status = usb_submit_urb (urb, GFP_ATOMIC);
233         if (status)
234                 WARN("usb_submit_urb failed with result %d", status);
235 }
236
237 /* ======================================================================
238  * initialization
239  */
240
241 int st5481_setup_usb(struct st5481_adapter *adapter)
242 {
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;
252         
253         DBG(1,"");
254         
255         if ((status = usb_reset_configuration (dev)) < 0) {
256                 WARN("reset_configuration failed,status=%d",status);
257                 return status;
258         }
259
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;
265
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;
270         }
271
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);
275
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;
280         }
281
282         // Allocate URB for control endpoint
283         urb = usb_alloc_urb(0, GFP_KERNEL);
284         if (!urb) {
285                 return -ENOMEM;
286         }
287         ctrl->urb = urb;
288         
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);
293
294                 
295         fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
296
297         // Allocate URBs and buffers for interrupt endpoint
298         urb = usb_alloc_urb(0, GFP_KERNEL);
299         if (!urb) { 
300                 return -ENOMEM;
301         }
302         intr->urb = urb;
303         
304         buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
305         if (!buf) {
306                 return -ENOMEM;
307         }
308
309         endpoint = &altsetting->endpoint[EP_INT-1];
310                                 
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);
317                 
318         return 0;
319 }
320
321 /*
322  * Release buffers and URBs for the interrupt and control
323  * endpoint.
324  */
325 void st5481_release_usb(struct st5481_adapter *adapter)
326 {
327         struct st5481_intr *intr = &adapter->intr;
328         struct st5481_ctrl *ctrl = &adapter->ctrl;
329
330         DBG(1,"");
331
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);
337
338         usb_unlink_urb(intr->urb);
339         if (intr->urb->transfer_buffer)
340                 kfree(intr->urb->transfer_buffer);
341         usb_free_urb(intr->urb);
342 }
343
344 /*
345  *  Initialize the adapter.
346  */
347 void st5481_start(struct st5481_adapter *adapter)
348 {
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,
364                 0
365         };      
366         struct st5481_intr *intr = &adapter->intr;
367         int i = 0;
368         u8 request,value;
369
370         DBG(8,"");
371
372         adapter->leds = RED_LED; 
373
374         // Start receiving on the interrupt endpoint
375         SUBMIT_URB(intr->urb, GFP_KERNEL); 
376
377         while ((request = init_cmd_table[i++])) {
378                 value = init_cmd_table[i++];
379                 st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
380         }
381         st5481_ph_command(adapter, ST5481_CMD_PUP);
382 }
383
384 /*
385  * Reset the adapter to default values.
386  */
387 void st5481_stop(struct st5481_adapter *adapter)
388 {
389         DBG(8,"");
390
391         st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
392 }
393
394 /* ======================================================================
395  * isochronous USB  helpers
396  */
397
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) 
403 {
404         int k;
405
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;
420         }
421 }
422
423 int
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)
428 {
429         int j, retval;
430         unsigned char *buf;
431
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;
437
438                 // Allocate memory for 2000bytes/sec (16Kb/s)
439                 buf = kmalloc(buf_size, GFP_KERNEL);
440                 if (!buf)
441                         goto err;
442                         
443                 // Fill the isochronous URB
444                 fill_isoc_urb(urb[j], dev, pipe, buf, 
445                               num_packets, packet_size, complete,
446                               context);
447         }
448         return 0;
449
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]);
456                 }
457         }
458         return retval;
459 }
460
461 void st5481_release_isocpipes(struct urb* urb[2])
462 {
463         int j;
464
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]);
470         }
471 }
472
473 /*
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.
479  */
480 static void usb_in_complete(struct urb *urb, struct pt_regs *regs)
481 {
482         struct st5481_in *in = urb->context;
483         unsigned char *ptr;
484         struct sk_buff *skb;
485         int len, count, status;
486
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
493                 }
494         }
495
496         DBG_ISO_PACKET(0x80,urb);
497
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;
510                 }
511                 
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;
519                         }
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");
528                 }
529         }
530
531         // Prepare URB for next transfer
532         urb->dev = in->adapter->usb_dev;
533         urb->actual_length = 0;
534
535         SUBMIT_URB(urb, GFP_ATOMIC);
536 }
537
538 int st5481_setup_in(struct st5481_in *in)
539 {
540         struct usb_device *dev = in->adapter->usb_dev;
541         int retval;
542
543         DBG(4,"");
544
545         in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
546         retval = -ENOMEM;
547         if (!in->rcvbuf)
548                 goto err;
549
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;
558
559  err_free:
560         kfree(in->rcvbuf);
561  err:
562         return retval;
563 }
564
565 void st5481_release_in(struct st5481_in *in)
566 {
567         DBG(2,"");
568
569         st5481_release_isocpipes(in->urb);
570 }
571
572 /*
573  * Make the transfer_buffer contiguous by
574  * copying from the iso descriptors if necessary. 
575  */
576 static int st5481_isoc_flatten(struct urb *urb)
577 {
578         struct usb_iso_packet_descriptor *pipd,*pend;
579         unsigned char *src,*dst;
580         unsigned int len;
581         
582         if (urb->status < 0) {
583                 return urb->status;
584         }
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++) {
590                 
591                 if (pipd->status < 0) {
592                         return (pipd->status);
593                 }
594         
595                 len = pipd->actual_length;
596                 pipd->actual_length = 0;
597                 src = urb->transfer_buffer+pipd->offset;
598
599                 if (src != dst) {
600                         // Need to copy since isoc buffers not full
601                         while (len--) {
602                                 *dst++ = *src++;
603                         }                       
604                 } else {
605                         // No need to copy, just update destination buffer
606                         dst += len;
607                 }
608         }
609         // Return size of flattened buffer
610         return (dst - (unsigned char *)urb->transfer_buffer);
611 }
612
613 static void st5481_start_rcv(void *context)
614 {
615         struct st5481_in *in = context;
616         struct st5481_adapter *adapter = in->adapter;
617
618         DBG(4,"");
619
620         in->urb[0]->dev = adapter->usb_dev;
621         SUBMIT_URB(in->urb[0], GFP_KERNEL);
622
623         in->urb[1]->dev = adapter->usb_dev;
624         SUBMIT_URB(in->urb[1], GFP_KERNEL);
625 }
626
627 void st5481_in_mode(struct st5481_in *in, int mode)
628 {
629         if (in->mode == mode)
630                 return;
631
632         in->mode = mode;
633
634         usb_unlink_urb(in->urb[0]);
635         usb_unlink_urb(in->urb[1]);
636
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);
641                 
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);
650         }
651 }
652