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