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