kconfig: use xfwrite wrapper function to silence warnings
[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 static void usb_pta_init(void)
193 {
194         unsigned int usb_dma_ctrl = 0;
195         /* Set PTA mode to USB */
196         andl(AR9170_PTA_REG_DMA_MODE_CTRL,
197                 ~AR9170_PTA_DMA_MODE_CTRL_DISABLE_USB);
198
199         /* Do a software reset to PTA component */
200         orl(AR9170_PTA_REG_DMA_MODE_CTRL, AR9170_PTA_DMA_MODE_CTRL_RESET);
201         andl(AR9170_PTA_REG_DMA_MODE_CTRL, ~AR9170_PTA_DMA_MODE_CTRL_RESET);
202
203         if (usb_detect_highspeed()) {
204                 fw.usb.os_cfg_desc = &usb_config_fullspeed;
205                 fw.usb.cfg_desc = &usb_config_highspeed;
206
207                 /* 512 Byte DMA transfers */
208                 usb_dma_ctrl |= AR9170_USB_DMA_CTL_HIGH_SPEED;
209         } else {
210                 fw.usb.cfg_desc = &usb_config_fullspeed;
211                 fw.usb.os_cfg_desc = &usb_config_highspeed;
212         }
213
214 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
215 # if (CONFIG_CARL9170FW_RX_FRAME_LEN == 4096)
216         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_4K;
217 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 8192)
218         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_8K;
219 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 16384)
220         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_16K;
221 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 32768)
222         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_32K;
223 # else
224 #       error "Invalid AR9170_RX_FRAME_LEN setting"
225 # endif
226
227 #else /* CONFIG_CARL9170FW_USB_UP_STREAM */
228         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_PACKET_MODE;
229 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
230
231 #ifdef CONFIG_CARL9170FW_USB_DOWN_STREAM
232         /* Enable down stream mode */
233         usb_dma_ctrl |= AR9170_USB_DMA_CTL_DOWN_STREAM;
234 #endif /* CONFIG_CARL9170FW_USB_DOWN_STREAM */
235
236 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
237         /* Set the up stream mode maximum aggregate number */
238         set(AR9170_USB_REG_MAX_AGG_UPLOAD, 4);
239
240         /*
241          * Set the up stream mode timeout value.
242          * NB: The vendor driver (otus) set 0x80?
243          */
244         set(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
245 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
246
247         /* Enable up stream and down stream */
248         usb_dma_ctrl |= AR9170_USB_DMA_CTL_ENABLE_TO_DEVICE |
249             AR9170_USB_DMA_CTL_ENABLE_FROM_DEVICE;
250
251         set(AR9170_USB_REG_DMA_CTL, usb_dma_ctrl);
252 }
253
254 void usb_init(void)
255 {
256         usb_pta_init();
257
258         fw.usb.config = 1;
259         /*
260          * The fw structure is always initialized with "0"
261          * during boot(); No need to waste precious bytes here.
262          *
263          * fw.usb.interface_setting = 0;
264          * fw.usb.alternate_interface_setting = 0;
265          * fw.usb.device_feature = 0;
266          */
267
268 #ifdef CONFIG_CARL9170FW_WOL
269         fw.usb.device_feature |= USB_DEVICE_REMOTE_WAKEUP;
270         usb_enable_remote_wakeup();
271 #endif /* CONFIG_CARL9170FW_WOL */
272 }
273
274 #define GET_ARRAY(a, o) ((uint32_t *) (((unsigned long) data) + offset))
275
276 static void usb_ep0rx_data(const void *data, const unsigned int len)
277 {
278         unsigned int offset;
279         uint32_t value;
280
281         BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
282         BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
283
284         for (offset = 0; offset < ((len + 3) & ~3); offset += 4) {
285                 value = get(AR9170_USB_REG_EP0_DATA);
286                 memcpy(GET_ARRAY(data, offset), &value,
287                        min(len - offset, (unsigned int)4));
288         }
289 }
290
291 static int usb_ep0tx_data(const void *data, const unsigned int len)
292 {
293         unsigned int offset = 0, block, last_block = 0;
294         uint32_t value;
295
296         BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
297         BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
298
299         block = min(len, (unsigned int) 4);
300         offset = 0;
301         while (offset < len) {
302
303                 if (last_block != block || block < 4)
304                         setb(AR9170_USB_REG_FIFO_SIZE, (1 << block) - 1);
305
306                 memcpy(&value, GET_ARRAY(data, offset), block);
307
308                 set(AR9170_USB_REG_EP0_DATA, value);
309
310                 offset += block;
311                 last_block = block = min(len - offset, (unsigned int) 4);
312         }
313
314         setb(AR9170_USB_REG_FIFO_SIZE, 0xf);
315
316         /* this will push the data to the host */
317         return 1;
318 }
319 #undef GET_ARRAY
320
321 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
322 static int usb_get_status(const struct usb_ctrlrequest *ctrl)
323 {
324         __le16 status = cpu_to_le16(fw.usb.device_feature);
325
326         if ((ctrl->bRequestType & USB_DIR_MASK) != USB_DIR_IN)
327                 return -1;
328
329         switch (ctrl->bRequestType & USB_RECIP_MASK) {
330         case USB_RECIP_DEVICE:
331                 status &= cpu_to_le16(~USB_DEVICE_SELF_POWERED);
332                 status &= cpu_to_le16(~USB_DEVICE_REMOTE_WAKEUP);
333                 break;
334
335         case USB_RECIP_INTERFACE:
336                 /* USB spec: This is reserved for future use. */
337                 status = cpu_to_le16(0);
338                 break;
339
340         case USB_RECIP_ENDPOINT:
341         case USB_RECIP_OTHER:
342         default:
343                 break;
344         }
345
346         return usb_ep0tx_data((const void *) &status, sizeof(status));
347 }
348
349 static int usb_get_string_desc(const struct usb_ctrlrequest *ctrl)
350 {
351         const struct usb_string_descriptor *string_desc = NULL;
352
353         switch (le16_to_cpu(ctrl->wValue) & 0xff) {
354         case 0x00:
355                 string_desc = (const struct usb_string_descriptor *)
356                         rom.hw.usb.string0_desc;
357                 break;
358
359         case 0x10:
360                 string_desc = (const struct usb_string_descriptor *)
361                         rom.hw.usb.string1_desc;
362                 break;
363
364         case 0x20:
365                 string_desc = (const struct usb_string_descriptor *)
366                         rom.hw.usb.string2_desc;
367                 break;
368
369         case 0x30:
370                 string_desc = (const struct usb_string_descriptor *)
371                         rom.hw.usb.string3_desc;
372                 break;
373
374         default:
375                 break;
376         }
377
378         if (string_desc)
379                 return usb_ep0tx_data(string_desc, string_desc->bLength);
380
381         return -1;
382 }
383
384 static int usb_get_device_desc(const struct usb_ctrlrequest *ctrl __unused)
385 {
386         return usb_ep0tx_data(&rom.hw.usb.device_desc,
387                               rom.hw.usb.device_desc.bLength);
388 }
389
390 static int usb_get_config_desc(const struct usb_ctrlrequest *ctrl __unused)
391 {
392         fw.usb.cfg_desc->cfg.bDescriptorType = USB_DT_CONFIG;
393
394         return usb_ep0tx_data(fw.usb.cfg_desc,
395                 le16_to_cpu(fw.usb.cfg_desc->cfg.wTotalLength));
396 }
397
398 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
399 static int usb_get_otherspeed_desc(const struct usb_ctrlrequest *ctrl __unused)
400 {
401
402         fw.usb.os_cfg_desc->cfg.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
403
404         return usb_ep0tx_data(fw.usb.os_cfg_desc,
405                 le16_to_cpu(fw.usb.os_cfg_desc->cfg.wTotalLength));
406 }
407 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
408
409 static int usb_get_qualifier_desc(const struct usb_ctrlrequest *ctrl __unused)
410 {
411         struct usb_qualifier_descriptor qual;
412
413         /*
414          * The qualifier descriptor shares some structural details
415          * with the main device descriptor.
416          */
417
418         memcpy(&qual, &rom.hw.usb.device_desc, sizeof(qual));
419
420         /* (Re)-Initialize fields */
421         qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
422         qual.bLength = sizeof(qual);
423         qual.bNumConfigurations = rom.hw.usb.device_desc.bNumConfigurations;
424         qual.bRESERVED = 0;
425
426         return usb_ep0tx_data(&qual, qual.bLength);
427 }
428
429 #define USB_CHECK_REQTYPE(ctrl, recip, dir)                     \
430         (((ctrl->bRequestType & USB_RECIP_MASK) != recip) ||    \
431          ((ctrl->bRequestType & USB_DIR_MASK) != dir))
432
433 static int usb_get_descriptor(const struct usb_ctrlrequest *ctrl)
434 {
435         int status = -1;
436
437         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
438                 return status;
439
440         switch (le16_to_cpu(ctrl->wValue) >> 8) {
441         case USB_DT_DEVICE:
442                 status = usb_get_device_desc(ctrl);
443                 break;
444
445         case USB_DT_CONFIG:
446                 status = usb_get_config_desc(ctrl);
447                 break;
448
449         case USB_DT_STRING:
450                 status = usb_get_string_desc(ctrl);
451                 break;
452
453         case USB_DT_INTERFACE:
454                 break;
455
456         case USB_DT_ENDPOINT:
457                 break;
458
459         case USB_DT_DEVICE_QUALIFIER:
460                 status = usb_get_qualifier_desc(ctrl);
461                 break;
462
463 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
464         case USB_DT_OTHER_SPEED_CONFIG:
465                 status = usb_get_otherspeed_desc(ctrl);
466                 break;
467 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
468         default:
469                 break;
470
471         }
472
473         return status;
474 }
475
476 static int usb_get_configuration(const struct usb_ctrlrequest *ctrl)
477 {
478         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
479                 return -1;
480
481         return usb_ep0tx_data(&fw.usb.config, 1);
482 }
483
484 static int usb_set_configuration(const struct usb_ctrlrequest *ctrl)
485 {
486         unsigned int config;
487
488         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
489                 return -1;
490
491         config = le16_to_cpu(ctrl->wValue);
492         switch (config) {
493         case 0:
494                 /* Disable Device */
495                 andb(AR9170_USB_REG_DEVICE_ADDRESS,
496                       (uint8_t) ~(AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
497 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
498         case 1:
499                 fw.usb.config = config;
500
501                 if (usb_detect_highspeed()) {
502                         /* High Speed Configuration */
503                         usb_init_highspeed_fifo_cfg();
504                 } else {
505                         /* Full Speed Configuration */
506                         usb_init_fullspeed_fifo_cfg();
507                 }
508                 break;
509
510         default:
511                 return -1;
512         }
513         /* usb_pta_init() ? */
514
515         usb_reset_eps();
516         orb(AR9170_USB_REG_DEVICE_ADDRESS,
517             (AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
518
519         usb_enable_global_int();
520         usb_trigger_out();
521         return 1;
522 #else
523         default:
524                 return -1;
525         }
526 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
527 }
528
529 static int usb_set_address(const struct usb_ctrlrequest *ctrl)
530 {
531         unsigned int address;
532
533         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
534                 return -1;
535
536         address = le16_to_cpu(ctrl->wValue);
537
538         /*
539          * The original firmware used 0x100 (which is, of course,
540          * too big to fit into uint8_t).
541          * However based on the available information (hw.h), BIT(7)
542          * is used as some sort of flag and should not be
543          * part of the device address.
544          */
545         if (address >= BIT(7))
546                 return -1;
547
548         setb(AR9170_USB_REG_DEVICE_ADDRESS, (uint8_t) address);
549         return 1;
550 }
551
552 static int usb_get_interface(const struct usb_ctrlrequest *ctrl)
553 {
554         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_IN))
555                 return -1;
556
557         if (usb_configured() == false)
558                 return -1;
559
560         switch (fw.usb.config) {
561         case 1:
562                 break;
563
564         default:
565                 return -1;
566         }
567
568         return usb_ep0tx_data(&fw.usb.alternate_interface_setting, 1);
569 }
570
571 static int usb_manipulate_feature(const struct usb_ctrlrequest *ctrl, bool __unused clear)
572 {
573         unsigned int feature;
574         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
575                 return -1;
576
577         if (usb_configured() == false)
578                 return -1;
579
580         feature = le16_to_cpu(ctrl->wValue);
581
582 #ifdef CONFIG_CARL9170FW_WOL
583         if (feature & USB_DEVICE_REMOTE_WAKEUP) {
584                 if (clear)
585                         usb_disable_remote_wakeup();
586                 else
587                         usb_enable_remote_wakeup();
588         }
589 #endif /* CONFIG_CARL9170FW_WOL */
590
591         if (clear)
592                 fw.usb.device_feature &= ~feature;
593         else
594                 fw.usb.device_feature |= feature;
595
596         return 1;
597 }
598
599 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
600 static int usb_set_interface(const struct usb_ctrlrequest *ctrl)
601 {
602         unsigned int intf, alt_intf;
603         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_OUT))
604                 return -1;
605
606         if (usb_configured() == false)
607                 return -1;
608
609         intf = le16_to_cpu(ctrl->wIndex);
610         alt_intf = le16_to_cpu(ctrl->wValue);
611
612         switch (intf) {
613         case 0:
614                 if (alt_intf != fw.usb.cfg_desc->intf.bAlternateSetting)
615                         return -1;
616
617                 fw.usb.interface_setting = (uint8_t) intf;
618                 fw.usb.alternate_interface_setting = (uint8_t) alt_intf;
619                 if (usb_detect_highspeed())
620                         usb_init_highspeed_fifo_cfg();
621                 else
622                         usb_init_fullspeed_fifo_cfg();
623
624                 usb_reset_eps();
625                 usb_enable_global_int();
626                 usb_trigger_out();
627                 return 1;
628
629         default:
630                 return -1;
631         }
632 }
633 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
634 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
635
636 static int usb_standard_command(const struct usb_ctrlrequest *ctrl __unused)
637 {
638         int status = -1;
639
640 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
641         switch (ctrl->bRequest) {
642         case USB_REQ_GET_STATUS:
643                 status = usb_get_status(ctrl);
644                 break;
645
646         case USB_REQ_CLEAR_FEATURE:
647         case USB_REQ_SET_FEATURE:
648                 usb_manipulate_feature(ctrl, ctrl->bRequest == USB_REQ_CLEAR_FEATURE);
649                 break;
650
651         case USB_REQ_SET_ADDRESS:
652                 status = usb_set_address(ctrl);
653                 break;
654
655         case USB_REQ_GET_DESCRIPTOR:
656                 status = usb_get_descriptor(ctrl);
657                 break;
658
659         case USB_REQ_SET_DESCRIPTOR:
660                 break;
661
662         case USB_REQ_GET_CONFIGURATION:
663                 status = usb_get_configuration(ctrl);
664                 break;
665
666         case USB_REQ_SET_CONFIGURATION:
667                 status = usb_set_configuration(ctrl);
668                 break;
669
670         case USB_REQ_GET_INTERFACE:
671                 status = usb_get_interface(ctrl);
672                 break;
673
674         case USB_REQ_SET_INTERFACE:
675 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
676                 status = usb_set_interface(ctrl);
677 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
678                 break;
679
680         case USB_REQ_SYNCH_FRAME:
681                 break;
682
683         default:
684                 break;
685
686         }
687 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
688
689         return status;
690 }
691
692 static int usb_class_command(const struct usb_ctrlrequest *ctrl __unused)
693 {
694         return -1;
695 }
696
697 static int usb_vendor_command(const struct usb_ctrlrequest *ctrl __unused)
698 {
699         /*
700          * Note: Firmware upload/boot is not implemented.
701          * It's impossible to replace the current image
702          * in place.
703          */
704
705         return -1;
706 }
707
708 #undef USB_CHECK_TYPE
709
710 void usb_ep0setup(void)
711 {
712         struct usb_ctrlrequest ctrl;
713         int status = -1;
714         usb_ep0rx_data(&ctrl, sizeof(ctrl));
715
716         switch (ctrl.bRequestType & USB_TYPE_MASK) {
717         case USB_TYPE_STANDARD:
718                 status = usb_standard_command(&ctrl);
719                 break;
720
721         case USB_TYPE_CLASS:
722                 status = usb_class_command(&ctrl);
723                 break;
724
725         case USB_TYPE_VENDOR:
726                 status = usb_vendor_command(&ctrl);
727                 break;
728
729         default:
730                 break;
731
732         }
733
734         if (status < 0)
735                 fw.usb.ep0_action |= CARL9170_EP0_STALL;
736 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
737         if (status > 0)
738                 fw.usb.ep0_action |= CARL9170_EP0_TRIGGER;
739 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
740 }
741
742 void usb_ep0rx(void)
743 {
744         if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
745                 return ;
746
747         usb_ep0rx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
748         fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;
749 }
750
751 void usb_ep0tx(void)
752 {
753         if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
754                 return ;
755
756         usb_ep0tx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
757         fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;
758 }