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