usbdev.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /*
  2. * BRIEF MODULE DESCRIPTION
  3. * Au1000 USB Device-Side (device layer)
  4. *
  5. * Copyright 2001-2002 MontaVista Software Inc.
  6. * Author: MontaVista Software, Inc.
  7. * stevel@mvista.com or source@mvista.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  17. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  20. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/ioport.h>
  31. #include <linux/sched.h>
  32. #include <linux/signal.h>
  33. #include <linux/errno.h>
  34. #include <linux/poll.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/module.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/list.h>
  41. #include <linux/smp_lock.h>
  42. #define DEBUG
  43. #include <linux/usb.h>
  44. #include <asm/io.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/irq.h>
  47. #include <asm/mipsregs.h>
  48. #include <asm/au1000.h>
  49. #include <asm/au1000_dma.h>
  50. #include <asm/au1000_usbdev.h>
  51. #ifdef DEBUG
  52. #undef VDEBUG
  53. #ifdef VDEBUG
  54. #define vdbg(fmt, arg...) printk(KERN_DEBUG __FILE__ ": " fmt "\n" , ## arg)
  55. #else
  56. #define vdbg(fmt, arg...) do {} while (0)
  57. #endif
  58. #else
  59. #define vdbg(fmt, arg...) do {} while (0)
  60. #endif
  61. #define ALLOC_FLAGS (in_interrupt () ? GFP_ATOMIC : GFP_KERNEL)
  62. #define EP_FIFO_DEPTH 8
  63. typedef enum {
  64. SETUP_STAGE = 0,
  65. DATA_STAGE,
  66. STATUS_STAGE
  67. } ep0_stage_t;
  68. typedef struct {
  69. int read_fifo;
  70. int write_fifo;
  71. int ctrl_stat;
  72. int read_fifo_status;
  73. int write_fifo_status;
  74. } endpoint_reg_t;
  75. typedef struct {
  76. usbdev_pkt_t *head;
  77. usbdev_pkt_t *tail;
  78. int count;
  79. } pkt_list_t;
  80. typedef struct {
  81. int active;
  82. struct usb_endpoint_descriptor *desc;
  83. endpoint_reg_t *reg;
  84. /* Only one of these are used, unless this is the control ep */
  85. pkt_list_t inlist;
  86. pkt_list_t outlist;
  87. unsigned int indma, outdma; /* DMA channel numbers for IN, OUT */
  88. /* following are extracted from endpoint descriptor for easy access */
  89. int max_pkt_size;
  90. int type;
  91. int direction;
  92. /* WE assign endpoint addresses! */
  93. int address;
  94. spinlock_t lock;
  95. } endpoint_t;
  96. static struct usb_dev {
  97. endpoint_t ep[6];
  98. ep0_stage_t ep0_stage;
  99. struct usb_device_descriptor * dev_desc;
  100. struct usb_interface_descriptor* if_desc;
  101. struct usb_config_descriptor * conf_desc;
  102. u8 * full_conf_desc;
  103. struct usb_string_descriptor * str_desc[6];
  104. /* callback to function layer */
  105. void (*func_cb)(usbdev_cb_type_t type, unsigned long arg,
  106. void *cb_data);
  107. void* cb_data;
  108. usbdev_state_t state; // device state
  109. int suspended; // suspended flag
  110. int address; // device address
  111. int interface;
  112. int num_ep;
  113. u8 alternate_setting;
  114. u8 configuration; // configuration value
  115. int remote_wakeup_en;
  116. } usbdev;
  117. static endpoint_reg_t ep_reg[] = {
  118. // FIFO's 0 and 1 are EP0 default control
  119. {USBD_EP0RD, USBD_EP0WR, USBD_EP0CS, USBD_EP0RDSTAT, USBD_EP0WRSTAT },
  120. {0},
  121. // FIFO 2 is EP2, IN
  122. { -1, USBD_EP2WR, USBD_EP2CS, -1, USBD_EP2WRSTAT },
  123. // FIFO 3 is EP3, IN
  124. { -1, USBD_EP3WR, USBD_EP3CS, -1, USBD_EP3WRSTAT },
  125. // FIFO 4 is EP4, OUT
  126. {USBD_EP4RD, -1, USBD_EP4CS, USBD_EP4RDSTAT, -1 },
  127. // FIFO 5 is EP5, OUT
  128. {USBD_EP5RD, -1, USBD_EP5CS, USBD_EP5RDSTAT, -1 }
  129. };
  130. static struct {
  131. unsigned int id;
  132. const char *str;
  133. } ep_dma_id[] = {
  134. { DMA_ID_USBDEV_EP0_TX, "USBDev EP0 IN" },
  135. { DMA_ID_USBDEV_EP0_RX, "USBDev EP0 OUT" },
  136. { DMA_ID_USBDEV_EP2_TX, "USBDev EP2 IN" },
  137. { DMA_ID_USBDEV_EP3_TX, "USBDev EP3 IN" },
  138. { DMA_ID_USBDEV_EP4_RX, "USBDev EP4 OUT" },
  139. { DMA_ID_USBDEV_EP5_RX, "USBDev EP5 OUT" }
  140. };
  141. #define DIR_OUT 0
  142. #define DIR_IN (1<<3)
  143. #define CONTROL_EP USB_ENDPOINT_XFER_CONTROL
  144. #define BULK_EP USB_ENDPOINT_XFER_BULK
  145. static inline endpoint_t *
  146. epaddr_to_ep(struct usb_dev* dev, int ep_addr)
  147. {
  148. if (ep_addr >= 0 && ep_addr < 2)
  149. return &dev->ep[0];
  150. if (ep_addr < 6)
  151. return &dev->ep[ep_addr];
  152. return NULL;
  153. }
  154. static const char* std_req_name[] = {
  155. "GET_STATUS",
  156. "CLEAR_FEATURE",
  157. "RESERVED",
  158. "SET_FEATURE",
  159. "RESERVED",
  160. "SET_ADDRESS",
  161. "GET_DESCRIPTOR",
  162. "SET_DESCRIPTOR",
  163. "GET_CONFIGURATION",
  164. "SET_CONFIGURATION",
  165. "GET_INTERFACE",
  166. "SET_INTERFACE",
  167. "SYNCH_FRAME"
  168. };
  169. static inline const char*
  170. get_std_req_name(int req)
  171. {
  172. return (req >= 0 && req <= 12) ? std_req_name[req] : "UNKNOWN";
  173. }
  174. #if 0
  175. static void
  176. dump_setup(struct usb_ctrlrequest* s)
  177. {
  178. dbg("%s: requesttype=%d", __FUNCTION__, s->requesttype);
  179. dbg("%s: request=%d %s", __FUNCTION__, s->request,
  180. get_std_req_name(s->request));
  181. dbg("%s: value=0x%04x", __FUNCTION__, s->wValue);
  182. dbg("%s: index=%d", __FUNCTION__, s->index);
  183. dbg("%s: length=%d", __FUNCTION__, s->length);
  184. }
  185. #endif
  186. static inline usbdev_pkt_t *
  187. alloc_packet(endpoint_t * ep, int data_size, void* data)
  188. {
  189. usbdev_pkt_t* pkt = kmalloc(sizeof(usbdev_pkt_t) + data_size,
  190. ALLOC_FLAGS);
  191. if (!pkt)
  192. return NULL;
  193. pkt->ep_addr = ep->address;
  194. pkt->size = data_size;
  195. pkt->status = 0;
  196. pkt->next = NULL;
  197. if (data)
  198. memcpy(pkt->payload, data, data_size);
  199. return pkt;
  200. }
  201. /*
  202. * Link a packet to the tail of the enpoint's packet list.
  203. * EP spinlock must be held when calling.
  204. */
  205. static void
  206. link_tail(endpoint_t * ep, pkt_list_t * list, usbdev_pkt_t * pkt)
  207. {
  208. if (!list->tail) {
  209. list->head = list->tail = pkt;
  210. list->count = 1;
  211. } else {
  212. list->tail->next = pkt;
  213. list->tail = pkt;
  214. list->count++;
  215. }
  216. }
  217. /*
  218. * Unlink and return a packet from the head of the given packet
  219. * list. It is the responsibility of the caller to free the packet.
  220. * EP spinlock must be held when calling.
  221. */
  222. static usbdev_pkt_t *
  223. unlink_head(pkt_list_t * list)
  224. {
  225. usbdev_pkt_t *pkt;
  226. pkt = list->head;
  227. if (!pkt || !list->count) {
  228. return NULL;
  229. }
  230. list->head = pkt->next;
  231. if (!list->head) {
  232. list->head = list->tail = NULL;
  233. list->count = 0;
  234. } else
  235. list->count--;
  236. return pkt;
  237. }
  238. /*
  239. * Create and attach a new packet to the tail of the enpoint's
  240. * packet list. EP spinlock must be held when calling.
  241. */
  242. static usbdev_pkt_t *
  243. add_packet(endpoint_t * ep, pkt_list_t * list, int size)
  244. {
  245. usbdev_pkt_t *pkt = alloc_packet(ep, size, NULL);
  246. if (!pkt)
  247. return NULL;
  248. link_tail(ep, list, pkt);
  249. return pkt;
  250. }
  251. /*
  252. * Unlink and free a packet from the head of the enpoint's
  253. * packet list. EP spinlock must be held when calling.
  254. */
  255. static inline void
  256. free_packet(pkt_list_t * list)
  257. {
  258. kfree(unlink_head(list));
  259. }
  260. /* EP spinlock must be held when calling. */
  261. static inline void
  262. flush_pkt_list(pkt_list_t * list)
  263. {
  264. while (list->count)
  265. free_packet(list);
  266. }
  267. /* EP spinlock must be held when calling */
  268. static inline void
  269. flush_write_fifo(endpoint_t * ep)
  270. {
  271. if (ep->reg->write_fifo_status >= 0) {
  272. au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF |
  273. USBDEV_FSTAT_OF,
  274. ep->reg->write_fifo_status);
  275. //udelay(100);
  276. //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF,
  277. // ep->reg->write_fifo_status);
  278. }
  279. }
  280. /* EP spinlock must be held when calling */
  281. static inline void
  282. flush_read_fifo(endpoint_t * ep)
  283. {
  284. if (ep->reg->read_fifo_status >= 0) {
  285. au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF |
  286. USBDEV_FSTAT_OF,
  287. ep->reg->read_fifo_status);
  288. //udelay(100);
  289. //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF,
  290. // ep->reg->read_fifo_status);
  291. }
  292. }
  293. /* EP spinlock must be held when calling. */
  294. static void
  295. endpoint_flush(endpoint_t * ep)
  296. {
  297. // First, flush all packets
  298. flush_pkt_list(&ep->inlist);
  299. flush_pkt_list(&ep->outlist);
  300. // Now flush the endpoint's h/w FIFO(s)
  301. flush_write_fifo(ep);
  302. flush_read_fifo(ep);
  303. }
  304. /* EP spinlock must be held when calling. */
  305. static void
  306. endpoint_stall(endpoint_t * ep)
  307. {
  308. u32 cs;
  309. warn(__FUNCTION__);
  310. cs = au_readl(ep->reg->ctrl_stat) | USBDEV_CS_STALL;
  311. au_writel(cs, ep->reg->ctrl_stat);
  312. }
  313. /* EP spinlock must be held when calling. */
  314. static void
  315. endpoint_unstall(endpoint_t * ep)
  316. {
  317. u32 cs;
  318. warn(__FUNCTION__);
  319. cs = au_readl(ep->reg->ctrl_stat) & ~USBDEV_CS_STALL;
  320. au_writel(cs, ep->reg->ctrl_stat);
  321. }
  322. static void
  323. endpoint_reset_datatoggle(endpoint_t * ep)
  324. {
  325. // FIXME: is this possible?
  326. }
  327. /* EP spinlock must be held when calling. */
  328. static int
  329. endpoint_fifo_read(endpoint_t * ep)
  330. {
  331. int read_count = 0;
  332. u8 *bufptr;
  333. usbdev_pkt_t *pkt = ep->outlist.tail;
  334. if (!pkt)
  335. return -EINVAL;
  336. bufptr = &pkt->payload[pkt->size];
  337. while (au_readl(ep->reg->read_fifo_status) & USBDEV_FSTAT_FCNT_MASK) {
  338. *bufptr++ = au_readl(ep->reg->read_fifo) & 0xff;
  339. read_count++;
  340. pkt->size++;
  341. }
  342. return read_count;
  343. }
  344. #if 0
  345. /* EP spinlock must be held when calling. */
  346. static int
  347. endpoint_fifo_write(endpoint_t * ep, int index)
  348. {
  349. int write_count = 0;
  350. u8 *bufptr;
  351. usbdev_pkt_t *pkt = ep->inlist.head;
  352. if (!pkt)
  353. return -EINVAL;
  354. bufptr = &pkt->payload[index];
  355. while ((au_readl(ep->reg->write_fifo_status) &
  356. USBDEV_FSTAT_FCNT_MASK) < EP_FIFO_DEPTH) {
  357. if (bufptr < pkt->payload + pkt->size) {
  358. au_writel(*bufptr++, ep->reg->write_fifo);
  359. write_count++;
  360. } else {
  361. break;
  362. }
  363. }
  364. return write_count;
  365. }
  366. #endif
  367. /*
  368. * This routine is called to restart transmission of a packet.
  369. * The endpoint's TSIZE must be set to the new packet's size,
  370. * and DMA to the write FIFO needs to be restarted.
  371. * EP spinlock must be held when calling.
  372. */
  373. static void
  374. kickstart_send_packet(endpoint_t * ep)
  375. {
  376. u32 cs;
  377. usbdev_pkt_t *pkt = ep->inlist.head;
  378. vdbg("%s: ep%d, pkt=%p", __FUNCTION__, ep->address, pkt);
  379. if (!pkt) {
  380. err("%s: head=NULL! list->count=%d", __FUNCTION__,
  381. ep->inlist.count);
  382. return;
  383. }
  384. dma_cache_wback_inv((unsigned long)pkt->payload, pkt->size);
  385. /*
  386. * make sure FIFO is empty
  387. */
  388. flush_write_fifo(ep);
  389. cs = au_readl(ep->reg->ctrl_stat) & USBDEV_CS_STALL;
  390. cs |= (pkt->size << USBDEV_CS_TSIZE_BIT);
  391. au_writel(cs, ep->reg->ctrl_stat);
  392. if (get_dma_active_buffer(ep->indma) == 1) {
  393. set_dma_count1(ep->indma, pkt->size);
  394. set_dma_addr1(ep->indma, virt_to_phys(pkt->payload));
  395. enable_dma_buffer1(ep->indma); // reenable
  396. } else {
  397. set_dma_count0(ep->indma, pkt->size);
  398. set_dma_addr0(ep->indma, virt_to_phys(pkt->payload));
  399. enable_dma_buffer0(ep->indma); // reenable
  400. }
  401. if (dma_halted(ep->indma))
  402. start_dma(ep->indma);
  403. }
  404. /*
  405. * This routine is called when a packet in the inlist has been
  406. * completed. Frees the completed packet and starts sending the
  407. * next. EP spinlock must be held when calling.
  408. */
  409. static usbdev_pkt_t *
  410. send_packet_complete(endpoint_t * ep)
  411. {
  412. usbdev_pkt_t *pkt = unlink_head(&ep->inlist);
  413. if (pkt) {
  414. pkt->status =
  415. (au_readl(ep->reg->ctrl_stat) & USBDEV_CS_NAK) ?
  416. PKT_STATUS_NAK : PKT_STATUS_ACK;
  417. vdbg("%s: ep%d, %s pkt=%p, list count=%d", __FUNCTION__,
  418. ep->address, (pkt->status & PKT_STATUS_NAK) ?
  419. "NAK" : "ACK", pkt, ep->inlist.count);
  420. }
  421. /*
  422. * The write fifo should already be drained if things are
  423. * working right, but flush it anyway just in case.
  424. */
  425. flush_write_fifo(ep);
  426. // begin transmitting next packet in the inlist
  427. if (ep->inlist.count) {
  428. kickstart_send_packet(ep);
  429. }
  430. return pkt;
  431. }
  432. /*
  433. * Add a new packet to the tail of the given ep's packet
  434. * inlist. The transmit complete interrupt frees packets from
  435. * the head of this list. EP spinlock must be held when calling.
  436. */
  437. static int
  438. send_packet(struct usb_dev* dev, usbdev_pkt_t *pkt, int async)
  439. {
  440. pkt_list_t *list;
  441. endpoint_t* ep;
  442. if (!pkt || !(ep = epaddr_to_ep(dev, pkt->ep_addr)))
  443. return -EINVAL;
  444. if (!pkt->size)
  445. return 0;
  446. list = &ep->inlist;
  447. if (!async && list->count) {
  448. halt_dma(ep->indma);
  449. flush_pkt_list(list);
  450. }
  451. link_tail(ep, list, pkt);
  452. vdbg("%s: ep%d, pkt=%p, size=%d, list count=%d", __FUNCTION__,
  453. ep->address, pkt, pkt->size, list->count);
  454. if (list->count == 1) {
  455. /*
  456. * if the packet count is one, it means the list was empty,
  457. * and no more data will go out this ep until we kick-start
  458. * it again.
  459. */
  460. kickstart_send_packet(ep);
  461. }
  462. return pkt->size;
  463. }
  464. /*
  465. * This routine is called to restart reception of a packet.
  466. * EP spinlock must be held when calling.
  467. */
  468. static void
  469. kickstart_receive_packet(endpoint_t * ep)
  470. {
  471. usbdev_pkt_t *pkt;
  472. // get and link a new packet for next reception
  473. if (!(pkt = add_packet(ep, &ep->outlist, ep->max_pkt_size))) {
  474. err("%s: could not alloc new packet", __FUNCTION__);
  475. return;
  476. }
  477. if (get_dma_active_buffer(ep->outdma) == 1) {
  478. clear_dma_done1(ep->outdma);
  479. set_dma_count1(ep->outdma, ep->max_pkt_size);
  480. set_dma_count0(ep->outdma, 0);
  481. set_dma_addr1(ep->outdma, virt_to_phys(pkt->payload));
  482. enable_dma_buffer1(ep->outdma); // reenable
  483. } else {
  484. clear_dma_done0(ep->outdma);
  485. set_dma_count0(ep->outdma, ep->max_pkt_size);
  486. set_dma_count1(ep->outdma, 0);
  487. set_dma_addr0(ep->outdma, virt_to_phys(pkt->payload));
  488. enable_dma_buffer0(ep->outdma); // reenable
  489. }
  490. if (dma_halted(ep->outdma))
  491. start_dma(ep->outdma);
  492. }
  493. /*
  494. * This routine is called when a packet in the outlist has been
  495. * completed (received) and we need to prepare for a new packet
  496. * to be received. Halts DMA and computes the packet size from the
  497. * remaining DMA counter. Then prepares a new packet for reception
  498. * and restarts DMA. FIXME: what if another packet comes in
  499. * on top of the completed packet? Counter would be wrong.
  500. * EP spinlock must be held when calling.
  501. */
  502. static usbdev_pkt_t *
  503. receive_packet_complete(endpoint_t * ep)
  504. {
  505. usbdev_pkt_t *pkt = ep->outlist.tail;
  506. u32 cs;
  507. halt_dma(ep->outdma);
  508. cs = au_readl(ep->reg->ctrl_stat);
  509. if (!pkt)
  510. return NULL;
  511. pkt->size = ep->max_pkt_size - get_dma_residue(ep->outdma);
  512. if (pkt->size)
  513. dma_cache_inv((unsigned long)pkt->payload, pkt->size);
  514. /*
  515. * need to pull out any remaining bytes in the FIFO.
  516. */
  517. endpoint_fifo_read(ep);
  518. /*
  519. * should be drained now, but flush anyway just in case.
  520. */
  521. flush_read_fifo(ep);
  522. pkt->status = (cs & USBDEV_CS_NAK) ? PKT_STATUS_NAK : PKT_STATUS_ACK;
  523. if (ep->address == 0 && (cs & USBDEV_CS_SU))
  524. pkt->status |= PKT_STATUS_SU;
  525. vdbg("%s: ep%d, %s pkt=%p, size=%d", __FUNCTION__,
  526. ep->address, (pkt->status & PKT_STATUS_NAK) ?
  527. "NAK" : "ACK", pkt, pkt->size);
  528. kickstart_receive_packet(ep);
  529. return pkt;
  530. }
  531. /*
  532. ****************************************************************************
  533. * Here starts the standard device request handlers. They are
  534. * all called by do_setup() via a table of function pointers.
  535. ****************************************************************************
  536. */
  537. static ep0_stage_t
  538. do_get_status(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  539. {
  540. switch (setup->bRequestType) {
  541. case 0x80: // Device
  542. // FIXME: send device status
  543. break;
  544. case 0x81: // Interface
  545. // FIXME: send interface status
  546. break;
  547. case 0x82: // End Point
  548. // FIXME: send endpoint status
  549. break;
  550. default:
  551. // Invalid Command
  552. endpoint_stall(&dev->ep[0]); // Stall End Point 0
  553. break;
  554. }
  555. return STATUS_STAGE;
  556. }
  557. static ep0_stage_t
  558. do_clear_feature(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  559. {
  560. switch (setup->bRequestType) {
  561. case 0x00: // Device
  562. if ((le16_to_cpu(setup->wValue) & 0xff) == 1)
  563. dev->remote_wakeup_en = 0;
  564. else
  565. endpoint_stall(&dev->ep[0]);
  566. break;
  567. case 0x02: // End Point
  568. if ((le16_to_cpu(setup->wValue) & 0xff) == 0) {
  569. endpoint_t *ep =
  570. epaddr_to_ep(dev,
  571. le16_to_cpu(setup->wIndex) & 0xff);
  572. endpoint_unstall(ep);
  573. endpoint_reset_datatoggle(ep);
  574. } else
  575. endpoint_stall(&dev->ep[0]);
  576. break;
  577. }
  578. return SETUP_STAGE;
  579. }
  580. static ep0_stage_t
  581. do_reserved(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  582. {
  583. // Invalid request, stall End Point 0
  584. endpoint_stall(&dev->ep[0]);
  585. return SETUP_STAGE;
  586. }
  587. static ep0_stage_t
  588. do_set_feature(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  589. {
  590. switch (setup->bRequestType) {
  591. case 0x00: // Device
  592. if ((le16_to_cpu(setup->wValue) & 0xff) == 1)
  593. dev->remote_wakeup_en = 1;
  594. else
  595. endpoint_stall(&dev->ep[0]);
  596. break;
  597. case 0x02: // End Point
  598. if ((le16_to_cpu(setup->wValue) & 0xff) == 0) {
  599. endpoint_t *ep =
  600. epaddr_to_ep(dev,
  601. le16_to_cpu(setup->wIndex) & 0xff);
  602. endpoint_stall(ep);
  603. } else
  604. endpoint_stall(&dev->ep[0]);
  605. break;
  606. }
  607. return SETUP_STAGE;
  608. }
  609. static ep0_stage_t
  610. do_set_address(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  611. {
  612. int new_state = dev->state;
  613. int new_addr = le16_to_cpu(setup->wValue);
  614. dbg("%s: our address=%d", __FUNCTION__, new_addr);
  615. if (new_addr > 127) {
  616. // usb spec doesn't tell us what to do, so just go to
  617. // default state
  618. new_state = DEFAULT;
  619. dev->address = 0;
  620. } else if (dev->address != new_addr) {
  621. dev->address = new_addr;
  622. new_state = ADDRESS;
  623. }
  624. if (dev->state != new_state) {
  625. dev->state = new_state;
  626. /* inform function layer of usbdev state change */
  627. dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data);
  628. }
  629. return SETUP_STAGE;
  630. }
  631. static ep0_stage_t
  632. do_get_descriptor(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  633. {
  634. int strnum, desc_len = le16_to_cpu(setup->wLength);
  635. switch (le16_to_cpu(setup->wValue) >> 8) {
  636. case USB_DT_DEVICE:
  637. // send device descriptor!
  638. desc_len = desc_len > dev->dev_desc->bLength ?
  639. dev->dev_desc->bLength : desc_len;
  640. dbg("sending device desc, size=%d", desc_len);
  641. send_packet(dev, alloc_packet(&dev->ep[0], desc_len,
  642. dev->dev_desc), 0);
  643. break;
  644. case USB_DT_CONFIG:
  645. // If the config descr index in low-byte of
  646. // setup->wValue is valid, send config descr,
  647. // otherwise stall ep0.
  648. if ((le16_to_cpu(setup->wValue) & 0xff) == 0) {
  649. // send config descriptor!
  650. if (desc_len <= USB_DT_CONFIG_SIZE) {
  651. dbg("sending partial config desc, size=%d",
  652. desc_len);
  653. send_packet(dev,
  654. alloc_packet(&dev->ep[0],
  655. desc_len,
  656. dev->conf_desc),
  657. 0);
  658. } else {
  659. int len = le16_to_cpu(dev->conf_desc->wTotalLength);
  660. dbg("sending whole config desc,"
  661. " size=%d, our size=%d", desc_len, len);
  662. desc_len = desc_len > len ? len : desc_len;
  663. send_packet(dev,
  664. alloc_packet(&dev->ep[0],
  665. desc_len,
  666. dev->full_conf_desc),
  667. 0);
  668. }
  669. } else
  670. endpoint_stall(&dev->ep[0]);
  671. break;
  672. case USB_DT_STRING:
  673. // If the string descr index in low-byte of setup->wValue
  674. // is valid, send string descr, otherwise stall ep0.
  675. strnum = le16_to_cpu(setup->wValue) & 0xff;
  676. if (strnum >= 0 && strnum < 6) {
  677. struct usb_string_descriptor *desc =
  678. dev->str_desc[strnum];
  679. desc_len = desc_len > desc->bLength ?
  680. desc->bLength : desc_len;
  681. dbg("sending string desc %d", strnum);
  682. send_packet(dev,
  683. alloc_packet(&dev->ep[0], desc_len,
  684. desc), 0);
  685. } else
  686. endpoint_stall(&dev->ep[0]);
  687. break;
  688. default:
  689. // Invalid request
  690. err("invalid get desc=%d, stalled",
  691. le16_to_cpu(setup->wValue) >> 8);
  692. endpoint_stall(&dev->ep[0]); // Stall endpoint 0
  693. break;
  694. }
  695. return STATUS_STAGE;
  696. }
  697. static ep0_stage_t
  698. do_set_descriptor(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  699. {
  700. // TODO: implement
  701. // there will be an OUT data stage (the descriptor to set)
  702. return DATA_STAGE;
  703. }
  704. static ep0_stage_t
  705. do_get_configuration(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  706. {
  707. // send dev->configuration
  708. dbg("sending config");
  709. send_packet(dev, alloc_packet(&dev->ep[0], 1, &dev->configuration),
  710. 0);
  711. return STATUS_STAGE;
  712. }
  713. static ep0_stage_t
  714. do_set_configuration(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  715. {
  716. // set active config to low-byte of setup->wValue
  717. dev->configuration = le16_to_cpu(setup->wValue) & 0xff;
  718. dbg("set config, config=%d", dev->configuration);
  719. if (!dev->configuration && dev->state > DEFAULT) {
  720. dev->state = ADDRESS;
  721. /* inform function layer of usbdev state change */
  722. dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data);
  723. } else if (dev->configuration == 1) {
  724. dev->state = CONFIGURED;
  725. /* inform function layer of usbdev state change */
  726. dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data);
  727. } else {
  728. // FIXME: "respond with request error" - how?
  729. }
  730. return SETUP_STAGE;
  731. }
  732. static ep0_stage_t
  733. do_get_interface(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  734. {
  735. // interface must be zero.
  736. if ((le16_to_cpu(setup->wIndex) & 0xff) || dev->state == ADDRESS) {
  737. // FIXME: respond with "request error". how?
  738. } else if (dev->state == CONFIGURED) {
  739. // send dev->alternate_setting
  740. dbg("sending alt setting");
  741. send_packet(dev, alloc_packet(&dev->ep[0], 1,
  742. &dev->alternate_setting), 0);
  743. }
  744. return STATUS_STAGE;
  745. }
  746. static ep0_stage_t
  747. do_set_interface(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  748. {
  749. if (dev->state == ADDRESS) {
  750. // FIXME: respond with "request error". how?
  751. } else if (dev->state == CONFIGURED) {
  752. dev->interface = le16_to_cpu(setup->wIndex) & 0xff;
  753. dev->alternate_setting =
  754. le16_to_cpu(setup->wValue) & 0xff;
  755. // interface and alternate_setting must be zero
  756. if (dev->interface || dev->alternate_setting) {
  757. // FIXME: respond with "request error". how?
  758. }
  759. }
  760. return SETUP_STAGE;
  761. }
  762. static ep0_stage_t
  763. do_synch_frame(struct usb_dev* dev, struct usb_ctrlrequest* setup)
  764. {
  765. // TODO
  766. return SETUP_STAGE;
  767. }
  768. typedef ep0_stage_t (*req_method_t)(struct usb_dev* dev,
  769. struct usb_ctrlrequest* setup);
  770. /* Table of the standard device request handlers */
  771. static const req_method_t req_method[] = {
  772. do_get_status,
  773. do_clear_feature,
  774. do_reserved,
  775. do_set_feature,
  776. do_reserved,
  777. do_set_address,
  778. do_get_descriptor,
  779. do_set_descriptor,
  780. do_get_configuration,
  781. do_set_configuration,
  782. do_get_interface,
  783. do_set_interface,
  784. do_synch_frame
  785. };
  786. // SETUP packet request dispatcher
  787. static void
  788. do_setup (struct usb_dev* dev, struct usb_ctrlrequest* setup)
  789. {
  790. req_method_t m;
  791. dbg("%s: req %d %s", __FUNCTION__, setup->bRequestType,
  792. get_std_req_name(setup->bRequestType));
  793. if ((setup->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD ||
  794. (setup->bRequestType & USB_RECIP_MASK) != USB_RECIP_DEVICE) {
  795. err("%s: invalid requesttype 0x%02x", __FUNCTION__,
  796. setup->bRequestType);
  797. return;
  798. }
  799. if ((setup->bRequestType & 0x80) == USB_DIR_OUT && setup->wLength)
  800. dbg("%s: OUT phase! length=%d", __FUNCTION__, setup->wLength);
  801. if (setup->bRequestType < sizeof(req_method)/sizeof(req_method_t))
  802. m = req_method[setup->bRequestType];
  803. else
  804. m = do_reserved;
  805. dev->ep0_stage = (*m)(dev, setup);
  806. }
  807. /*
  808. * A SETUP, DATA0, or DATA1 packet has been received
  809. * on the default control endpoint's fifo.
  810. */
  811. static void
  812. process_ep0_receive (struct usb_dev* dev)
  813. {
  814. endpoint_t *ep0 = &dev->ep[0];
  815. usbdev_pkt_t *pkt;
  816. spin_lock(&ep0->lock);
  817. // complete packet and prepare a new packet
  818. pkt = receive_packet_complete(ep0);
  819. if (!pkt) {
  820. // FIXME: should put a warn/err here.
  821. spin_unlock(&ep0->lock);
  822. return;
  823. }
  824. // unlink immediately from endpoint.
  825. unlink_head(&ep0->outlist);
  826. // override current stage if h/w says it's a setup packet
  827. if (pkt->status & PKT_STATUS_SU)
  828. dev->ep0_stage = SETUP_STAGE;
  829. switch (dev->ep0_stage) {
  830. case SETUP_STAGE:
  831. vdbg("SU bit is %s in setup stage",
  832. (pkt->status & PKT_STATUS_SU) ? "set" : "not set");
  833. if (pkt->size == sizeof(struct usb_ctrlrequest)) {
  834. #ifdef VDEBUG
  835. if (pkt->status & PKT_STATUS_ACK)
  836. vdbg("received SETUP");
  837. else
  838. vdbg("received NAK SETUP");
  839. #endif
  840. do_setup(dev, (struct usb_ctrlrequest*)pkt->payload);
  841. } else
  842. err("%s: wrong size SETUP received", __FUNCTION__);
  843. break;
  844. case DATA_STAGE:
  845. /*
  846. * this setup has an OUT data stage. Of the standard
  847. * device requests, only set_descriptor has this stage,
  848. * so this packet is that descriptor. TODO: drop it for
  849. * now, set_descriptor not implemented.
  850. *
  851. * Need to place a byte in the write FIFO here, to prepare
  852. * to send a zero-length DATA ack packet to the host in the
  853. * STATUS stage.
  854. */
  855. au_writel(0, ep0->reg->write_fifo);
  856. dbg("received OUT stage DATAx on EP0, size=%d", pkt->size);
  857. dev->ep0_stage = SETUP_STAGE;
  858. break;
  859. case STATUS_STAGE:
  860. // this setup had an IN data stage, and host is ACK'ing
  861. // the packet we sent during that stage.
  862. if (pkt->size != 0)
  863. warn("received non-zero ACK on EP0??");
  864. #ifdef VDEBUG
  865. else
  866. vdbg("received ACK on EP0");
  867. #endif
  868. dev->ep0_stage = SETUP_STAGE;
  869. break;
  870. }
  871. spin_unlock(&ep0->lock);
  872. // we're done processing the packet, free it
  873. kfree(pkt);
  874. }
  875. /*
  876. * A DATA0/1 packet has been received on one of the OUT endpoints (4 or 5)
  877. */
  878. static void
  879. process_ep_receive (struct usb_dev* dev, endpoint_t *ep)
  880. {
  881. usbdev_pkt_t *pkt;
  882. spin_lock(&ep->lock);
  883. pkt = receive_packet_complete(ep);
  884. spin_unlock(&ep->lock);
  885. dev->func_cb(CB_PKT_COMPLETE, (unsigned long)pkt, dev->cb_data);
  886. }
  887. /* This ISR handles the receive complete and suspend events */
  888. static void
  889. req_sus_intr (int irq, void *dev_id, struct pt_regs *regs)
  890. {
  891. struct usb_dev *dev = (struct usb_dev *) dev_id;
  892. u32 status;
  893. status = au_readl(USBD_INTSTAT);
  894. au_writel(status, USBD_INTSTAT); // ack'em
  895. if (status & (1<<0))
  896. process_ep0_receive(dev);
  897. if (status & (1<<4))
  898. process_ep_receive(dev, &dev->ep[4]);
  899. if (status & (1<<5))
  900. process_ep_receive(dev, &dev->ep[5]);
  901. }
  902. /* This ISR handles the DMA done events on EP0 */
  903. static void
  904. dma_done_ep0_intr(int irq, void *dev_id, struct pt_regs *regs)
  905. {
  906. struct usb_dev *dev = (struct usb_dev *) dev_id;
  907. usbdev_pkt_t* pkt;
  908. endpoint_t *ep0 = &dev->ep[0];
  909. u32 cs0, buff_done;
  910. spin_lock(&ep0->lock);
  911. cs0 = au_readl(ep0->reg->ctrl_stat);
  912. // first check packet transmit done
  913. if ((buff_done = get_dma_buffer_done(ep0->indma)) != 0) {
  914. // transmitted a DATAx packet during DATA stage
  915. // on control endpoint 0
  916. // clear DMA done bit
  917. if (buff_done & DMA_D0)
  918. clear_dma_done0(ep0->indma);
  919. if (buff_done & DMA_D1)
  920. clear_dma_done1(ep0->indma);
  921. pkt = send_packet_complete(ep0);
  922. if (pkt)
  923. kfree(pkt);
  924. }
  925. /*
  926. * Now check packet receive done. Shouldn't get these,
  927. * the receive packet complete intr should happen
  928. * before the DMA done intr occurs.
  929. */
  930. if ((buff_done = get_dma_buffer_done(ep0->outdma)) != 0) {
  931. // clear DMA done bit
  932. if (buff_done & DMA_D0)
  933. clear_dma_done0(ep0->outdma);
  934. if (buff_done & DMA_D1)
  935. clear_dma_done1(ep0->outdma);
  936. //process_ep0_receive(dev);
  937. }
  938. spin_unlock(&ep0->lock);
  939. }
  940. /* This ISR handles the DMA done events on endpoints 2,3,4,5 */
  941. static void
  942. dma_done_ep_intr(int irq, void *dev_id, struct pt_regs *regs)
  943. {
  944. struct usb_dev *dev = (struct usb_dev *) dev_id;
  945. int i;
  946. for (i = 2; i < 6; i++) {
  947. u32 buff_done;
  948. usbdev_pkt_t* pkt;
  949. endpoint_t *ep = &dev->ep[i];
  950. if (!ep->active) continue;
  951. spin_lock(&ep->lock);
  952. if (ep->direction == USB_DIR_IN) {
  953. buff_done = get_dma_buffer_done(ep->indma);
  954. if (buff_done != 0) {
  955. // transmitted a DATAx pkt on the IN ep
  956. // clear DMA done bit
  957. if (buff_done & DMA_D0)
  958. clear_dma_done0(ep->indma);
  959. if (buff_done & DMA_D1)
  960. clear_dma_done1(ep->indma);
  961. pkt = send_packet_complete(ep);
  962. spin_unlock(&ep->lock);
  963. dev->func_cb(CB_PKT_COMPLETE,
  964. (unsigned long)pkt,
  965. dev->cb_data);
  966. spin_lock(&ep->lock);
  967. }
  968. } else {
  969. /*
  970. * Check packet receive done (OUT ep). Shouldn't get
  971. * these, the rx packet complete intr should happen
  972. * before the DMA done intr occurs.
  973. */
  974. buff_done = get_dma_buffer_done(ep->outdma);
  975. if (buff_done != 0) {
  976. // received a DATAx pkt on the OUT ep
  977. // clear DMA done bit
  978. if (buff_done & DMA_D0)
  979. clear_dma_done0(ep->outdma);
  980. if (buff_done & DMA_D1)
  981. clear_dma_done1(ep->outdma);
  982. //process_ep_receive(dev, ep);
  983. }
  984. }
  985. spin_unlock(&ep->lock);
  986. }
  987. }
  988. /***************************************************************************
  989. * Here begins the external interface functions
  990. ***************************************************************************
  991. */
  992. /*
  993. * allocate a new packet
  994. */
  995. int
  996. usbdev_alloc_packet(int ep_addr, int data_size, usbdev_pkt_t** pkt)
  997. {
  998. endpoint_t * ep = epaddr_to_ep(&usbdev, ep_addr);
  999. usbdev_pkt_t* lpkt = NULL;
  1000. if (!ep || !ep->active || ep->address < 2)
  1001. return -ENODEV;
  1002. if (data_size > ep->max_pkt_size)
  1003. return -EINVAL;
  1004. lpkt = *pkt = alloc_packet(ep, data_size, NULL);
  1005. if (!lpkt)
  1006. return -ENOMEM;
  1007. return 0;
  1008. }
  1009. /*
  1010. * packet send
  1011. */
  1012. int
  1013. usbdev_send_packet(int ep_addr, usbdev_pkt_t * pkt)
  1014. {
  1015. unsigned long flags;
  1016. int count;
  1017. endpoint_t * ep;
  1018. if (!pkt || !(ep = epaddr_to_ep(&usbdev, pkt->ep_addr)) ||
  1019. !ep->active || ep->address < 2)
  1020. return -ENODEV;
  1021. if (ep->direction != USB_DIR_IN)
  1022. return -EINVAL;
  1023. spin_lock_irqsave(&ep->lock, flags);
  1024. count = send_packet(&usbdev, pkt, 1);
  1025. spin_unlock_irqrestore(&ep->lock, flags);
  1026. return count;
  1027. }
  1028. /*
  1029. * packet receive
  1030. */
  1031. int
  1032. usbdev_receive_packet(int ep_addr, usbdev_pkt_t** pkt)
  1033. {
  1034. unsigned long flags;
  1035. usbdev_pkt_t* lpkt = NULL;
  1036. endpoint_t *ep = epaddr_to_ep(&usbdev, ep_addr);
  1037. if (!ep || !ep->active || ep->address < 2)
  1038. return -ENODEV;
  1039. if (ep->direction != USB_DIR_OUT)
  1040. return -EINVAL;
  1041. spin_lock_irqsave(&ep->lock, flags);
  1042. if (ep->outlist.count > 1)
  1043. lpkt = unlink_head(&ep->outlist);
  1044. spin_unlock_irqrestore(&ep->lock, flags);
  1045. if (!lpkt) {
  1046. /* no packet available */
  1047. *pkt = NULL;
  1048. return -ENODATA;
  1049. }
  1050. *pkt = lpkt;
  1051. return lpkt->size;
  1052. }
  1053. /*
  1054. * return total queued byte count on the endpoint.
  1055. */
  1056. int
  1057. usbdev_get_byte_count(int ep_addr)
  1058. {
  1059. unsigned long flags;
  1060. pkt_list_t *list;
  1061. usbdev_pkt_t *scan;
  1062. int count = 0;
  1063. endpoint_t * ep = epaddr_to_ep(&usbdev, ep_addr);
  1064. if (!ep || !ep->active || ep->address < 2)
  1065. return -ENODEV;
  1066. if (ep->direction == USB_DIR_IN) {
  1067. list = &ep->inlist;
  1068. spin_lock_irqsave(&ep->lock, flags);
  1069. for (scan = list->head; scan; scan = scan->next)
  1070. count += scan->size;
  1071. spin_unlock_irqrestore(&ep->lock, flags);
  1072. } else {
  1073. list = &ep->outlist;
  1074. spin_lock_irqsave(&ep->lock, flags);
  1075. if (list->count > 1) {
  1076. for (scan = list->head; scan != list->tail;
  1077. scan = scan->next)
  1078. count += scan->size;
  1079. }
  1080. spin_unlock_irqrestore(&ep->lock, flags);
  1081. }
  1082. return count;
  1083. }
  1084. void
  1085. usbdev_exit(void)
  1086. {
  1087. endpoint_t *ep;
  1088. int i;
  1089. au_writel(0, USBD_INTEN); // disable usb dev ints
  1090. au_writel(0, USBD_ENABLE); // disable usb dev
  1091. free_irq(AU1000_USB_DEV_REQ_INT, &usbdev);
  1092. free_irq(AU1000_USB_DEV_SUS_INT, &usbdev);
  1093. // free all control endpoint resources
  1094. ep = &usbdev.ep[0];
  1095. free_au1000_dma(ep->indma);
  1096. free_au1000_dma(ep->outdma);
  1097. endpoint_flush(ep);
  1098. // free ep resources
  1099. for (i = 2; i < 6; i++) {
  1100. ep = &usbdev.ep[i];
  1101. if (!ep->active) continue;
  1102. if (ep->direction == USB_DIR_IN) {
  1103. free_au1000_dma(ep->indma);
  1104. } else {
  1105. free_au1000_dma(ep->outdma);
  1106. }
  1107. endpoint_flush(ep);
  1108. }
  1109. if (usbdev.full_conf_desc)
  1110. kfree(usbdev.full_conf_desc);
  1111. }
  1112. int
  1113. usbdev_init(struct usb_device_descriptor* dev_desc,
  1114. struct usb_config_descriptor* config_desc,
  1115. struct usb_interface_descriptor* if_desc,
  1116. struct usb_endpoint_descriptor* ep_desc,
  1117. struct usb_string_descriptor* str_desc[],
  1118. void (*cb)(usbdev_cb_type_t, unsigned long, void *),
  1119. void* cb_data)
  1120. {
  1121. endpoint_t *ep0;
  1122. int i, ret=0;
  1123. u8* fcd;
  1124. if (dev_desc->bNumConfigurations > 1 ||
  1125. config_desc->bNumInterfaces > 1 ||
  1126. if_desc->bNumEndpoints > 4) {
  1127. err("Only one config, one i/f, and no more "
  1128. "than 4 ep's allowed");
  1129. ret = -EINVAL;
  1130. goto out;
  1131. }
  1132. if (!cb) {
  1133. err("Function-layer callback required");
  1134. ret = -EINVAL;
  1135. goto out;
  1136. }
  1137. if (dev_desc->bMaxPacketSize0 != USBDEV_EP0_MAX_PACKET_SIZE) {
  1138. warn("EP0 Max Packet size must be %d",
  1139. USBDEV_EP0_MAX_PACKET_SIZE);
  1140. dev_desc->bMaxPacketSize0 = USBDEV_EP0_MAX_PACKET_SIZE;
  1141. }
  1142. memset(&usbdev, 0, sizeof(struct usb_dev));
  1143. usbdev.state = DEFAULT;
  1144. usbdev.dev_desc = dev_desc;
  1145. usbdev.if_desc = if_desc;
  1146. usbdev.conf_desc = config_desc;
  1147. for (i=0; i<6; i++)
  1148. usbdev.str_desc[i] = str_desc[i];
  1149. usbdev.func_cb = cb;
  1150. usbdev.cb_data = cb_data;
  1151. /* Initialize default control endpoint */
  1152. ep0 = &usbdev.ep[0];
  1153. ep0->active = 1;
  1154. ep0->type = CONTROL_EP;
  1155. ep0->max_pkt_size = USBDEV_EP0_MAX_PACKET_SIZE;
  1156. spin_lock_init(&ep0->lock);
  1157. ep0->desc = NULL; // ep0 has no descriptor
  1158. ep0->address = 0;
  1159. ep0->direction = 0;
  1160. ep0->reg = &ep_reg[0];
  1161. /* Initialize the other requested endpoints */
  1162. for (i = 0; i < if_desc->bNumEndpoints; i++) {
  1163. struct usb_endpoint_descriptor* epd = &ep_desc[i];
  1164. endpoint_t *ep;
  1165. if ((epd->bEndpointAddress & 0x80) == USB_DIR_IN) {
  1166. ep = &usbdev.ep[2];
  1167. ep->address = 2;
  1168. if (ep->active) {
  1169. ep = &usbdev.ep[3];
  1170. ep->address = 3;
  1171. if (ep->active) {
  1172. err("too many IN ep's requested");
  1173. ret = -ENODEV;
  1174. goto out;
  1175. }
  1176. }
  1177. } else {
  1178. ep = &usbdev.ep[4];
  1179. ep->address = 4;
  1180. if (ep->active) {
  1181. ep = &usbdev.ep[5];
  1182. ep->address = 5;
  1183. if (ep->active) {
  1184. err("too many OUT ep's requested");
  1185. ret = -ENODEV;
  1186. goto out;
  1187. }
  1188. }
  1189. }
  1190. ep->active = 1;
  1191. epd->bEndpointAddress &= ~0x0f;
  1192. epd->bEndpointAddress |= (u8)ep->address;
  1193. ep->direction = epd->bEndpointAddress & 0x80;
  1194. ep->type = epd->bmAttributes & 0x03;
  1195. ep->max_pkt_size = le16_to_cpu(epd->wMaxPacketSize);
  1196. spin_lock_init(&ep->lock);
  1197. ep->desc = epd;
  1198. ep->reg = &ep_reg[ep->address];
  1199. }
  1200. /*
  1201. * initialize the full config descriptor
  1202. */
  1203. usbdev.full_conf_desc = fcd = kmalloc(le16_to_cpu(config_desc->wTotalLength),
  1204. ALLOC_FLAGS);
  1205. if (!fcd) {
  1206. err("failed to alloc full config descriptor");
  1207. ret = -ENOMEM;
  1208. goto out;
  1209. }
  1210. memcpy(fcd, config_desc, USB_DT_CONFIG_SIZE);
  1211. fcd += USB_DT_CONFIG_SIZE;
  1212. memcpy(fcd, if_desc, USB_DT_INTERFACE_SIZE);
  1213. fcd += USB_DT_INTERFACE_SIZE;
  1214. for (i = 0; i < if_desc->bNumEndpoints; i++) {
  1215. memcpy(fcd, &ep_desc[i], USB_DT_ENDPOINT_SIZE);
  1216. fcd += USB_DT_ENDPOINT_SIZE;
  1217. }
  1218. /* Now we're ready to enable the controller */
  1219. au_writel(0x0002, USBD_ENABLE);
  1220. udelay(100);
  1221. au_writel(0x0003, USBD_ENABLE);
  1222. udelay(100);
  1223. /* build and send config table based on ep descriptors */
  1224. for (i = 0; i < 6; i++) {
  1225. endpoint_t *ep;
  1226. if (i == 1)
  1227. continue; // skip dummy ep
  1228. ep = &usbdev.ep[i];
  1229. if (ep->active) {
  1230. au_writel((ep->address << 4) | 0x04, USBD_CONFIG);
  1231. au_writel(((ep->max_pkt_size & 0x380) >> 7) |
  1232. (ep->direction >> 4) | (ep->type << 4),
  1233. USBD_CONFIG);
  1234. au_writel((ep->max_pkt_size & 0x7f) << 1, USBD_CONFIG);
  1235. au_writel(0x00, USBD_CONFIG);
  1236. au_writel(ep->address, USBD_CONFIG);
  1237. } else {
  1238. u8 dir = (i==2 || i==3) ? DIR_IN : DIR_OUT;
  1239. au_writel((i << 4) | 0x04, USBD_CONFIG);
  1240. au_writel(((16 & 0x380) >> 7) | dir |
  1241. (BULK_EP << 4), USBD_CONFIG);
  1242. au_writel((16 & 0x7f) << 1, USBD_CONFIG);
  1243. au_writel(0x00, USBD_CONFIG);
  1244. au_writel(i, USBD_CONFIG);
  1245. }
  1246. }
  1247. /*
  1248. * Enable Receive FIFO Complete interrupts only. Transmit
  1249. * complete is being handled by the DMA done interrupts.
  1250. */
  1251. au_writel(0x31, USBD_INTEN);
  1252. /*
  1253. * Controller is now enabled, request DMA and IRQ
  1254. * resources.
  1255. */
  1256. /* request the USB device transfer complete interrupt */
  1257. if (request_irq(AU1000_USB_DEV_REQ_INT, req_sus_intr, SA_INTERRUPT,
  1258. "USBdev req", &usbdev)) {
  1259. err("Can't get device request intr");
  1260. ret = -ENXIO;
  1261. goto out;
  1262. }
  1263. /* request the USB device suspend interrupt */
  1264. if (request_irq(AU1000_USB_DEV_SUS_INT, req_sus_intr, SA_INTERRUPT,
  1265. "USBdev sus", &usbdev)) {
  1266. err("Can't get device suspend intr");
  1267. ret = -ENXIO;
  1268. goto out;
  1269. }
  1270. /* Request EP0 DMA and IRQ */
  1271. if ((ep0->indma = request_au1000_dma(ep_dma_id[0].id,
  1272. ep_dma_id[0].str,
  1273. dma_done_ep0_intr,
  1274. SA_INTERRUPT,
  1275. &usbdev)) < 0) {
  1276. err("Can't get %s DMA", ep_dma_id[0].str);
  1277. ret = -ENXIO;
  1278. goto out;
  1279. }
  1280. if ((ep0->outdma = request_au1000_dma(ep_dma_id[1].id,
  1281. ep_dma_id[1].str,
  1282. NULL, 0, NULL)) < 0) {
  1283. err("Can't get %s DMA", ep_dma_id[1].str);
  1284. ret = -ENXIO;
  1285. goto out;
  1286. }
  1287. // Flush the ep0 buffers and FIFOs
  1288. endpoint_flush(ep0);
  1289. // start packet reception on ep0
  1290. kickstart_receive_packet(ep0);
  1291. /* Request DMA and IRQ for the other endpoints */
  1292. for (i = 2; i < 6; i++) {
  1293. endpoint_t *ep = &usbdev.ep[i];
  1294. if (!ep->active)
  1295. continue;
  1296. // Flush the endpoint buffers and FIFOs
  1297. endpoint_flush(ep);
  1298. if (ep->direction == USB_DIR_IN) {
  1299. ep->indma =
  1300. request_au1000_dma(ep_dma_id[ep->address].id,
  1301. ep_dma_id[ep->address].str,
  1302. dma_done_ep_intr,
  1303. SA_INTERRUPT,
  1304. &usbdev);
  1305. if (ep->indma < 0) {
  1306. err("Can't get %s DMA",
  1307. ep_dma_id[ep->address].str);
  1308. ret = -ENXIO;
  1309. goto out;
  1310. }
  1311. } else {
  1312. ep->outdma =
  1313. request_au1000_dma(ep_dma_id[ep->address].id,
  1314. ep_dma_id[ep->address].str,
  1315. NULL, 0, NULL);
  1316. if (ep->outdma < 0) {
  1317. err("Can't get %s DMA",
  1318. ep_dma_id[ep->address].str);
  1319. ret = -ENXIO;
  1320. goto out;
  1321. }
  1322. // start packet reception on OUT endpoint
  1323. kickstart_receive_packet(ep);
  1324. }
  1325. }
  1326. out:
  1327. if (ret)
  1328. usbdev_exit();
  1329. return ret;
  1330. }
  1331. EXPORT_SYMBOL(usbdev_init);
  1332. EXPORT_SYMBOL(usbdev_exit);
  1333. EXPORT_SYMBOL(usbdev_alloc_packet);
  1334. EXPORT_SYMBOL(usbdev_receive_packet);
  1335. EXPORT_SYMBOL(usbdev_send_packet);
  1336. EXPORT_SYMBOL(usbdev_get_byte_count);