Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Adrian Bunk | 672c3fd | 2005-06-25 14:59:18 -0700 | [diff] [blame] | 18 | static int st5481_isoc_flatten(struct urb *urb); |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 39 | test_and_clear_bit(0, &ctrl->busy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | return; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 41 | } |
| 42 | urb->setup_packet = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | (unsigned char *)&ctrl->msg_fifo.data[r_index]; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 44 | |
| 45 | DBG(1, "request=0x%02x,value=0x%04x,index=%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Adrian Bunk | 672c3fd | 2005-06-25 14:59:18 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | struct st5481_ctrl *ctrl = &adapter->ctrl; |
| 65 | int w_index; |
| 66 | struct ctrl_msg *ctrl_msg; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) { |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 69 | WARNING("control msg FIFO full"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return; |
| 71 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 72 | ctrl_msg = &ctrl->msg_fifo.data[w_index]; |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 89 | u8 request, u16 value, |
| 90 | ctrl_complete_t complete, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 92 | usb_ctrl_msg(adapter, request, |
| 93 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 101 | u_char pipe, |
| 102 | ctrl_complete_t complete, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 104 | DBG(1, "pipe=%02x", pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 118 | DBG(8, "command=%s", ST5481_CMD_string(command)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 128 | static void usb_ctrl_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | struct st5481_adapter *adapter = urb->context; |
| 131 | struct st5481_ctrl *ctrl = &adapter->ctrl; |
| 132 | struct ctrl_msg *ctrl_msg; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if (unlikely(urb->status < 0)) { |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 135 | switch (urb->status) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | ctrl_msg = (struct ctrl_msg *)urb->setup_packet; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (ctrl_msg->dr.bRequest == USB_REQ_CLEAR_FEATURE) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 150 | /* Special case handling for pipe reset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | le16_to_cpus(&ctrl_msg->dr.wIndex); |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 152 | usb_reset_endpoint(adapter->usb_dev, ctrl_msg->dr.wIndex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (ctrl_msg->complete) |
| 156 | ctrl_msg->complete(ctrl_msg->context); |
| 157 | |
| 158 | clear_bit(0, &ctrl->busy); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 175 | static void usb_int_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 198 | |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 199 | DBG_PACKET(2, data, INT_PKT_SIZE); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 217 | ;// printk("OUT_DOWN\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 229 | status = usb_submit_urb(urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | if (status) |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 231 | WARNING("usb_submit_urb failed with result %d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* ====================================================================== |
| 235 | * initialization |
| 236 | */ |
| 237 | |
| 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; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 249 | |
| 250 | DBG(2, ""); |
| 251 | |
| 252 | if ((status = usb_reset_configuration(dev)) < 0) { |
| 253 | WARNING("reset_configuration failed,status=%d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 264 | if (altsetting->desc.bNumEndpoints != 7) { |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 265 | WARNING("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | return -EINVAL; |
| 267 | } |
| 268 | |
| 269 | // The descriptor is wrong for some early samples of the ST5481 chip |
Vaishali Thakkar | 4e2987a | 2015-06-06 06:44:00 +0530 | [diff] [blame] | 270 | altsetting->endpoint[3].desc.wMaxPacketSize = cpu_to_le16(32); |
| 271 | altsetting->endpoint[4].desc.wMaxPacketSize = cpu_to_le16(32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | // Use alternative setting 3 on interface 0 to have 2B+D |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 274 | if ((status = usb_set_interface(dev, 0, 3)) < 0) { |
| 275 | WARNING("usb_set_interface failed,status=%d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 296 | if (!urb) { |
Marina Makienko | 114a6f8 | 2013-02-25 22:26:50 +0000 | [diff] [blame] | 297 | goto err1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | intr->urb = urb; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL); |
| 302 | if (!buf) { |
Marina Makienko | 114a6f8 | 2013-02-25 22:26:50 +0000 | [diff] [blame] | 303 | goto err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | endpoint = &altsetting->endpoint[EP_INT-1]; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | // Fill the interrupt URB |
| 309 | usb_fill_int_urb(urb, dev, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 310 | usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress), |
| 311 | buf, INT_PKT_SIZE, |
| 312 | usb_int_complete, adapter, |
| 313 | endpoint->desc.bInterval); |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return 0; |
Marina Makienko | 114a6f8 | 2013-02-25 22:26:50 +0000 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Release buffers and URBs for the interrupt and control |
| 328 | * endpoint. |
| 329 | */ |
| 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 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 335 | DBG(1, ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | // Stop and free Control and Interrupt URBs |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 338 | usb_kill_urb(ctrl->urb); |
Jesper Juhl | 3c7208f | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 339 | kfree(ctrl->urb->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | usb_free_urb(ctrl->urb); |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 341 | ctrl->urb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 343 | usb_kill_urb(intr->urb); |
Jesper Juhl | 3c7208f | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 344 | kfree(intr->urb->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | usb_free_urb(intr->urb); |
Robert P. J. Day | f342954 | 2008-04-28 02:14:40 -0700 | [diff] [blame] | 346 | intr->urb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Initialize the adapter. |
| 351 | */ |
| 352 | void st5481_start(struct st5481_adapter *adapter) |
| 353 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | // FFCTRL_OUT_D,4, |
| 363 | // FFCTRH_OUT_D,12, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | 0 |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 370 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | struct st5481_intr *intr = &adapter->intr; |
| 372 | int i = 0; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 373 | u8 request, value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 375 | DBG(8, ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 377 | adapter->leds = RED_LED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | // Start receiving on the interrupt endpoint |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 380 | SUBMIT_URB(intr->urb, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 392 | void st5481_stop(struct st5481_adapter *adapter) |
| 393 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 394 | DBG(8, ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 404 | fill_isoc_urb(struct urb *urb, struct usb_device *dev, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 405 | unsigned int pipe, void *buf, int num_packets, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | int packet_size, usb_complete_t complete, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 407 | void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
| 409 | int k; |
| 410 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 411 | urb->dev = dev; |
| 412 | urb->pipe = pipe; |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 413 | urb->interval = 1; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 414 | urb->transfer_buffer = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | urb->number_of_packets = num_packets; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 416 | urb->transfer_buffer_length = num_packets * packet_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | urb->actual_length = 0; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 418 | urb->complete = complete; |
| 419 | urb->context = context; |
| 420 | urb->transfer_flags = URB_ISO_ASAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | for (k = 0; k < num_packets; k++) { |
| 422 | urb->iso_frame_desc[k].offset = packet_size * k; |
| 423 | urb->iso_frame_desc[k].length = packet_size; |
| 424 | urb->iso_frame_desc[k].actual_length = 0; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | int |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 429 | st5481_setup_isocpipes(struct urb *urb[2], struct usb_device *dev, |
| 430 | unsigned int pipe, int num_packets, |
| 431 | int packet_size, int buf_size, |
| 432 | usb_complete_t complete, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
| 434 | int j, retval; |
| 435 | unsigned char *buf; |
| 436 | |
| 437 | for (j = 0; j < 2; j++) { |
| 438 | retval = -ENOMEM; |
| 439 | urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL); |
| 440 | if (!urb[j]) |
| 441 | goto err; |
| 442 | |
| 443 | // Allocate memory for 2000bytes/sec (16Kb/s) |
| 444 | buf = kmalloc(buf_size, GFP_KERNEL); |
| 445 | if (!buf) |
| 446 | goto err; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | // Fill the isochronous URB |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 449 | fill_isoc_urb(urb[j], dev, pipe, buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | num_packets, packet_size, complete, |
| 451 | context); |
| 452 | } |
| 453 | return 0; |
| 454 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 455 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | for (j = 0; j < 2; j++) { |
| 457 | if (urb[j]) { |
Jesper Juhl | 3c7208f | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 458 | kfree(urb[j]->transfer_buffer); |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 459 | urb[j]->transfer_buffer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | usb_free_urb(urb[j]); |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 461 | urb[j] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | return retval; |
| 465 | } |
| 466 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 467 | void st5481_release_isocpipes(struct urb *urb[2]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
| 469 | int j; |
| 470 | |
| 471 | for (j = 0; j < 2; j++) { |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 472 | usb_kill_urb(urb[j]); |
Jesper Juhl | 3c7208f | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 473 | kfree(urb[j]->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | usb_free_urb(urb[j]); |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 475 | urb[j] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Decode frames received on the B/D channel. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 481 | * Note that this function will be called continuously |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 482 | * with 64Kbit/s / 16Kbit/s of data and hence it will be |
| 483 | * called 50 times per second with 20 ISOC descriptors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | * Called at interrupt. |
| 485 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 486 | static void usb_in_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
| 488 | struct st5481_in *in = urb->context; |
| 489 | unsigned char *ptr; |
| 490 | struct sk_buff *skb; |
| 491 | int len, count, status; |
| 492 | |
| 493 | if (unlikely(urb->status < 0)) { |
Karsten Keil | 61ffcaf | 2005-09-17 23:52:42 +0200 | [diff] [blame] | 494 | switch (urb->status) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 495 | case -ENOENT: |
| 496 | case -ESHUTDOWN: |
| 497 | case -ECONNRESET: |
| 498 | DBG(1, "urb killed status %d", urb->status); |
| 499 | return; // Give up |
| 500 | default: |
| 501 | WARNING("urb status %d", urb->status); |
| 502 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 506 | DBG_ISO_PACKET(0x80, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | len = st5481_isoc_flatten(urb); |
| 509 | ptr = urb->transfer_buffer; |
| 510 | while (len > 0) { |
| 511 | if (in->mode == L1_MODE_TRANS) { |
| 512 | memcpy(in->rcvbuf, ptr, len); |
| 513 | status = len; |
| 514 | len = 0; |
| 515 | } else { |
| 516 | status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 517 | in->rcvbuf, in->bufsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | ptr += count; |
| 519 | len -= count; |
| 520 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if (status > 0) { |
| 523 | // Good frame received |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 524 | DBG(4, "count=%d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | DBG_PACKET(0x400, in->rcvbuf, status); |
| 526 | if (!(skb = dev_alloc_skb(status))) { |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 527 | WARNING("receive out of memory\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | break; |
| 529 | } |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 530 | skb_put_data(skb, in->rcvbuf, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb); |
| 532 | } else if (status == -HDLC_CRC_ERROR) { |
| 533 | INFO("CRC error"); |
| 534 | } else if (status == -HDLC_FRAMING_ERROR) { |
| 535 | INFO("framing error"); |
| 536 | } else if (status == -HDLC_LENGTH_ERROR) { |
| 537 | INFO("length error"); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | // Prepare URB for next transfer |
| 542 | urb->dev = in->adapter->usb_dev; |
| 543 | urb->actual_length = 0; |
| 544 | |
| 545 | SUBMIT_URB(urb, GFP_ATOMIC); |
| 546 | } |
| 547 | |
| 548 | int st5481_setup_in(struct st5481_in *in) |
| 549 | { |
| 550 | struct usb_device *dev = in->adapter->usb_dev; |
| 551 | int retval; |
| 552 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 553 | DBG(4, ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL); |
| 556 | retval = -ENOMEM; |
| 557 | if (!in->rcvbuf) |
| 558 | goto err; |
| 559 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 560 | retval = st5481_setup_isocpipes(in->urb, dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | usb_rcvisocpipe(dev, in->ep), |
| 562 | in->num_packets, in->packet_size, |
| 563 | in->num_packets * in->packet_size, |
| 564 | usb_in_complete, in); |
| 565 | if (retval) |
| 566 | goto err_free; |
| 567 | return 0; |
| 568 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 569 | err_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | kfree(in->rcvbuf); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 571 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | return retval; |
| 573 | } |
| 574 | |
| 575 | void st5481_release_in(struct st5481_in *in) |
| 576 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 577 | DBG(2, ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | st5481_release_isocpipes(in->urb); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Make the transfer_buffer contiguous by |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 584 | * copying from the iso descriptors if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | */ |
Adrian Bunk | 672c3fd | 2005-06-25 14:59:18 -0700 | [diff] [blame] | 586 | static int st5481_isoc_flatten(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 588 | struct usb_iso_packet_descriptor *pipd, *pend; |
| 589 | unsigned char *src, *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | unsigned int len; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | if (urb->status < 0) { |
| 593 | return urb->status; |
| 594 | } |
| 595 | for (pipd = &urb->iso_frame_desc[0], |
| 596 | pend = &urb->iso_frame_desc[urb->number_of_packets], |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 597 | dst = urb->transfer_buffer; |
| 598 | pipd < pend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | pipd++) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if (pipd->status < 0) { |
| 602 | return (pipd->status); |
| 603 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | len = pipd->actual_length; |
| 606 | pipd->actual_length = 0; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 607 | src = urb->transfer_buffer + pipd->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
| 609 | if (src != dst) { |
| 610 | // Need to copy since isoc buffers not full |
| 611 | while (len--) { |
| 612 | *dst++ = *src++; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } else { |
| 615 | // No need to copy, just update destination buffer |
| 616 | dst += len; |
| 617 | } |
| 618 | } |
| 619 | // Return size of flattened buffer |
| 620 | return (dst - (unsigned char *)urb->transfer_buffer); |
| 621 | } |
| 622 | |
| 623 | static void st5481_start_rcv(void *context) |
| 624 | { |
| 625 | struct st5481_in *in = context; |
| 626 | struct st5481_adapter *adapter = in->adapter; |
| 627 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 628 | DBG(4, ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | in->urb[0]->dev = adapter->usb_dev; |
| 631 | SUBMIT_URB(in->urb[0], GFP_KERNEL); |
| 632 | |
| 633 | in->urb[1]->dev = adapter->usb_dev; |
| 634 | SUBMIT_URB(in->urb[1], GFP_KERNEL); |
| 635 | } |
| 636 | |
| 637 | void st5481_in_mode(struct st5481_in *in, int mode) |
| 638 | { |
| 639 | if (in->mode == mode) |
| 640 | return; |
| 641 | |
| 642 | in->mode = mode; |
| 643 | |
| 644 | usb_unlink_urb(in->urb[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | usb_unlink_urb(in->urb[1]); |
| 646 | |
| 647 | if (in->mode != L1_MODE_NULL) { |
Karsten Keil | c38fc3b | 2009-07-08 20:31:42 +0200 | [diff] [blame] | 648 | if (in->mode != L1_MODE_TRANS) { |
| 649 | u32 features = HDLC_BITREVERSE; |
| 650 | |
| 651 | if (in->mode == L1_MODE_HDLC_56K) |
| 652 | features |= HDLC_56KBIT; |
| 653 | isdnhdlc_rcv_init(&in->hdlc_state, features); |
| 654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL); |
| 656 | st5481_usb_device_ctrl_msg(in->adapter, in->counter, |
| 657 | in->packet_size, |
| 658 | NULL, NULL); |
| 659 | st5481_start_rcv(in); |
| 660 | } else { |
| 661 | st5481_usb_device_ctrl_msg(in->adapter, in->counter, |
| 662 | 0, NULL, NULL); |
| 663 | } |
| 664 | } |