carl9170 firmware: checkpatch/style fixes
[carl9170fw.git] / carlfw / usb / usb.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * USB Controller
5  *
6  * Copyright (c) 2000-2005  ZyDAS Technology Corporation
7  * Copyright (c) 2007-2009 Atheros Communications, Inc.
8  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
9  * Copyright 2009-2011  Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 #include "carl9170.h"
26 #include "usb.h"
27 #include "printf.h"
28 #include "rom.h"
29
30 /*
31  * NB: The firmware has to write into these structures
32  * so don't try to make them "const".
33  */
34
35 static struct ar9170_usb_config usb_config_highspeed = {
36         .cfg = {
37                 .bLength = USB_DT_CONFIG_SIZE,
38                 .bDescriptorType = USB_DT_CONFIG,
39                 .wTotalLength = cpu_to_le16(sizeof(usb_config_highspeed)),
40                 .bNumInterfaces = 1,
41                 .bConfigurationValue = 1,
42                 .iConfiguration = 0,
43                 .bmAttributes = USB_CONFIG_ATT_ONE |
44 #ifdef CONFIG_CARL9170FW_WOL
45                                 USB_CONFIG_ATT_WAKEUP |
46 #endif /* CONFIG_CARL9170FW_WOL */
47                                 0,
48                 .bMaxPower = 0xfa, /* 500 mA */
49         },
50
51         .intf = {
52                 .bLength = USB_DT_INTERFACE_SIZE,
53                 .bDescriptorType = USB_DT_INTERFACE,
54                 .bInterfaceNumber = 0,
55                 .bAlternateSetting = 0,
56                 .bNumEndpoints = AR9170_USB_NUM_EXTRA_EP,
57                 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
58                 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
59                 .bInterfaceProtocol = 0,
60                 .iInterface = 0,
61         },
62
63         .ep = {
64                 {       /* EP 1 */
65                         .bLength = USB_DT_ENDPOINT_SIZE,
66                         .bDescriptorType = USB_DT_ENDPOINT,
67                         .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_TX,
68                         .bmAttributes = USB_ENDPOINT_XFER_BULK,
69                         .wMaxPacketSize = cpu_to_le16(512),
70                         .bInterval = 0,
71                 },
72
73                 {       /* EP 2 */
74                         .bLength = USB_DT_ENDPOINT_SIZE,
75                         .bDescriptorType = USB_DT_ENDPOINT,
76                         .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_RX,
77                         .bmAttributes = USB_ENDPOINT_XFER_BULK,
78                         .wMaxPacketSize = cpu_to_le16(512),
79                         .bInterval = 0,
80                 },
81
82                 {       /* EP 3 */
83                         .bLength = USB_DT_ENDPOINT_SIZE,
84                         .bDescriptorType = USB_DT_ENDPOINT,
85                         .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_IRQ,
86                         .bmAttributes = USB_ENDPOINT_XFER_INT,
87                         .wMaxPacketSize = cpu_to_le16(64),
88                         .bInterval = 1,
89                 },
90
91                 {       /* EP 4 */
92                         .bLength = USB_DT_ENDPOINT_SIZE,
93                         .bDescriptorType = USB_DT_ENDPOINT,
94                         .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_CMD,
95                         .bmAttributes = USB_ENDPOINT_XFER_INT,
96                         .wMaxPacketSize = cpu_to_le16(64),
97                         .bInterval = 1,
98                 },
99         },
100 };
101
102 static struct ar9170_usb_config usb_config_fullspeed = {
103         .cfg = {
104                 .bLength = USB_DT_CONFIG_SIZE,
105                 .bDescriptorType = USB_DT_CONFIG,
106                 .wTotalLength = cpu_to_le16(sizeof(usb_config_fullspeed)),
107                 .bNumInterfaces = 1,
108                 .bConfigurationValue = 1,
109                 .iConfiguration = 0,
110                 .bmAttributes = USB_CONFIG_ATT_ONE |
111 #ifdef CONFIG_CARL9170FW_WOL
112                                 USB_CONFIG_ATT_WAKEUP |
113 #endif /* CONFIG_CARL9170FW_WOL */
114                                 0,
115                 .bMaxPower = 0xfa, /* 500 mA */
116         },
117
118         .intf = {
119                 .bLength = USB_DT_INTERFACE_SIZE,
120                 .bDescriptorType = USB_DT_INTERFACE,
121                 .bInterfaceNumber = 0,
122                 .bAlternateSetting = 0,
123                 .bNumEndpoints = AR9170_USB_NUM_EXTRA_EP,
124                 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
125                 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
126                 .bInterfaceProtocol = 0,
127                 .iInterface = 0,
128         },
129
130         .ep = {
131                 {       /* EP 1 */
132                         .bLength = USB_DT_ENDPOINT_SIZE,
133                         .bDescriptorType = USB_DT_ENDPOINT,
134                         .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_TX,
135                         .bmAttributes = USB_ENDPOINT_XFER_BULK,
136                         .wMaxPacketSize = cpu_to_le16(64),
137                         .bInterval = 0,
138                 },
139
140                 {       /* EP 2 */
141                         .bLength = USB_DT_ENDPOINT_SIZE,
142                         .bDescriptorType = USB_DT_ENDPOINT,
143                         .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_RX,
144                         .bmAttributes = USB_ENDPOINT_XFER_BULK,
145                         .wMaxPacketSize = cpu_to_le16(64),
146                         .bInterval = 0,
147                 },
148
149                 {       /* EP 3 */
150                         .bLength = USB_DT_ENDPOINT_SIZE,
151                         .bDescriptorType = USB_DT_ENDPOINT,
152                         .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_IRQ,
153                         .bmAttributes = USB_ENDPOINT_XFER_INT,
154                         .wMaxPacketSize = cpu_to_le16(64),
155                         .bInterval = 1,
156                 },
157
158                 {       /* EP 4 */
159                         .bLength = USB_DT_ENDPOINT_SIZE,
160                         .bDescriptorType = USB_DT_ENDPOINT,
161                         .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_CMD,
162                         .bmAttributes = USB_ENDPOINT_XFER_INT,
163                         .wMaxPacketSize = cpu_to_le16(64),
164                         .bInterval = 1,
165                 },
166         },
167 };
168
169 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
170 static void usb_reset_eps(void)
171 {
172         unsigned int i;
173
174         /* clear all EPs' toggle bit */
175         for (i = 1; i < __AR9170_USB_NUM_MAX_EP; i++) {
176                 usb_set_input_ep_toggle(i);
177                 usb_clear_input_ep_toggle(i);
178         }
179
180         /*
181          * NB: I've no idea why this cannot be integrated into the
182          * previous loop?
183          */
184         for (i = 1; i < __AR9170_USB_NUM_MAX_EP; i++) {
185                 usb_set_output_ep_toggle(i);
186                 usb_clear_output_ep_toggle(i);
187         }
188 }
189 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
190
191
192 #ifdef CONFIG_CARL9170FW_USB_INIT_FIRMWARE
193 static void usb_pta_init(void)
194 {
195         unsigned int usb_dma_ctrl = 0;
196         /* Set PTA mode to USB */
197         andl(AR9170_PTA_REG_DMA_MODE_CTRL,
198                 ~AR9170_PTA_DMA_MODE_CTRL_DISABLE_USB);
199
200         /* Do a software reset to PTA component */
201         orl(AR9170_PTA_REG_DMA_MODE_CTRL, AR9170_PTA_DMA_MODE_CTRL_RESET);
202         andl(AR9170_PTA_REG_DMA_MODE_CTRL, ~AR9170_PTA_DMA_MODE_CTRL_RESET);
203
204         if (usb_detect_highspeed()) {
205                 fw.usb.os_cfg_desc = &usb_config_fullspeed;
206                 fw.usb.cfg_desc = &usb_config_highspeed;
207
208                 /* 512 Byte DMA transfers */
209                 usb_dma_ctrl |= AR9170_USB_DMA_CTL_HIGH_SPEED;
210         } else {
211                 fw.usb.cfg_desc = &usb_config_fullspeed;
212                 fw.usb.os_cfg_desc = &usb_config_highspeed;
213         }
214
215 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
216 # if (CONFIG_CARL9170FW_RX_FRAME_LEN == 4096)
217         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_4K;
218 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 8192)
219         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_8K;
220 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 16384)
221         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_16K;
222 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 32768)
223         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_32K;
224 # else
225 #       error "Invalid AR9170_RX_FRAME_LEN setting"
226 # endif
227
228 #else /* CONFIG_CARL9170FW_USB_UP_STREAM */
229         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_PACKET_MODE;
230 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
231
232 #ifdef CONFIG_CARL9170FW_USB_DOWN_STREAM
233         /* Enable down stream mode */
234         usb_dma_ctrl |= AR9170_USB_DMA_CTL_DOWN_STREAM;
235 #endif /* CONFIG_CARL9170FW_USB_DOWN_STREAM */
236
237 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
238         /* Set the up stream mode maximum aggregate number */
239         set(AR9170_USB_REG_MAX_AGG_UPLOAD, 4);
240
241         /*
242          * Set the up stream mode timeout value.
243          * NB: The vendor driver (otus) set 0x80?
244          */
245         set(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
246 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
247
248         /* Enable up stream and down stream */
249         usb_dma_ctrl |= AR9170_USB_DMA_CTL_ENABLE_TO_DEVICE |
250             AR9170_USB_DMA_CTL_ENABLE_FROM_DEVICE;
251
252         set(AR9170_USB_REG_DMA_CTL, usb_dma_ctrl);
253 }
254 #endif /* CONFIG_CARL9170FW_USB_INIT_FIRMWARE */
255
256 void usb_init(void)
257 {
258 #ifdef CONFIG_CARL9170FW_USB_INIT_FIRMWARE
259         usb_pta_init();
260 #endif /* CONFIG_CARL9170FW_USB_INIT_FIRMWARE */
261
262         fw.usb.config = 1;
263         /*
264          * The fw structure is always initialized with "0"
265          * during boot(); No need to waste precious bytes here.
266          *
267          * fw.usb.interface_setting = 0;
268          * fw.usb.alternate_interface_setting = 0;
269          * fw.usb.device_feature = 0;
270          */
271
272 #ifdef CONFIG_CARL9170FW_WOL
273         fw.usb.device_feature |= USB_DEVICE_REMOTE_WAKEUP;
274         usb_enable_remote_wakeup();
275 #endif /* CONFIG_CARL9170FW_WOL */
276 }
277
278 #define GET_ARRAY(a, o) ((uint32_t *) (((unsigned long) data) + offset))
279
280 static void usb_ep0rx_data(const void *data, const unsigned int len)
281 {
282         unsigned int offset;
283         uint32_t value;
284
285         BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
286         BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
287
288         for (offset = 0; offset < ((len + 3) & ~3); offset += 4) {
289                 value = get(AR9170_USB_REG_EP0_DATA);
290                 memcpy(GET_ARRAY(data, offset), &value,
291                        min(len - offset, (unsigned int)4));
292         }
293 }
294
295 static int usb_ep0tx_data(const void *data, const unsigned int len)
296 {
297         unsigned int offset = 0, block, last_block = 0;
298         uint32_t value;
299
300         BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
301         BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
302
303         block = min(len, (unsigned int) 4);
304         offset = 0;
305         while (offset < len) {
306
307                 if (last_block != block || block < 4)
308                         setb(AR9170_USB_REG_FIFO_SIZE, (1 << block) - 1);
309
310                 memcpy(&value, GET_ARRAY(data, offset), block);
311
312                 set(AR9170_USB_REG_EP0_DATA, value);
313
314                 offset += block;
315                 last_block = block = min(len - offset, (unsigned int) 4);
316         }
317
318         setb(AR9170_USB_REG_FIFO_SIZE, 0xf);
319
320         /* this will push the data to the host */
321         return 1;
322 }
323 #undef GET_ARRAY
324
325 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
326 static int usb_get_status(const struct usb_ctrlrequest *ctrl)
327 {
328         __le16 status = cpu_to_le16(fw.usb.device_feature);
329
330         if ((ctrl->bRequestType & USB_DIR_MASK) != USB_DIR_IN)
331                 return -1;
332
333         switch (ctrl->bRequestType & USB_RECIP_MASK) {
334         case USB_RECIP_DEVICE:
335                 status &= cpu_to_le16(~USB_DEVICE_SELF_POWERED);
336                 status &= cpu_to_le16(~USB_DEVICE_REMOTE_WAKEUP);
337                 break;
338
339         case USB_RECIP_INTERFACE:
340                 /* USB spec: This is reserved for future use. */
341                 status = cpu_to_le16(0);
342                 break;
343
344         case USB_RECIP_ENDPOINT:
345         case USB_RECIP_OTHER:
346         default:
347                 break;
348         }
349
350         return usb_ep0tx_data((const void *) &status, sizeof(status));
351 }
352
353 static int usb_get_string_desc(const struct usb_ctrlrequest *ctrl)
354 {
355         const struct usb_string_descriptor *string_desc = NULL;
356
357         switch (le16_to_cpu(ctrl->wValue) & 0xff) {
358         case 0x00:
359                 string_desc = (const struct usb_string_descriptor *)
360                         rom.hw.usb.string0_desc;
361                 break;
362
363         case 0x10:
364                 string_desc = (const struct usb_string_descriptor *)
365                         rom.hw.usb.string1_desc;
366                 break;
367
368         case 0x20:
369                 string_desc = (const struct usb_string_descriptor *)
370                         rom.hw.usb.string2_desc;
371                 break;
372
373         case 0x30:
374                 string_desc = (const struct usb_string_descriptor *)
375                         rom.hw.usb.string3_desc;
376                 break;
377
378         default:
379                 break;
380         }
381
382         if (string_desc)
383                 return usb_ep0tx_data(string_desc, string_desc->bLength);
384
385         return -1;
386 }
387
388 static int usb_get_device_desc(const struct usb_ctrlrequest *ctrl __unused)
389 {
390         return usb_ep0tx_data(&rom.hw.usb.device_desc,
391                               rom.hw.usb.device_desc.bLength);
392 }
393
394 static int usb_get_config_desc(const struct usb_ctrlrequest *ctrl __unused)
395 {
396         fw.usb.cfg_desc->cfg.bDescriptorType = USB_DT_CONFIG;
397
398         return usb_ep0tx_data(fw.usb.cfg_desc,
399                 le16_to_cpu(fw.usb.cfg_desc->cfg.wTotalLength));
400 }
401
402 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
403 static int usb_get_otherspeed_desc(const struct usb_ctrlrequest *ctrl __unused)
404 {
405
406         fw.usb.os_cfg_desc->cfg.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
407
408         return usb_ep0tx_data(fw.usb.os_cfg_desc,
409                 le16_to_cpu(fw.usb.os_cfg_desc->cfg.wTotalLength));
410 }
411 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
412
413 static int usb_get_qualifier_desc(const struct usb_ctrlrequest *ctrl __unused)
414 {
415         struct usb_qualifier_descriptor qual;
416
417         /*
418          * The qualifier descriptor shares some structural details
419          * with the main device descriptor.
420          */
421
422         memcpy(&qual, &rom.hw.usb.device_desc, sizeof(qual));
423
424         /* (Re)-Initialize fields */
425         qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
426         qual.bLength = sizeof(qual);
427         qual.bNumConfigurations = rom.hw.usb.device_desc.bNumConfigurations;
428         qual.bRESERVED = 0;
429
430         return usb_ep0tx_data(&qual, qual.bLength);
431 }
432
433 #define USB_CHECK_REQTYPE(ctrl, recip, dir)                     \
434         (((ctrl->bRequestType & USB_RECIP_MASK) != recip) ||    \
435          ((ctrl->bRequestType & USB_DIR_MASK) != dir))
436
437 static int usb_get_descriptor(const struct usb_ctrlrequest *ctrl)
438 {
439         int status = -1;
440
441         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
442                 return status;
443
444         switch (le16_to_cpu(ctrl->wValue) >> 8) {
445         case USB_DT_DEVICE:
446                 status = usb_get_device_desc(ctrl);
447                 break;
448
449         case USB_DT_CONFIG:
450                 status = usb_get_config_desc(ctrl);
451                 break;
452
453         case USB_DT_STRING:
454                 status = usb_get_string_desc(ctrl);
455                 break;
456
457         case USB_DT_INTERFACE:
458                 break;
459
460         case USB_DT_ENDPOINT:
461                 break;
462
463         case USB_DT_DEVICE_QUALIFIER:
464                 status = usb_get_qualifier_desc(ctrl);
465                 break;
466
467 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
468         case USB_DT_OTHER_SPEED_CONFIG:
469                 status = usb_get_otherspeed_desc(ctrl);
470                 break;
471 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
472         default:
473                 break;
474
475         }
476
477         return status;
478 }
479
480 static int usb_get_configuration(const struct usb_ctrlrequest *ctrl)
481 {
482         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
483                 return -1;
484
485         return usb_ep0tx_data(&fw.usb.config, 1);
486 }
487
488 static int usb_set_configuration(const struct usb_ctrlrequest *ctrl)
489 {
490         unsigned int config;
491
492         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
493                 return -1;
494
495         config = le16_to_cpu(ctrl->wValue);
496         switch (config) {
497         case 0:
498                 /* Disable Device */
499                 andb(AR9170_USB_REG_DEVICE_ADDRESS,
500                       (uint8_t) ~(AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
501 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
502         case 1:
503                 fw.usb.config = config;
504
505                 if (usb_detect_highspeed()) {
506                         /* High Speed Configuration */
507                         usb_init_highspeed_fifo_cfg();
508                 } else {
509                         /* Full Speed Configuration */
510                         usb_init_fullspeed_fifo_cfg();
511                 }
512                 break;
513
514         default:
515                 return -1;
516         }
517         /* usb_pta_init() ? */
518
519         usb_reset_eps();
520         orb(AR9170_USB_REG_DEVICE_ADDRESS,
521             (AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
522
523         usb_enable_global_int();
524         usb_trigger_out();
525         return 1;
526 #else
527         default:
528                 return -1;
529         }
530 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
531 }
532
533 static int usb_set_address(const struct usb_ctrlrequest *ctrl)
534 {
535         unsigned int address;
536
537         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
538                 return -1;
539
540         address = le16_to_cpu(ctrl->wValue);
541
542         /*
543          * The original firmware used 0x100 (which is, of course,
544          * too big to fit into uint8_t).
545          * However based on the available information (hw.h), BIT(7)
546          * is used as some sort of flag and should not be
547          * part of the device address.
548          */
549         if (address >= BIT(7))
550                 return -1;
551
552         setb(AR9170_USB_REG_DEVICE_ADDRESS, (uint8_t) address);
553         return 1;
554 }
555
556 static int usb_get_interface(const struct usb_ctrlrequest *ctrl)
557 {
558         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_IN))
559                 return -1;
560
561         if (usb_configured() == false)
562                 return -1;
563
564         switch (fw.usb.config) {
565         case 1:
566                 break;
567
568         default:
569                 return -1;
570         }
571
572         return usb_ep0tx_data(&fw.usb.alternate_interface_setting, 1);
573 }
574
575 static int usb_manipulate_feature(const struct usb_ctrlrequest *ctrl, bool __unused clear)
576 {
577         unsigned int feature;
578         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
579                 return -1;
580
581         if (usb_configured() == false)
582                 return -1;
583
584         feature = le16_to_cpu(ctrl->wValue);
585
586 #ifdef CONFIG_CARL9170FW_WOL
587         if (feature & USB_DEVICE_REMOTE_WAKEUP) {
588                 if (clear)
589                         usb_disable_remote_wakeup();
590                 else
591                         usb_enable_remote_wakeup();
592         }
593 #endif /* CONFIG_CARL9170FW_WOL */
594
595         if (clear)
596                 fw.usb.device_feature &= ~feature;
597         else
598                 fw.usb.device_feature |= feature;
599
600         return 1;
601 }
602
603 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
604 static int usb_set_interface(const struct usb_ctrlrequest *ctrl)
605 {
606         unsigned int intf, alt_intf;
607         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_OUT))
608                 return -1;
609
610         if (usb_configured() == false)
611                 return -1;
612
613         intf = le16_to_cpu(ctrl->wIndex);
614         alt_intf = le16_to_cpu(ctrl->wValue);
615
616         switch (intf) {
617         case 0:
618                 if (alt_intf != fw.usb.cfg_desc->intf.bAlternateSetting)
619                         return -1;
620
621                 fw.usb.interface_setting = (uint8_t) intf;
622                 fw.usb.alternate_interface_setting = (uint8_t) alt_intf;
623                 if (usb_detect_highspeed())
624                         usb_init_highspeed_fifo_cfg();
625                 else
626                         usb_init_fullspeed_fifo_cfg();
627
628                 usb_reset_eps();
629                 usb_enable_global_int();
630                 usb_trigger_out();
631                 return 1;
632
633         default:
634                 return -1;
635         }
636 }
637 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
638 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
639
640 static int usb_standard_command(const struct usb_ctrlrequest *ctrl __unused)
641 {
642         int status = -1;
643
644 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
645         switch (ctrl->bRequest) {
646         case USB_REQ_GET_STATUS:
647                 status = usb_get_status(ctrl);
648                 break;
649
650         case USB_REQ_CLEAR_FEATURE:
651         case USB_REQ_SET_FEATURE:
652                 usb_manipulate_feature(ctrl, ctrl->bRequest == USB_REQ_CLEAR_FEATURE);
653                 break;
654
655         case USB_REQ_SET_ADDRESS:
656                 status = usb_set_address(ctrl);
657                 break;
658
659         case USB_REQ_GET_DESCRIPTOR:
660                 status = usb_get_descriptor(ctrl);
661                 break;
662
663         case USB_REQ_SET_DESCRIPTOR:
664                 break;
665
666         case USB_REQ_GET_CONFIGURATION:
667                 status = usb_get_configuration(ctrl);
668                 break;
669
670         case USB_REQ_SET_CONFIGURATION:
671                 status = usb_set_configuration(ctrl);
672                 break;
673
674         case USB_REQ_GET_INTERFACE:
675                 status = usb_get_interface(ctrl);
676                 break;
677
678         case USB_REQ_SET_INTERFACE:
679 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
680                 status = usb_set_interface(ctrl);
681 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
682                 break;
683
684         case USB_REQ_SYNCH_FRAME:
685                 break;
686
687         default:
688                 break;
689
690         }
691 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
692
693         return status;
694 }
695
696 static int usb_class_command(const struct usb_ctrlrequest *ctrl __unused)
697 {
698         return -1;
699 }
700
701 static int usb_vendor_command(const struct usb_ctrlrequest *ctrl __unused)
702 {
703         /*
704          * Note: Firmware upload/boot is not implemented.
705          * It's impossible to replace the current image
706          * in place.
707          */
708
709         return -1;
710 }
711
712 #undef USB_CHECK_TYPE
713
714 void usb_ep0setup(void)
715 {
716         struct usb_ctrlrequest ctrl;
717         int status = -1;
718         usb_ep0rx_data(&ctrl, sizeof(ctrl));
719
720         switch (ctrl.bRequestType & USB_TYPE_MASK) {
721         case USB_TYPE_STANDARD:
722                 status = usb_standard_command(&ctrl);
723                 break;
724
725         case USB_TYPE_CLASS:
726                 status = usb_class_command(&ctrl);
727                 break;
728
729         case USB_TYPE_VENDOR:
730                 status = usb_vendor_command(&ctrl);
731                 break;
732
733         default:
734                 break;
735
736         }
737
738         if (status < 0)
739                 fw.usb.ep0_action |= CARL9170_EP0_STALL;
740 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
741         if (status > 0)
742                 fw.usb.ep0_action |= CARL9170_EP0_TRIGGER;
743 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
744 }
745
746 void usb_ep0rx(void)
747 {
748         if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
749                 return ;
750
751         usb_ep0rx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
752         fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;
753 }
754
755 void usb_ep0tx(void)
756 {
757         if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
758                 return ;
759
760         usb_ep0tx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
761         fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;
762 }