usbdev.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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("%s", __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("%s", __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. kfree(pkt);
  923. }
  924. /*
  925. * Now check packet receive done. Shouldn't get these,
  926. * the receive packet complete intr should happen
  927. * before the DMA done intr occurs.
  928. */
  929. if ((buff_done = get_dma_buffer_done(ep0->outdma)) != 0) {
  930. // clear DMA done bit
  931. if (buff_done & DMA_D0)
  932. clear_dma_done0(ep0->outdma);
  933. if (buff_done & DMA_D1)
  934. clear_dma_done1(ep0->outdma);
  935. //process_ep0_receive(dev);
  936. }
  937. spin_unlock(&ep0->lock);
  938. }
  939. /* This ISR handles the DMA done events on endpoints 2,3,4,5 */
  940. static void
  941. dma_done_ep_intr(int irq, void *dev_id, struct pt_regs *regs)
  942. {
  943. struct usb_dev *dev = (struct usb_dev *) dev_id;
  944. int i;
  945. for (i = 2; i < 6; i++) {
  946. u32 buff_done;
  947. usbdev_pkt_t* pkt;
  948. endpoint_t *ep = &dev->ep[i];
  949. if (!ep->active) continue;
  950. spin_lock(&ep->lock);
  951. if (ep->direction == USB_DIR_IN) {
  952. buff_done = get_dma_buffer_done(ep->indma);
  953. if (buff_done != 0) {
  954. // transmitted a DATAx pkt on the IN ep
  955. // clear DMA done bit
  956. if (buff_done & DMA_D0)
  957. clear_dma_done0(ep->indma);
  958. if (buff_done & DMA_D1)
  959. clear_dma_done1(ep->indma);
  960. pkt = send_packet_complete(ep);
  961. spin_unlock(&ep->lock);
  962. dev->func_cb(CB_PKT_COMPLETE,
  963. (unsigned long)pkt,
  964. dev->cb_data);
  965. spin_lock(&ep->lock);
  966. }
  967. } else {
  968. /*
  969. * Check packet receive done (OUT ep). Shouldn't get
  970. * these, the rx packet complete intr should happen
  971. * before the DMA done intr occurs.
  972. */
  973. buff_done = get_dma_buffer_done(ep->outdma);
  974. if (buff_done != 0) {
  975. // received a DATAx pkt on the OUT ep
  976. // clear DMA done bit
  977. if (buff_done & DMA_D0)
  978. clear_dma_done0(ep->outdma);
  979. if (buff_done & DMA_D1)
  980. clear_dma_done1(ep->outdma);
  981. //process_ep_receive(dev, ep);
  982. }
  983. }
  984. spin_unlock(&ep->lock);
  985. }
  986. }
  987. /***************************************************************************
  988. * Here begins the external interface functions
  989. ***************************************************************************
  990. */
  991. /*
  992. * allocate a new packet
  993. */
  994. int
  995. usbdev_alloc_packet(int ep_addr, int data_size, usbdev_pkt_t** pkt)
  996. {
  997. endpoint_t * ep = epaddr_to_ep(&usbdev, ep_addr);
  998. usbdev_pkt_t* lpkt = NULL;
  999. if (!ep || !ep->active || ep->address < 2)
  1000. return -ENODEV;
  1001. if (data_size > ep->max_pkt_size)
  1002. return -EINVAL;
  1003. lpkt = *pkt = alloc_packet(ep, data_size, NULL);
  1004. if (!lpkt)
  1005. return -ENOMEM;
  1006. return 0;
  1007. }
  1008. /*
  1009. * packet send
  1010. */
  1011. int
  1012. usbdev_send_packet(int ep_addr, usbdev_pkt_t * pkt)
  1013. {
  1014. unsigned long flags;
  1015. int count;
  1016. endpoint_t * ep;
  1017. if (!pkt || !(ep = epaddr_to_ep(&usbdev, pkt->ep_addr)) ||
  1018. !ep->active || ep->address < 2)
  1019. return -ENODEV;
  1020. if (ep->direction != USB_DIR_IN)
  1021. return -EINVAL;
  1022. spin_lock_irqsave(&ep->lock, flags);
  1023. count = send_packet(&usbdev, pkt, 1);
  1024. spin_unlock_irqrestore(&ep->lock, flags);
  1025. return count;
  1026. }
  1027. /*
  1028. * packet receive
  1029. */
  1030. int
  1031. usbdev_receive_packet(int ep_addr, usbdev_pkt_t** pkt)
  1032. {
  1033. unsigned long flags;
  1034. usbdev_pkt_t* lpkt = NULL;
  1035. endpoint_t *ep = epaddr_to_ep(&usbdev, ep_addr);
  1036. if (!ep || !ep->active || ep->address < 2)
  1037. return -ENODEV;
  1038. if (ep->direction != USB_DIR_OUT)
  1039. return -EINVAL;
  1040. spin_lock_irqsave(&ep->lock, flags);
  1041. if (ep->outlist.count > 1)
  1042. lpkt = unlink_head(&ep->outlist);
  1043. spin_unlock_irqrestore(&ep->lock, flags);
  1044. if (!lpkt) {
  1045. /* no packet available */
  1046. *pkt = NULL;
  1047. return -ENODATA;
  1048. }
  1049. *pkt = lpkt;
  1050. return lpkt->size;
  1051. }
  1052. /*
  1053. * return total queued byte count on the endpoint.
  1054. */
  1055. int
  1056. usbdev_get_byte_count(int ep_addr)
  1057. {
  1058. unsigned long flags;
  1059. pkt_list_t *list;
  1060. usbdev_pkt_t *scan;
  1061. int count = 0;
  1062. endpoint_t * ep = epaddr_to_ep(&usbdev, ep_addr);
  1063. if (!ep || !ep->active || ep->address < 2)
  1064. return -ENODEV;
  1065. if (ep->direction == USB_DIR_IN) {
  1066. list = &ep->inlist;
  1067. spin_lock_irqsave(&ep->lock, flags);
  1068. for (scan = list->head; scan; scan = scan->next)
  1069. count += scan->size;
  1070. spin_unlock_irqrestore(&ep->lock, flags);
  1071. } else {
  1072. list = &ep->outlist;
  1073. spin_lock_irqsave(&ep->lock, flags);
  1074. if (list->count > 1) {
  1075. for (scan = list->head; scan != list->tail;
  1076. scan = scan->next)
  1077. count += scan->size;
  1078. }
  1079. spin_unlock_irqrestore(&ep->lock, flags);
  1080. }
  1081. return count;
  1082. }
  1083. void
  1084. usbdev_exit(void)
  1085. {
  1086. endpoint_t *ep;
  1087. int i;
  1088. au_writel(0, USBD_INTEN); // disable usb dev ints
  1089. au_writel(0, USBD_ENABLE); // disable usb dev
  1090. free_irq(AU1000_USB_DEV_REQ_INT, &usbdev);
  1091. free_irq(AU1000_USB_DEV_SUS_INT, &usbdev);
  1092. // free all control endpoint resources
  1093. ep = &usbdev.ep[0];
  1094. free_au1000_dma(ep->indma);
  1095. free_au1000_dma(ep->outdma);
  1096. endpoint_flush(ep);
  1097. // free ep resources
  1098. for (i = 2; i < 6; i++) {
  1099. ep = &usbdev.ep[i];
  1100. if (!ep->active) continue;
  1101. if (ep->direction == USB_DIR_IN) {
  1102. free_au1000_dma(ep->indma);
  1103. } else {
  1104. free_au1000_dma(ep->outdma);
  1105. }
  1106. endpoint_flush(ep);
  1107. }
  1108. kfree(usbdev.full_conf_desc);
  1109. }
  1110. int
  1111. usbdev_init(struct usb_device_descriptor* dev_desc,
  1112. struct usb_config_descriptor* config_desc,
  1113. struct usb_interface_descriptor* if_desc,
  1114. struct usb_endpoint_descriptor* ep_desc,
  1115. struct usb_string_descriptor* str_desc[],
  1116. void (*cb)(usbdev_cb_type_t, unsigned long, void *),
  1117. void* cb_data)
  1118. {
  1119. endpoint_t *ep0;
  1120. int i, ret=0;
  1121. u8* fcd;
  1122. if (dev_desc->bNumConfigurations > 1 ||
  1123. config_desc->bNumInterfaces > 1 ||
  1124. if_desc->bNumEndpoints > 4) {
  1125. err("Only one config, one i/f, and no more "
  1126. "than 4 ep's allowed");
  1127. ret = -EINVAL;
  1128. goto out;
  1129. }
  1130. if (!cb) {
  1131. err("Function-layer callback required");
  1132. ret = -EINVAL;
  1133. goto out;
  1134. }
  1135. if (dev_desc->bMaxPacketSize0 != USBDEV_EP0_MAX_PACKET_SIZE) {
  1136. warn("EP0 Max Packet size must be %d",
  1137. USBDEV_EP0_MAX_PACKET_SIZE);
  1138. dev_desc->bMaxPacketSize0 = USBDEV_EP0_MAX_PACKET_SIZE;
  1139. }
  1140. memset(&usbdev, 0, sizeof(struct usb_dev));
  1141. usbdev.state = DEFAULT;
  1142. usbdev.dev_desc = dev_desc;
  1143. usbdev.if_desc = if_desc;
  1144. usbdev.conf_desc = config_desc;
  1145. for (i=0; i<6; i++)
  1146. usbdev.str_desc[i] = str_desc[i];
  1147. usbdev.func_cb = cb;
  1148. usbdev.cb_data = cb_data;
  1149. /* Initialize default control endpoint */
  1150. ep0 = &usbdev.ep[0];
  1151. ep0->active = 1;
  1152. ep0->type = CONTROL_EP;
  1153. ep0->max_pkt_size = USBDEV_EP0_MAX_PACKET_SIZE;
  1154. spin_lock_init(&ep0->lock);
  1155. ep0->desc = NULL; // ep0 has no descriptor
  1156. ep0->address = 0;
  1157. ep0->direction = 0;
  1158. ep0->reg = &ep_reg[0];
  1159. /* Initialize the other requested endpoints */
  1160. for (i = 0; i < if_desc->bNumEndpoints; i++) {
  1161. struct usb_endpoint_descriptor* epd = &ep_desc[i];
  1162. endpoint_t *ep;
  1163. if ((epd->bEndpointAddress & 0x80) == USB_DIR_IN) {
  1164. ep = &usbdev.ep[2];
  1165. ep->address = 2;
  1166. if (ep->active) {
  1167. ep = &usbdev.ep[3];
  1168. ep->address = 3;
  1169. if (ep->active) {
  1170. err("too many IN ep's requested");
  1171. ret = -ENODEV;
  1172. goto out;
  1173. }
  1174. }
  1175. } else {
  1176. ep = &usbdev.ep[4];
  1177. ep->address = 4;
  1178. if (ep->active) {
  1179. ep = &usbdev.ep[5];
  1180. ep->address = 5;
  1181. if (ep->active) {
  1182. err("too many OUT ep's requested");
  1183. ret = -ENODEV;
  1184. goto out;
  1185. }
  1186. }
  1187. }
  1188. ep->active = 1;
  1189. epd->bEndpointAddress &= ~0x0f;
  1190. epd->bEndpointAddress |= (u8)ep->address;
  1191. ep->direction = epd->bEndpointAddress & 0x80;
  1192. ep->type = epd->bmAttributes & 0x03;
  1193. ep->max_pkt_size = le16_to_cpu(epd->wMaxPacketSize);
  1194. spin_lock_init(&ep->lock);
  1195. ep->desc = epd;
  1196. ep->reg = &ep_reg[ep->address];
  1197. }
  1198. /*
  1199. * initialize the full config descriptor
  1200. */
  1201. usbdev.full_conf_desc = fcd = kmalloc(le16_to_cpu(config_desc->wTotalLength),
  1202. ALLOC_FLAGS);
  1203. if (!fcd) {
  1204. err("failed to alloc full config descriptor");
  1205. ret = -ENOMEM;
  1206. goto out;
  1207. }
  1208. memcpy(fcd, config_desc, USB_DT_CONFIG_SIZE);
  1209. fcd += USB_DT_CONFIG_SIZE;
  1210. memcpy(fcd, if_desc, USB_DT_INTERFACE_SIZE);
  1211. fcd += USB_DT_INTERFACE_SIZE;
  1212. for (i = 0; i < if_desc->bNumEndpoints; i++) {
  1213. memcpy(fcd, &ep_desc[i], USB_DT_ENDPOINT_SIZE);
  1214. fcd += USB_DT_ENDPOINT_SIZE;
  1215. }
  1216. /* Now we're ready to enable the controller */
  1217. au_writel(0x0002, USBD_ENABLE);
  1218. udelay(100);
  1219. au_writel(0x0003, USBD_ENABLE);
  1220. udelay(100);
  1221. /* build and send config table based on ep descriptors */
  1222. for (i = 0; i < 6; i++) {
  1223. endpoint_t *ep;
  1224. if (i == 1)
  1225. continue; // skip dummy ep
  1226. ep = &usbdev.ep[i];
  1227. if (ep->active) {
  1228. au_writel((ep->address << 4) | 0x04, USBD_CONFIG);
  1229. au_writel(((ep->max_pkt_size & 0x380) >> 7) |
  1230. (ep->direction >> 4) | (ep->type << 4),
  1231. USBD_CONFIG);
  1232. au_writel((ep->max_pkt_size & 0x7f) << 1, USBD_CONFIG);
  1233. au_writel(0x00, USBD_CONFIG);
  1234. au_writel(ep->address, USBD_CONFIG);
  1235. } else {
  1236. u8 dir = (i==2 || i==3) ? DIR_IN : DIR_OUT;
  1237. au_writel((i << 4) | 0x04, USBD_CONFIG);
  1238. au_writel(((16 & 0x380) >> 7) | dir |
  1239. (BULK_EP << 4), USBD_CONFIG);
  1240. au_writel((16 & 0x7f) << 1, USBD_CONFIG);
  1241. au_writel(0x00, USBD_CONFIG);
  1242. au_writel(i, USBD_CONFIG);
  1243. }
  1244. }
  1245. /*
  1246. * Enable Receive FIFO Complete interrupts only. Transmit
  1247. * complete is being handled by the DMA done interrupts.
  1248. */
  1249. au_writel(0x31, USBD_INTEN);
  1250. /*
  1251. * Controller is now enabled, request DMA and IRQ
  1252. * resources.
  1253. */
  1254. /* request the USB device transfer complete interrupt */
  1255. if (request_irq(AU1000_USB_DEV_REQ_INT, req_sus_intr, SA_INTERRUPT,
  1256. "USBdev req", &usbdev)) {
  1257. err("Can't get device request intr");
  1258. ret = -ENXIO;
  1259. goto out;
  1260. }
  1261. /* request the USB device suspend interrupt */
  1262. if (request_irq(AU1000_USB_DEV_SUS_INT, req_sus_intr, SA_INTERRUPT,
  1263. "USBdev sus", &usbdev)) {
  1264. err("Can't get device suspend intr");
  1265. ret = -ENXIO;
  1266. goto out;
  1267. }
  1268. /* Request EP0 DMA and IRQ */
  1269. if ((ep0->indma = request_au1000_dma(ep_dma_id[0].id,
  1270. ep_dma_id[0].str,
  1271. dma_done_ep0_intr,
  1272. SA_INTERRUPT,
  1273. &usbdev)) < 0) {
  1274. err("Can't get %s DMA", ep_dma_id[0].str);
  1275. ret = -ENXIO;
  1276. goto out;
  1277. }
  1278. if ((ep0->outdma = request_au1000_dma(ep_dma_id[1].id,
  1279. ep_dma_id[1].str,
  1280. NULL, 0, NULL)) < 0) {
  1281. err("Can't get %s DMA", ep_dma_id[1].str);
  1282. ret = -ENXIO;
  1283. goto out;
  1284. }
  1285. // Flush the ep0 buffers and FIFOs
  1286. endpoint_flush(ep0);
  1287. // start packet reception on ep0
  1288. kickstart_receive_packet(ep0);
  1289. /* Request DMA and IRQ for the other endpoints */
  1290. for (i = 2; i < 6; i++) {
  1291. endpoint_t *ep = &usbdev.ep[i];
  1292. if (!ep->active)
  1293. continue;
  1294. // Flush the endpoint buffers and FIFOs
  1295. endpoint_flush(ep);
  1296. if (ep->direction == USB_DIR_IN) {
  1297. ep->indma =
  1298. request_au1000_dma(ep_dma_id[ep->address].id,
  1299. ep_dma_id[ep->address].str,
  1300. dma_done_ep_intr,
  1301. SA_INTERRUPT,
  1302. &usbdev);
  1303. if (ep->indma < 0) {
  1304. err("Can't get %s DMA",
  1305. ep_dma_id[ep->address].str);
  1306. ret = -ENXIO;
  1307. goto out;
  1308. }
  1309. } else {
  1310. ep->outdma =
  1311. request_au1000_dma(ep_dma_id[ep->address].id,
  1312. ep_dma_id[ep->address].str,
  1313. NULL, 0, NULL);
  1314. if (ep->outdma < 0) {
  1315. err("Can't get %s DMA",
  1316. ep_dma_id[ep->address].str);
  1317. ret = -ENXIO;
  1318. goto out;
  1319. }
  1320. // start packet reception on OUT endpoint
  1321. kickstart_receive_packet(ep);
  1322. }
  1323. }
  1324. out:
  1325. if (ret)
  1326. usbdev_exit();
  1327. return ret;
  1328. }
  1329. EXPORT_SYMBOL(usbdev_init);
  1330. EXPORT_SYMBOL(usbdev_exit);
  1331. EXPORT_SYMBOL(usbdev_alloc_packet);
  1332. EXPORT_SYMBOL(usbdev_receive_packet);
  1333. EXPORT_SYMBOL(usbdev_send_packet);
  1334. EXPORT_SYMBOL(usbdev_get_byte_count);