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  */
usb_next_ctrl_msg(struct urb * urb,struct st5481_adapter * adapter)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  */
usb_ctrl_msg(struct st5481_adapter * adapter,u8 request,u8 requesttype,u16 value,u16 index,ctrl_complete_t complete,void * context)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 		WARNING("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  */
st5481_usb_device_ctrl_msg(struct st5481_adapter * adapter,u8 request,u16 value,ctrl_complete_t complete,void * context)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  */
st5481_usb_pipe_reset(struct st5481_adapter * adapter,u_char pipe,ctrl_complete_t complete,void * context)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 
st5481_ph_command(struct st5481_adapter * adapter,unsigned int command)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  */
usb_ctrl_complete(struct urb * urb)128 static void usb_ctrl_complete(struct urb *urb)
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 		switch (urb->status) {
136 		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;
144 		}
145 	}
146 
147 	ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
148 
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);
153 	}
154 
155 	if (ctrl_msg->complete)
156 		ctrl_msg->complete(ctrl_msg->context);
157 
158 	clear_bit(0, &ctrl->busy);
159 
160 	// 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  */
usb_int_complete(struct urb * urb)175 static void usb_int_complete(struct urb *urb)
176 {
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) {
184 	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;
196 	}
197 
198 
199 	DBG_PACKET(2, data, INT_PKT_SIZE);
200 
201 	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)
217 		;//		printk("OUT_DOWN\n");
218 
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 
228 exit:
229 	status = usb_submit_urb(urb, GFP_ATOMIC);
230 	if (status)
231 		WARNING("usb_submit_urb failed with result %d", status);
232 }
233 
234 /* ======================================================================
235  * initialization
236  */
237 
st5481_setup_usb(struct st5481_adapter * adapter)238 int 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;
249 
250 	DBG(2, "");
251 
252 	if ((status = usb_reset_configuration(dev)) < 0) {
253 		WARNING("reset_configuration failed,status=%d", status);
254 		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
264 	if (altsetting->desc.bNumEndpoints != 7) {
265 		WARNING("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
266 		return -EINVAL;
267 	}
268 
269 	// The descriptor is wrong for some early samples of the ST5481 chip
270 	altsetting->endpoint[3].desc.wMaxPacketSize = cpu_to_le16(32);
271 	altsetting->endpoint[4].desc.wMaxPacketSize = cpu_to_le16(32);
272 
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);
276 		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;
285 
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);
290 
291 
292 	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);
296 	if (!urb) {
297 		goto err1;
298 	}
299 	intr->urb = urb;
300 
301 	buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
302 	if (!buf) {
303 		goto err2;
304 	}
305 
306 	endpoint = &altsetting->endpoint[EP_INT-1];
307 
308 	// Fill the interrupt URB
309 	usb_fill_int_urb(urb, dev,
310 			 usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress),
311 			 buf, INT_PKT_SIZE,
312 			 usb_int_complete, adapter,
313 			 endpoint->desc.bInterval);
314 
315 	return 0;
316 err2:
317 	usb_free_urb(intr->urb);
318 	intr->urb = NULL;
319 err1:
320 	usb_free_urb(ctrl->urb);
321 	ctrl->urb = NULL;
322 
323 	return -ENOMEM;
324 }
325 
326 /*
327  * Release buffers and URBs for the interrupt and control
328  * endpoint.
329  */
st5481_release_usb(struct st5481_adapter * adapter)330 void st5481_release_usb(struct st5481_adapter *adapter)
331 {
332 	struct st5481_intr *intr = &adapter->intr;
333 	struct st5481_ctrl *ctrl = &adapter->ctrl;
334 
335 	DBG(1, "");
336 
337 	// Stop and free Control and Interrupt URBs
338 	usb_kill_urb(ctrl->urb);
339 	kfree(ctrl->urb->transfer_buffer);
340 	usb_free_urb(ctrl->urb);
341 	ctrl->urb = NULL;
342 
343 	usb_kill_urb(intr->urb);
344 	kfree(intr->urb->transfer_buffer);
345 	usb_free_urb(intr->urb);
346 	intr->urb = NULL;
347 }
348 
349 /*
350  *  Initialize the adapter.
351  */
st5481_start(struct st5481_adapter * adapter)352 void st5481_start(struct st5481_adapter *adapter)
353 {
354 	static const u8 init_cmd_table[] = {
355 		SET_DEFAULT, 0,
356 		STT, 0,
357 		SDA_MIN, 0x0d,
358 		SDA_MAX, 0x29,
359 		SDELAY_VALUE, 0x14,
360 		GPIO_DIR, 0x01,
361 		GPIO_OUT, RED_LED,
362 //		FFCTRL_OUT_D,4,
363 //		FFCTRH_OUT_D,12,
364 		FFCTRL_OUT_B1, 6,
365 		FFCTRH_OUT_B1, 20,
366 		FFCTRL_OUT_B2, 6,
367 		FFCTRH_OUT_B2, 20,
368 		MPMSK, RXCI_INT + DEN_INT + DCOLL_INT,
369 		0
370 	};
371 	struct st5481_intr *intr = &adapter->intr;
372 	int i = 0;
373 	u8 request, value;
374 
375 	DBG(8, "");
376 
377 	adapter->leds = RED_LED;
378 
379 	// Start receiving on the interrupt endpoint
380 	SUBMIT_URB(intr->urb, GFP_KERNEL);
381 
382 	while ((request = init_cmd_table[i++])) {
383 		value = init_cmd_table[i++];
384 		st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
385 	}
386 	st5481_ph_command(adapter, ST5481_CMD_PUP);
387 }
388 
389 /*
390  * Reset the adapter to default values.
391  */
st5481_stop(struct st5481_adapter * adapter)392 void st5481_stop(struct st5481_adapter *adapter)
393 {
394 	DBG(8, "");
395 
396 	st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
397 }
398 
399 /* ======================================================================
400  * isochronous USB  helpers
401  */
402 
403 static void
fill_isoc_urb(struct urb * urb,struct usb_device * dev,unsigned int pipe,void * buf,int num_packets,int packet_size,usb_complete_t complete,void * context)404 fill_isoc_urb(struct urb *urb, struct usb_device *dev,
405 	      unsigned int pipe, void *buf, int num_packets,
406 	      int packet_size, usb_complete_t complete,
407 	      void *context)
408 {
409 	int k;
410 
411 	usb_fill_int_urb(urb, dev, pipe, buf, num_packets * packet_size,
412 			 complete, context, 1);
413 
414 	urb->number_of_packets = num_packets;
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
st5481_setup_isocpipes(struct urb * urb[2],struct usb_device * dev,unsigned int pipe,int num_packets,int packet_size,int buf_size,usb_complete_t complete,void * context)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 			kfree(urb[j]->transfer_buffer);
454 			urb[j]->transfer_buffer = NULL;
455 			usb_free_urb(urb[j]);
456 			urb[j] = NULL;
457 		}
458 	}
459 	return retval;
460 }
461 
st5481_release_isocpipes(struct urb * urb[2])462 void st5481_release_isocpipes(struct urb *urb[2])
463 {
464 	int j;
465 
466 	for (j = 0; j < 2; j++) {
467 		usb_kill_urb(urb[j]);
468 		kfree(urb[j]->transfer_buffer);
469 		usb_free_urb(urb[j]);
470 		urb[j] = NULL;
471 	}
472 }
473 
474 /*
475  * Decode frames received on the B/D channel.
476  * Note that this function will be called continuously
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  */
usb_in_complete(struct urb * urb)481 static void usb_in_complete(struct urb *urb)
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 		switch (urb->status) {
490 		case -ENOENT:
491 		case -ESHUTDOWN:
492 		case -ECONNRESET:
493 			DBG(1, "urb killed status %d", urb->status);
494 			return; // Give up
495 		default:
496 			WARNING("urb status %d", urb->status);
497 			break;
498 		}
499 	}
500 
501 	DBG_ISO_PACKET(0x80, urb);
502 
503 	len = st5481_isoc_flatten(urb);
504 	ptr = urb->transfer_buffer;
505 	while (len > 0) {
506 		if (in->mode == L1_MODE_TRANS) {
507 			memcpy(in->rcvbuf, ptr, len);
508 			status = len;
509 			len = 0;
510 		} else {
511 			status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
512 						 in->rcvbuf, in->bufsize);
513 			ptr += count;
514 			len -= count;
515 		}
516 
517 		if (status > 0) {
518 			// Good frame received
519 			DBG(4, "count=%d", status);
520 			DBG_PACKET(0x400, in->rcvbuf, status);
521 			if (!(skb = dev_alloc_skb(status))) {
522 				WARNING("receive out of memory\n");
523 				break;
524 			}
525 			skb_put_data(skb, in->rcvbuf, status);
526 			in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb);
527 		} else if (status == -HDLC_CRC_ERROR) {
528 			INFO("CRC error");
529 		} else if (status == -HDLC_FRAMING_ERROR) {
530 			INFO("framing error");
531 		} else if (status == -HDLC_LENGTH_ERROR) {
532 			INFO("length error");
533 		}
534 	}
535 
536 	// Prepare URB for next transfer
537 	urb->dev = in->adapter->usb_dev;
538 	urb->actual_length = 0;
539 
540 	SUBMIT_URB(urb, GFP_ATOMIC);
541 }
542 
st5481_setup_in(struct st5481_in * in)543 int st5481_setup_in(struct st5481_in *in)
544 {
545 	struct usb_device *dev = in->adapter->usb_dev;
546 	int retval;
547 
548 	DBG(4, "");
549 
550 	in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
551 	retval = -ENOMEM;
552 	if (!in->rcvbuf)
553 		goto err;
554 
555 	retval = st5481_setup_isocpipes(in->urb, dev,
556 					usb_rcvisocpipe(dev, in->ep),
557 					in->num_packets,  in->packet_size,
558 					in->num_packets * in->packet_size,
559 					usb_in_complete, in);
560 	if (retval)
561 		goto err_free;
562 	return 0;
563 
564 err_free:
565 	kfree(in->rcvbuf);
566 err:
567 	return retval;
568 }
569 
st5481_release_in(struct st5481_in * in)570 void st5481_release_in(struct st5481_in *in)
571 {
572 	DBG(2, "");
573 
574 	st5481_release_isocpipes(in->urb);
575 }
576 
577 /*
578  * Make the transfer_buffer contiguous by
579  * copying from the iso descriptors if necessary.
580  */
st5481_isoc_flatten(struct urb * urb)581 static int st5481_isoc_flatten(struct urb *urb)
582 {
583 	struct usb_iso_packet_descriptor *pipd, *pend;
584 	unsigned char *src, *dst;
585 	unsigned int len;
586 
587 	if (urb->status < 0) {
588 		return urb->status;
589 	}
590 	for (pipd = &urb->iso_frame_desc[0],
591 		     pend = &urb->iso_frame_desc[urb->number_of_packets],
592 		     dst = urb->transfer_buffer;
593 	     pipd < pend;
594 	     pipd++) {
595 
596 		if (pipd->status < 0) {
597 			return (pipd->status);
598 		}
599 
600 		len = pipd->actual_length;
601 		pipd->actual_length = 0;
602 		src = urb->transfer_buffer + pipd->offset;
603 
604 		if (src != dst) {
605 			// Need to copy since isoc buffers not full
606 			while (len--) {
607 				*dst++ = *src++;
608 			}
609 		} else {
610 			// No need to copy, just update destination buffer
611 			dst += len;
612 		}
613 	}
614 	// Return size of flattened buffer
615 	return (dst - (unsigned char *)urb->transfer_buffer);
616 }
617 
st5481_start_rcv(void * context)618 static void st5481_start_rcv(void *context)
619 {
620 	struct st5481_in *in = context;
621 	struct st5481_adapter *adapter = in->adapter;
622 
623 	DBG(4, "");
624 
625 	in->urb[0]->dev = adapter->usb_dev;
626 	SUBMIT_URB(in->urb[0], GFP_KERNEL);
627 
628 	in->urb[1]->dev = adapter->usb_dev;
629 	SUBMIT_URB(in->urb[1], GFP_KERNEL);
630 }
631 
st5481_in_mode(struct st5481_in * in,int mode)632 void st5481_in_mode(struct st5481_in *in, int mode)
633 {
634 	if (in->mode == mode)
635 		return;
636 
637 	in->mode = mode;
638 
639 	usb_unlink_urb(in->urb[0]);
640 	usb_unlink_urb(in->urb[1]);
641 
642 	if (in->mode != L1_MODE_NULL) {
643 		if (in->mode != L1_MODE_TRANS) {
644 			u32 features = HDLC_BITREVERSE;
645 
646 			if (in->mode == L1_MODE_HDLC_56K)
647 				features |= HDLC_56KBIT;
648 			isdnhdlc_rcv_init(&in->hdlc_state, features);
649 		}
650 		st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
651 		st5481_usb_device_ctrl_msg(in->adapter, in->counter,
652 					   in->packet_size,
653 					   NULL, NULL);
654 		st5481_start_rcv(in);
655 	} else {
656 		st5481_usb_device_ctrl_msg(in->adapter, in->counter,
657 					   0, NULL, NULL);
658 	}
659 }
660