blob: 017c67ea3f4c7681ef5fb0fbd66b7009b89ed14d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Joe Perches475be4d2012-02-19 19:52:38 -08007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
Adrian Bunk672c3fd2005-06-25 14:59:18 -070018static int st5481_isoc_flatten(struct urb *urb);
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* ======================================================================
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 */
28static 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) {
Joe Perches475be4d2012-02-19 19:52:38 -080039 test_and_clear_bit(0, &ctrl->busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return;
Joe Perches475be4d2012-02-19 19:52:38 -080041 }
42 urb->setup_packet =
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 (unsigned char *)&ctrl->msg_fifo.data[r_index];
Joe Perches475be4d2012-02-19 19:52:38 -080044
45 DBG(1, "request=0x%02x,value=0x%04x,index=%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 ((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 */
Adrian Bunk672c3fd2005-06-25 14:59:18 -070060static void usb_ctrl_msg(struct st5481_adapter *adapter,
61 u8 request, u8 requesttype, u16 value, u16 index,
62 ctrl_complete_t complete, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct st5481_ctrl *ctrl = &adapter->ctrl;
65 int w_index;
66 struct ctrl_msg *ctrl_msg;
Joe Perches475be4d2012-02-19 19:52:38 -080067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -070069 WARNING("control msg FIFO full");
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return;
71 }
Joe Perches475be4d2012-02-19 19:52:38 -080072 ctrl_msg = &ctrl->msg_fifo.data[w_index];
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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 */
88void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
Joe Perches475be4d2012-02-19 19:52:38 -080089 u8 request, u16 value,
90 ctrl_complete_t complete, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Joe Perches475be4d2012-02-19 19:52:38 -080092 usb_ctrl_msg(adapter, request,
93 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 value, 0, complete, context);
95}
96
97/*
98 * Asynchronous pipe reset (async version of usb_clear_halt).
99 */
100void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
Joe Perches475be4d2012-02-19 19:52:38 -0800101 u_char pipe,
102 ctrl_complete_t complete, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Joe Perches475be4d2012-02-19 19:52:38 -0800104 DBG(1, "pipe=%02x", pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
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
116void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command)
117{
Joe Perches475be4d2012-02-19 19:52:38 -0800118 DBG(8, "command=%s", ST5481_CMD_string(command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
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 */
David Howells7d12e782006-10-05 14:55:46 +0100128static void usb_ctrl_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct st5481_adapter *adapter = urb->context;
131 struct st5481_ctrl *ctrl = &adapter->ctrl;
132 struct ctrl_msg *ctrl_msg;
Joe Perches475be4d2012-02-19 19:52:38 -0800133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (unlikely(urb->status < 0)) {
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200135 switch (urb->status) {
Joe Perches475be4d2012-02-19 19:52:38 -0800136 case -ENOENT:
137 case -ESHUTDOWN:
138 case -ECONNRESET:
139 DBG(1, "urb killed status %d", urb->status);
140 return; // Give up
141 default:
142 WARNING("urb status %d", urb->status);
143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 }
146
147 ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
Joe Perches475be4d2012-02-19 19:52:38 -0800148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (ctrl_msg->dr.bRequest == USB_REQ_CLEAR_FEATURE) {
Joe Perches475be4d2012-02-19 19:52:38 -0800150 /* Special case handling for pipe reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 le16_to_cpus(&ctrl_msg->dr.wIndex);
David Vrabel3444b262009-04-08 17:36:28 +0000152 usb_reset_endpoint(adapter->usb_dev, ctrl_msg->dr.wIndex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Joe Perches475be4d2012-02-19 19:52:38 -0800154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (ctrl_msg->complete)
156 ctrl_msg->complete(ctrl_msg->context);
157
158 clear_bit(0, &ctrl->busy);
Joe Perches475be4d2012-02-19 19:52:38 -0800159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 // Try to send next control message
161 usb_next_ctrl_msg(urb, adapter);
162 return;
163}
164
165/* ======================================================================
166 * interrupt pipe
167 */
168
169/*
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.
174 */
David Howells7d12e782006-10-05 14:55:46 +0100175static void usb_int_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 u8 *data = urb->transfer_buffer;
178 u8 irqbyte;
179 struct st5481_adapter *adapter = urb->context;
180 int j;
181 int status;
182
183 switch (urb->status) {
Joe Perches475be4d2012-02-19 19:52:38 -0800184 case 0:
185 /* success */
186 break;
187 case -ECONNRESET:
188 case -ENOENT:
189 case -ESHUTDOWN:
190 /* this urb is terminated, clean up */
191 DBG(2, "urb shutting down with status: %d", urb->status);
192 return;
193 default:
194 WARNING("nonzero urb status received: %d", urb->status);
195 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
Joe Perches475be4d2012-02-19 19:52:38 -0800198
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200199 DBG_PACKET(2, data, INT_PKT_SIZE);
Joe Perches475be4d2012-02-19 19:52:38 -0800200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (urb->actual_length == 0) {
202 goto exit;
203 }
204
205 irqbyte = data[MPINT];
206 if (irqbyte & DEN_INT)
207 FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
208
209 if (irqbyte & DCOLL_INT)
210 FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
211
212 irqbyte = data[FFINT_D];
213 if (irqbyte & OUT_UNDERRUN)
214 FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
215
216 if (irqbyte & OUT_DOWN)
Joe Perches475be4d2012-02-19 19:52:38 -0800217 ;// printk("OUT_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 irqbyte = data[MPINT];
220 if (irqbyte & RXCI_INT)
221 FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
222
223 for (j = 0; j < 2; j++)
224 adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
225
226 urb->actual_length = 0;
227
228exit:
Joe Perches475be4d2012-02-19 19:52:38 -0800229 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (status)
Arjan van de Venb6c63932008-07-25 01:45:52 -0700231 WARNING("usb_submit_urb failed with result %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/* ======================================================================
235 * initialization
236 */
237
238int st5481_setup_usb(struct st5481_adapter *adapter)
239{
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;
246 int status;
247 struct urb *urb;
248 u8 *buf;
Joe Perches475be4d2012-02-19 19:52:38 -0800249
250 DBG(2, "");
251
252 if ((status = usb_reset_configuration(dev)) < 0) {
253 WARNING("reset_configuration failed,status=%d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return status;
255 }
256
257 intf = usb_ifnum_to_if(dev, 0);
258 if (intf)
259 altsetting = usb_altnum_to_altsetting(intf, 3);
260 if (!altsetting)
261 return -ENXIO;
262
263 // Check if the config is sane
Joe Perches475be4d2012-02-19 19:52:38 -0800264 if (altsetting->desc.bNumEndpoints != 7) {
Arjan van de Venb6c63932008-07-25 01:45:52 -0700265 WARNING("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -EINVAL;
267 }
268
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);
272
273 // Use alternative setting 3 on interface 0 to have 2B+D
Joe Perches475be4d2012-02-19 19:52:38 -0800274 if ((status = usb_set_interface(dev, 0, 3)) < 0) {
275 WARNING("usb_set_interface failed,status=%d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return status;
277 }
278
279 // Allocate URB for control endpoint
280 urb = usb_alloc_urb(0, GFP_KERNEL);
281 if (!urb) {
282 return -ENOMEM;
283 }
284 ctrl->urb = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Joe Perches475be4d2012-02-19 19:52:38 -0800286 // Fill the control URB
287 usb_fill_control_urb(urb, dev,
288 usb_sndctrlpipe(dev, 0),
289 NULL, NULL, 0, usb_ctrl_complete, adapter);
290
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
293
294 // Allocate URBs and buffers for interrupt endpoint
295 urb = usb_alloc_urb(0, GFP_KERNEL);
Joe Perches475be4d2012-02-19 19:52:38 -0800296 if (!urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return -ENOMEM;
298 }
299 intr->urb = urb;
Joe Perches475be4d2012-02-19 19:52:38 -0800300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
302 if (!buf) {
303 return -ENOMEM;
304 }
305
306 endpoint = &altsetting->endpoint[EP_INT-1];
Joe Perches475be4d2012-02-19 19:52:38 -0800307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 // Fill the interrupt URB
309 usb_fill_int_urb(urb, dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800310 usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress),
311 buf, INT_PKT_SIZE,
312 usb_int_complete, adapter,
313 endpoint->desc.bInterval);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
316}
317
318/*
319 * Release buffers and URBs for the interrupt and control
320 * endpoint.
321 */
322void st5481_release_usb(struct st5481_adapter *adapter)
323{
324 struct st5481_intr *intr = &adapter->intr;
325 struct st5481_ctrl *ctrl = &adapter->ctrl;
326
Joe Perches475be4d2012-02-19 19:52:38 -0800327 DBG(1, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 // Stop and free Control and Interrupt URBs
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200330 usb_kill_urb(ctrl->urb);
Jesper Juhl3c7208f2005-11-07 01:01:29 -0800331 kfree(ctrl->urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 usb_free_urb(ctrl->urb);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200333 ctrl->urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200335 usb_kill_urb(intr->urb);
Jesper Juhl3c7208f2005-11-07 01:01:29 -0800336 kfree(intr->urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 usb_free_urb(intr->urb);
Robert P. J. Dayf3429542008-04-28 02:14:40 -0700338 intr->urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/*
342 * Initialize the adapter.
343 */
344void st5481_start(struct st5481_adapter *adapter)
345{
Joe Perches475be4d2012-02-19 19:52:38 -0800346 static const u8 init_cmd_table[] = {
347 SET_DEFAULT, 0,
348 STT, 0,
349 SDA_MIN, 0x0d,
350 SDA_MAX, 0x29,
351 SDELAY_VALUE, 0x14,
352 GPIO_DIR, 0x01,
353 GPIO_OUT, RED_LED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354// FFCTRL_OUT_D,4,
355// FFCTRH_OUT_D,12,
Joe Perches475be4d2012-02-19 19:52:38 -0800356 FFCTRL_OUT_B1, 6,
357 FFCTRH_OUT_B1, 20,
358 FFCTRL_OUT_B2, 6,
359 FFCTRH_OUT_B2, 20,
360 MPMSK, RXCI_INT + DEN_INT + DCOLL_INT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 0
Joe Perches475be4d2012-02-19 19:52:38 -0800362 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct st5481_intr *intr = &adapter->intr;
364 int i = 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800365 u8 request, value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Joe Perches475be4d2012-02-19 19:52:38 -0800367 DBG(8, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Joe Perches475be4d2012-02-19 19:52:38 -0800369 adapter->leds = RED_LED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 // Start receiving on the interrupt endpoint
Joe Perches475be4d2012-02-19 19:52:38 -0800372 SUBMIT_URB(intr->urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 while ((request = init_cmd_table[i++])) {
375 value = init_cmd_table[i++];
376 st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
377 }
378 st5481_ph_command(adapter, ST5481_CMD_PUP);
379}
380
381/*
382 * Reset the adapter to default values.
383 */
384void st5481_stop(struct st5481_adapter *adapter)
385{
Joe Perches475be4d2012-02-19 19:52:38 -0800386 DBG(8, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
389}
390
391/* ======================================================================
392 * isochronous USB helpers
393 */
394
395static void
396fill_isoc_urb(struct urb *urb, struct usb_device *dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800397 unsigned int pipe, void *buf, int num_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int packet_size, usb_complete_t complete,
Joe Perches475be4d2012-02-19 19:52:38 -0800399 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 int k;
402
Joe Perches475be4d2012-02-19 19:52:38 -0800403 urb->dev = dev;
404 urb->pipe = pipe;
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200405 urb->interval = 1;
Joe Perches475be4d2012-02-19 19:52:38 -0800406 urb->transfer_buffer = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 urb->number_of_packets = num_packets;
Joe Perches475be4d2012-02-19 19:52:38 -0800408 urb->transfer_buffer_length = num_packets * packet_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 urb->actual_length = 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800410 urb->complete = complete;
411 urb->context = context;
412 urb->transfer_flags = URB_ISO_ASAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 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;
417 }
418}
419
420int
Joe Perches475be4d2012-02-19 19:52:38 -0800421st5481_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 int j, retval;
427 unsigned char *buf;
428
429 for (j = 0; j < 2; j++) {
430 retval = -ENOMEM;
431 urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL);
432 if (!urb[j])
433 goto err;
434
435 // Allocate memory for 2000bytes/sec (16Kb/s)
436 buf = kmalloc(buf_size, GFP_KERNEL);
437 if (!buf)
438 goto err;
Joe Perches475be4d2012-02-19 19:52:38 -0800439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 // Fill the isochronous URB
Joe Perches475be4d2012-02-19 19:52:38 -0800441 fill_isoc_urb(urb[j], dev, pipe, buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 num_packets, packet_size, complete,
443 context);
444 }
445 return 0;
446
Joe Perches475be4d2012-02-19 19:52:38 -0800447err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 for (j = 0; j < 2; j++) {
449 if (urb[j]) {
Jesper Juhl3c7208f2005-11-07 01:01:29 -0800450 kfree(urb[j]->transfer_buffer);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200451 urb[j]->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 usb_free_urb(urb[j]);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200453 urb[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 }
456 return retval;
457}
458
Joe Perches475be4d2012-02-19 19:52:38 -0800459void st5481_release_isocpipes(struct urb *urb[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 int j;
462
463 for (j = 0; j < 2; j++) {
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200464 usb_kill_urb(urb[j]);
Jesper Juhl3c7208f2005-11-07 01:01:29 -0800465 kfree(urb[j]->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 usb_free_urb(urb[j]);
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200467 urb[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469}
470
471/*
472 * Decode frames received on the B/D channel.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300473 * Note that this function will be called continuously
Joe Perches475be4d2012-02-19 19:52:38 -0800474 * with 64Kbit/s / 16Kbit/s of data and hence it will be
475 * called 50 times per second with 20 ISOC descriptors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 * Called at interrupt.
477 */
David Howells7d12e782006-10-05 14:55:46 +0100478static void usb_in_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct st5481_in *in = urb->context;
481 unsigned char *ptr;
482 struct sk_buff *skb;
483 int len, count, status;
484
485 if (unlikely(urb->status < 0)) {
Karsten Keil61ffcaf2005-09-17 23:52:42 +0200486 switch (urb->status) {
Joe Perches475be4d2012-02-19 19:52:38 -0800487 case -ENOENT:
488 case -ESHUTDOWN:
489 case -ECONNRESET:
490 DBG(1, "urb killed status %d", urb->status);
491 return; // Give up
492 default:
493 WARNING("urb status %d", urb->status);
494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 }
497
Joe Perches475be4d2012-02-19 19:52:38 -0800498 DBG_ISO_PACKET(0x80, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 len = st5481_isoc_flatten(urb);
501 ptr = urb->transfer_buffer;
502 while (len > 0) {
503 if (in->mode == L1_MODE_TRANS) {
504 memcpy(in->rcvbuf, ptr, len);
505 status = len;
506 len = 0;
507 } else {
508 status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
Joe Perches475be4d2012-02-19 19:52:38 -0800509 in->rcvbuf, in->bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ptr += count;
511 len -= count;
512 }
Joe Perches475be4d2012-02-19 19:52:38 -0800513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (status > 0) {
515 // Good frame received
Joe Perches475be4d2012-02-19 19:52:38 -0800516 DBG(4, "count=%d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 DBG_PACKET(0x400, in->rcvbuf, status);
518 if (!(skb = dev_alloc_skb(status))) {
Arjan van de Venb6c63932008-07-25 01:45:52 -0700519 WARNING("receive out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521 }
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) {
525 INFO("CRC error");
526 } else if (status == -HDLC_FRAMING_ERROR) {
527 INFO("framing error");
528 } else if (status == -HDLC_LENGTH_ERROR) {
529 INFO("length error");
530 }
531 }
532
533 // Prepare URB for next transfer
534 urb->dev = in->adapter->usb_dev;
535 urb->actual_length = 0;
536
537 SUBMIT_URB(urb, GFP_ATOMIC);
538}
539
540int st5481_setup_in(struct st5481_in *in)
541{
542 struct usb_device *dev = in->adapter->usb_dev;
543 int retval;
544
Joe Perches475be4d2012-02-19 19:52:38 -0800545 DBG(4, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
548 retval = -ENOMEM;
549 if (!in->rcvbuf)
550 goto err;
551
Joe Perches475be4d2012-02-19 19:52:38 -0800552 retval = st5481_setup_isocpipes(in->urb, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 usb_rcvisocpipe(dev, in->ep),
554 in->num_packets, in->packet_size,
555 in->num_packets * in->packet_size,
556 usb_in_complete, in);
557 if (retval)
558 goto err_free;
559 return 0;
560
Joe Perches475be4d2012-02-19 19:52:38 -0800561err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 kfree(in->rcvbuf);
Joe Perches475be4d2012-02-19 19:52:38 -0800563err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return retval;
565}
566
567void st5481_release_in(struct st5481_in *in)
568{
Joe Perches475be4d2012-02-19 19:52:38 -0800569 DBG(2, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 st5481_release_isocpipes(in->urb);
572}
573
574/*
575 * Make the transfer_buffer contiguous by
Joe Perches475be4d2012-02-19 19:52:38 -0800576 * copying from the iso descriptors if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Adrian Bunk672c3fd2005-06-25 14:59:18 -0700578static int st5481_isoc_flatten(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Joe Perches475be4d2012-02-19 19:52:38 -0800580 struct usb_iso_packet_descriptor *pipd, *pend;
581 unsigned char *src, *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 unsigned int len;
Joe Perches475be4d2012-02-19 19:52:38 -0800583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (urb->status < 0) {
585 return urb->status;
586 }
587 for (pipd = &urb->iso_frame_desc[0],
588 pend = &urb->iso_frame_desc[urb->number_of_packets],
Joe Perches475be4d2012-02-19 19:52:38 -0800589 dst = urb->transfer_buffer;
590 pipd < pend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 pipd++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (pipd->status < 0) {
594 return (pipd->status);
595 }
Joe Perches475be4d2012-02-19 19:52:38 -0800596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 len = pipd->actual_length;
598 pipd->actual_length = 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800599 src = urb->transfer_buffer + pipd->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (src != dst) {
602 // Need to copy since isoc buffers not full
603 while (len--) {
604 *dst++ = *src++;
Joe Perches475be4d2012-02-19 19:52:38 -0800605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 } else {
607 // No need to copy, just update destination buffer
608 dst += len;
609 }
610 }
611 // Return size of flattened buffer
612 return (dst - (unsigned char *)urb->transfer_buffer);
613}
614
615static void st5481_start_rcv(void *context)
616{
617 struct st5481_in *in = context;
618 struct st5481_adapter *adapter = in->adapter;
619
Joe Perches475be4d2012-02-19 19:52:38 -0800620 DBG(4, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 in->urb[0]->dev = adapter->usb_dev;
623 SUBMIT_URB(in->urb[0], GFP_KERNEL);
624
625 in->urb[1]->dev = adapter->usb_dev;
626 SUBMIT_URB(in->urb[1], GFP_KERNEL);
627}
628
629void st5481_in_mode(struct st5481_in *in, int mode)
630{
631 if (in->mode == mode)
632 return;
633
634 in->mode = mode;
635
636 usb_unlink_urb(in->urb[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 usb_unlink_urb(in->urb[1]);
638
639 if (in->mode != L1_MODE_NULL) {
Karsten Keilc38fc3b2009-07-08 20:31:42 +0200640 if (in->mode != L1_MODE_TRANS) {
641 u32 features = HDLC_BITREVERSE;
642
643 if (in->mode == L1_MODE_HDLC_56K)
644 features |= HDLC_56KBIT;
645 isdnhdlc_rcv_init(&in->hdlc_state, features);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
648 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
649 in->packet_size,
650 NULL, NULL);
651 st5481_start_rcv(in);
652 } else {
653 st5481_usb_device_ctrl_msg(in->adapter, in->counter,
654 0, NULL, NULL);
655 }
656}