2 * Driver for ST5481 USB ISDN modem
5 * Copyright 2001 by Frode Isaksen <fisaksen@bewan.com>
6 * 2001 by Kai Germaschewski <kai.germaschewski@gmx.de>
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>
18 static int st5481_isoc_flatten(struct urb
*urb
);
20 /* ======================================================================
25 * Send the next endpoint 0 request stored in the FIFO.
26 * Called either by the completion or by usb_ctrl_msg.
28 static void usb_next_ctrl_msg(struct urb
*urb
,
29 struct st5481_adapter
*adapter
)
31 struct st5481_ctrl
*ctrl
= &adapter
->ctrl
;
34 if (test_and_set_bit(0, &ctrl
->busy
)) {
38 if ((r_index
= fifo_remove(&ctrl
->msg_fifo
.f
)) < 0) {
39 test_and_clear_bit(0,&ctrl
->busy
);
43 (unsigned char *)&ctrl
->msg_fifo
.data
[r_index
];
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
);
51 urb
->dev
= adapter
->usb_dev
;
53 SUBMIT_URB(urb
, GFP_ATOMIC
);
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.
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
)
64 struct st5481_ctrl
*ctrl
= &adapter
->ctrl
;
66 struct ctrl_msg
*ctrl_msg
;
68 if ((w_index
= fifo_add(&ctrl
->msg_fifo
.f
)) < 0) {
69 WARNING("control msg FIFO full");
72 ctrl_msg
= &ctrl
->msg_fifo
.data
[w_index
];
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
;
82 usb_next_ctrl_msg(ctrl
->urb
, adapter
);
86 * Asynchronous endpoint 0 device request.
88 void st5481_usb_device_ctrl_msg(struct st5481_adapter
*adapter
,
89 u8 request
, u16 value
,
90 ctrl_complete_t complete
, void *context
)
92 usb_ctrl_msg(adapter
, request
,
93 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
94 value
, 0, complete
, context
);
98 * Asynchronous pipe reset (async version of usb_clear_halt).
100 void st5481_usb_pipe_reset(struct st5481_adapter
*adapter
,
102 ctrl_complete_t complete
, void *context
)
104 DBG(1,"pipe=%02x",pipe
);
106 usb_ctrl_msg(adapter
,
107 USB_REQ_CLEAR_FEATURE
, USB_DIR_OUT
| USB_RECIP_ENDPOINT
,
108 0, pipe
, complete
, context
);
113 Physical level functions
116 void st5481_ph_command(struct st5481_adapter
*adapter
, unsigned int command
)
118 DBG(8,"command=%s", ST5481_CMD_string(command
));
120 st5481_usb_device_ctrl_msg(adapter
, TXCI
, command
, NULL
, NULL
);
124 * The request on endpoint 0 has completed.
125 * Call the user provided completion routine and try
126 * to send the next request.
128 static void usb_ctrl_complete(struct urb
*urb
)
130 struct st5481_adapter
*adapter
= urb
->context
;
131 struct st5481_ctrl
*ctrl
= &adapter
->ctrl
;
132 struct ctrl_msg
*ctrl_msg
;
134 if (unlikely(urb
->status
< 0)) {
135 switch (urb
->status
) {
139 DBG(1,"urb killed status %d", urb
->status
);
142 WARNING("urb status %d",urb
->status
);
147 ctrl_msg
= (struct ctrl_msg
*)urb
->setup_packet
;
149 if (ctrl_msg
->dr
.bRequest
== USB_REQ_CLEAR_FEATURE
) {
150 /* Special case handling for pipe reset */
151 le16_to_cpus(&ctrl_msg
->dr
.wIndex
);
152 usb_reset_endpoint(adapter
->usb_dev
, ctrl_msg
->dr
.wIndex
);
155 if (ctrl_msg
->complete
)
156 ctrl_msg
->complete(ctrl_msg
->context
);
158 clear_bit(0, &ctrl
->busy
);
160 // Try to send next control message
161 usb_next_ctrl_msg(urb
, adapter
);
165 /* ======================================================================
170 * The interrupt endpoint will be called when any
171 * of the 6 registers changes state (depending on masks).
172 * Decode the register values and schedule a private event.
173 * Called at interrupt.
175 static void usb_int_complete(struct urb
*urb
)
177 u8
*data
= urb
->transfer_buffer
;
179 struct st5481_adapter
*adapter
= urb
->context
;
183 switch (urb
->status
) {
190 /* this urb is terminated, clean up */
191 DBG(2, "urb shutting down with status: %d", urb
->status
);
194 WARNING("nonzero urb status received: %d", urb
->status
);
199 DBG_PACKET(2, data
, INT_PKT_SIZE
);
201 if (urb
->actual_length
== 0) {
205 irqbyte
= data
[MPINT
];
206 if (irqbyte
& DEN_INT
)
207 FsmEvent(&adapter
->d_out
.fsm
, EV_DOUT_DEN
, NULL
);
209 if (irqbyte
& DCOLL_INT
)
210 FsmEvent(&adapter
->d_out
.fsm
, EV_DOUT_COLL
, NULL
);
212 irqbyte
= data
[FFINT_D
];
213 if (irqbyte
& OUT_UNDERRUN
)
214 FsmEvent(&adapter
->d_out
.fsm
, EV_DOUT_UNDERRUN
, NULL
);
216 if (irqbyte
& OUT_DOWN
)
217 ;// printk("OUT_DOWN\n");
219 irqbyte
= data
[MPINT
];
220 if (irqbyte
& RXCI_INT
)
221 FsmEvent(&adapter
->l1m
, data
[CCIST
] & 0x0f, NULL
);
223 for (j
= 0; j
< 2; j
++)
224 adapter
->bcs
[j
].b_out
.flow_event
|= data
[FFINT_B1
+ j
];
226 urb
->actual_length
= 0;
229 status
= usb_submit_urb (urb
, GFP_ATOMIC
);
231 WARNING("usb_submit_urb failed with result %d", status
);
234 /* ======================================================================
238 int st5481_setup_usb(struct st5481_adapter
*adapter
)
240 struct usb_device
*dev
= adapter
->usb_dev
;
241 struct st5481_ctrl
*ctrl
= &adapter
->ctrl
;
242 struct st5481_intr
*intr
= &adapter
->intr
;
243 struct usb_interface
*intf
;
244 struct usb_host_interface
*altsetting
= NULL
;
245 struct usb_host_endpoint
*endpoint
;
252 if ((status
= usb_reset_configuration (dev
)) < 0) {
253 WARNING("reset_configuration failed,status=%d",status
);
257 intf
= usb_ifnum_to_if(dev
, 0);
259 altsetting
= usb_altnum_to_altsetting(intf
, 3);
263 // Check if the config is sane
264 if ( altsetting
->desc
.bNumEndpoints
!= 7 ) {
265 WARNING("expecting 7 got %d endpoints!", altsetting
->desc
.bNumEndpoints
);
269 // The descriptor is wrong for some early samples of the ST5481 chip
270 altsetting
->endpoint
[3].desc
.wMaxPacketSize
= __constant_cpu_to_le16(32);
271 altsetting
->endpoint
[4].desc
.wMaxPacketSize
= __constant_cpu_to_le16(32);
273 // Use alternative setting 3 on interface 0 to have 2B+D
274 if ((status
= usb_set_interface (dev
, 0, 3)) < 0) {
275 WARNING("usb_set_interface failed,status=%d",status
);
279 // Allocate URB for control endpoint
280 urb
= usb_alloc_urb(0, GFP_KERNEL
);
286 // Fill the control URB
287 usb_fill_control_urb (urb
, dev
,
288 usb_sndctrlpipe(dev
, 0),
289 NULL
, NULL
, 0, usb_ctrl_complete
, adapter
);
292 fifo_init(&ctrl
->msg_fifo
.f
, ARRAY_SIZE(ctrl
->msg_fifo
.data
));
294 // Allocate URBs and buffers for interrupt endpoint
295 urb
= usb_alloc_urb(0, GFP_KERNEL
);
301 buf
= kmalloc(INT_PKT_SIZE
, GFP_KERNEL
);
306 endpoint
= &altsetting
->endpoint
[EP_INT
-1];
308 // Fill the interrupt URB
309 usb_fill_int_urb(urb
, dev
,
310 usb_rcvintpipe(dev
, endpoint
->desc
.bEndpointAddress
),
312 usb_int_complete
, adapter
,
313 endpoint
->desc
.bInterval
);
319 * Release buffers and URBs for the interrupt and control
322 void st5481_release_usb(struct st5481_adapter
*adapter
)
324 struct st5481_intr
*intr
= &adapter
->intr
;
325 struct st5481_ctrl
*ctrl
= &adapter
->ctrl
;
329 // Stop and free Control and Interrupt URBs
330 usb_kill_urb(ctrl
->urb
);
331 kfree(ctrl
->urb
->transfer_buffer
);
332 usb_free_urb(ctrl
->urb
);
335 usb_kill_urb(intr
->urb
);
336 kfree(intr
->urb
->transfer_buffer
);
337 usb_free_urb(intr
->urb
);
342 * Initialize the adapter.
344 void st5481_start(struct st5481_adapter
*adapter
)
346 static const u8 init_cmd_table
[]={
360 MPMSK
,RXCI_INT
+DEN_INT
+DCOLL_INT
,
363 struct st5481_intr
*intr
= &adapter
->intr
;
369 adapter
->leds
= RED_LED
;
371 // Start receiving on the interrupt endpoint
372 SUBMIT_URB(intr
->urb
, GFP_KERNEL
);
374 while ((request
= init_cmd_table
[i
++])) {
375 value
= init_cmd_table
[i
++];
376 st5481_usb_device_ctrl_msg(adapter
, request
, value
, NULL
, NULL
);
378 st5481_ph_command(adapter
, ST5481_CMD_PUP
);
382 * Reset the adapter to default values.
384 void st5481_stop(struct st5481_adapter
*adapter
)
388 st5481_usb_device_ctrl_msg(adapter
, SET_DEFAULT
, 0, NULL
, NULL
);
391 /* ======================================================================
392 * isochronous USB helpers
396 fill_isoc_urb(struct urb
*urb
, struct usb_device
*dev
,
397 unsigned int pipe
, void *buf
, int num_packets
,
398 int packet_size
, usb_complete_t complete
,
406 urb
->transfer_buffer
=buf
;
407 urb
->number_of_packets
= num_packets
;
408 urb
->transfer_buffer_length
=num_packets
*packet_size
;
409 urb
->actual_length
= 0;
410 urb
->complete
=complete
;
411 urb
->context
=context
;
412 urb
->transfer_flags
=URB_ISO_ASAP
;
413 for (k
= 0; k
< num_packets
; k
++) {
414 urb
->iso_frame_desc
[k
].offset
= packet_size
* k
;
415 urb
->iso_frame_desc
[k
].length
= packet_size
;
416 urb
->iso_frame_desc
[k
].actual_length
= 0;
421 st5481_setup_isocpipes(struct urb
* urb
[2], struct usb_device
*dev
,
422 unsigned int pipe
, int num_packets
,
423 int packet_size
, int buf_size
,
424 usb_complete_t complete
, void *context
)
429 for (j
= 0; j
< 2; j
++) {
431 urb
[j
] = usb_alloc_urb(num_packets
, GFP_KERNEL
);
435 // Allocate memory for 2000bytes/sec (16Kb/s)
436 buf
= kmalloc(buf_size
, GFP_KERNEL
);
440 // Fill the isochronous URB
441 fill_isoc_urb(urb
[j
], dev
, pipe
, buf
,
442 num_packets
, packet_size
, complete
,
448 for (j
= 0; j
< 2; j
++) {
450 kfree(urb
[j
]->transfer_buffer
);
451 urb
[j
]->transfer_buffer
= NULL
;
452 usb_free_urb(urb
[j
]);
459 void st5481_release_isocpipes(struct urb
* urb
[2])
463 for (j
= 0; j
< 2; j
++) {
464 usb_kill_urb(urb
[j
]);
465 kfree(urb
[j
]->transfer_buffer
);
466 usb_free_urb(urb
[j
]);
472 * Decode frames received on the B/D channel.
473 * Note that this function will be called continously
474 * with 64Kbit/s / 16Kbit/s of data and hence it will be
475 * called 50 times per second with 20 ISOC descriptors.
476 * Called at interrupt.
478 static void usb_in_complete(struct urb
*urb
)
480 struct st5481_in
*in
= urb
->context
;
483 int len
, count
, status
;
485 if (unlikely(urb
->status
< 0)) {
486 switch (urb
->status
) {
490 DBG(1,"urb killed status %d", urb
->status
);
493 WARNING("urb status %d",urb
->status
);
498 DBG_ISO_PACKET(0x80,urb
);
500 len
= st5481_isoc_flatten(urb
);
501 ptr
= urb
->transfer_buffer
;
503 if (in
->mode
== L1_MODE_TRANS
) {
504 memcpy(in
->rcvbuf
, ptr
, len
);
508 status
= isdnhdlc_decode(&in
->hdlc_state
, ptr
, len
, &count
,
509 in
->rcvbuf
, in
->bufsize
);
515 // Good frame received
516 DBG(4,"count=%d",status
);
517 DBG_PACKET(0x400, in
->rcvbuf
, status
);
518 if (!(skb
= dev_alloc_skb(status
))) {
519 WARNING("receive out of memory\n");
522 memcpy(skb_put(skb
, status
), in
->rcvbuf
, status
);
523 in
->hisax_if
->l1l2(in
->hisax_if
, PH_DATA
| INDICATION
, skb
);
524 } else if (status
== -HDLC_CRC_ERROR
) {
526 } else if (status
== -HDLC_FRAMING_ERROR
) {
527 INFO("framing error");
528 } else if (status
== -HDLC_LENGTH_ERROR
) {
529 INFO("length error");
533 // Prepare URB for next transfer
534 urb
->dev
= in
->adapter
->usb_dev
;
535 urb
->actual_length
= 0;
537 SUBMIT_URB(urb
, GFP_ATOMIC
);
540 int st5481_setup_in(struct st5481_in
*in
)
542 struct usb_device
*dev
= in
->adapter
->usb_dev
;
547 in
->rcvbuf
= kmalloc(in
->bufsize
, GFP_KERNEL
);
552 retval
= st5481_setup_isocpipes(in
->urb
, dev
,
553 usb_rcvisocpipe(dev
, in
->ep
),
554 in
->num_packets
, in
->packet_size
,
555 in
->num_packets
* in
->packet_size
,
556 usb_in_complete
, in
);
567 void st5481_release_in(struct st5481_in
*in
)
571 st5481_release_isocpipes(in
->urb
);
575 * Make the transfer_buffer contiguous by
576 * copying from the iso descriptors if necessary.
578 static int st5481_isoc_flatten(struct urb
*urb
)
580 struct usb_iso_packet_descriptor
*pipd
,*pend
;
581 unsigned char *src
,*dst
;
584 if (urb
->status
< 0) {
587 for (pipd
= &urb
->iso_frame_desc
[0],
588 pend
= &urb
->iso_frame_desc
[urb
->number_of_packets
],
589 dst
= urb
->transfer_buffer
;
593 if (pipd
->status
< 0) {
594 return (pipd
->status
);
597 len
= pipd
->actual_length
;
598 pipd
->actual_length
= 0;
599 src
= urb
->transfer_buffer
+pipd
->offset
;
602 // Need to copy since isoc buffers not full
607 // No need to copy, just update destination buffer
611 // Return size of flattened buffer
612 return (dst
- (unsigned char *)urb
->transfer_buffer
);
615 static void st5481_start_rcv(void *context
)
617 struct st5481_in
*in
= context
;
618 struct st5481_adapter
*adapter
= in
->adapter
;
622 in
->urb
[0]->dev
= adapter
->usb_dev
;
623 SUBMIT_URB(in
->urb
[0], GFP_KERNEL
);
625 in
->urb
[1]->dev
= adapter
->usb_dev
;
626 SUBMIT_URB(in
->urb
[1], GFP_KERNEL
);
629 void st5481_in_mode(struct st5481_in
*in
, int mode
)
631 if (in
->mode
== mode
)
636 usb_unlink_urb(in
->urb
[0]);
637 usb_unlink_urb(in
->urb
[1]);
639 if (in
->mode
!= L1_MODE_NULL
) {
640 if (in
->mode
!= L1_MODE_TRANS
)
641 isdnhdlc_rcv_init(&in
->hdlc_state
,
642 in
->mode
== L1_MODE_HDLC_56K
);
644 st5481_usb_pipe_reset(in
->adapter
, in
->ep
, NULL
, NULL
);
645 st5481_usb_device_ctrl_msg(in
->adapter
, in
->counter
,
648 st5481_start_rcv(in
);
650 st5481_usb_device_ctrl_msg(in
->adapter
, in
->counter
,