Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * MAX3421 Host Controller driver for USB.
   4 *
   5 * Author: David Mosberger-Tang <davidm@egauge.net>
   6 *
   7 * (C) Copyright 2014 David Mosberger-Tang <davidm@egauge.net>
   8 *
   9 * MAX3421 is a chip implementing a USB 2.0 Full-/Low-Speed host
  10 * controller on a SPI bus.
  11 *
  12 * Based on:
  13 *	o MAX3421E datasheet
  14 *		https://datasheets.maximintegrated.com/en/ds/MAX3421E.pdf
  15 *	o MAX3421E Programming Guide
  16 *		https://www.hdl.co.jp/ftpdata/utl-001/AN3785.pdf
  17 *	o gadget/dummy_hcd.c
  18 *		For USB HCD implementation.
  19 *	o Arduino MAX3421 driver
  20 *	     https://github.com/felis/USB_Host_Shield_2.0/blob/master/Usb.cpp
  21 *
  22 * This file is licenced under the GPL v2.
  23 *
  24 * Important note on worst-case (full-speed) packet size constraints
  25 * (See USB 2.0 Section 5.6.3 and following):
  26 *
  27 *	- control:	  64 bytes
  28 *	- isochronous:	1023 bytes
  29 *	- interrupt:	  64 bytes
  30 *	- bulk:		  64 bytes
  31 *
  32 * Since the MAX3421 FIFO size is 64 bytes, we do not have to work about
  33 * multi-FIFO writes/reads for a single USB packet *except* for isochronous
  34 * transfers.  We don't support isochronous transfers at this time, so we
  35 * just assume that a USB packet always fits into a single FIFO buffer.
  36 *
  37 * NOTE: The June 2006 version of "MAX3421E Programming Guide"
  38 * (AN3785) has conflicting info for the RCVDAVIRQ bit:
  39 *
  40 *	The description of RCVDAVIRQ says "The CPU *must* clear
  41 *	this IRQ bit (by writing a 1 to it) before reading the
  42 *	RCVFIFO data.
  43 *
  44 * However, the earlier section on "Programming BULK-IN
  45 * Transfers" says * that:
  46 *
  47 *	After the CPU retrieves the data, it clears the
  48 *	RCVDAVIRQ bit.
  49 *
  50 * The December 2006 version has been corrected and it consistently
  51 * states the second behavior is the correct one.
  52 *
  53 * Synchronous SPI transactions sleep so we can't perform any such
  54 * transactions while holding a spin-lock (and/or while interrupts are
  55 * masked).  To achieve this, all SPI transactions are issued from a
  56 * single thread (max3421_spi_thread).
  57 */
  58
  59#include <linux/jiffies.h>
  60#include <linux/module.h>
  61#include <linux/spi/spi.h>
  62#include <linux/usb.h>
  63#include <linux/usb/hcd.h>
  64#include <linux/of.h>
  65
  66#include <linux/platform_data/max3421-hcd.h>
  67
  68#define DRIVER_DESC	"MAX3421 USB Host-Controller Driver"
  69#define DRIVER_VERSION	"1.0"
  70
  71/* 11-bit counter that wraps around (USB 2.0 Section 8.3.3): */
  72#define USB_MAX_FRAME_NUMBER	0x7ff
  73#define USB_MAX_RETRIES		3 /* # of retries before error is reported */
  74
 
 
 
 
 
 
  75#define POWER_BUDGET	500	/* in mA; use 8 for low-power port testing */
  76
  77/* Port-change mask: */
  78#define PORT_C_MASK	((USB_PORT_STAT_C_CONNECTION |	\
  79			  USB_PORT_STAT_C_ENABLE |	\
  80			  USB_PORT_STAT_C_SUSPEND |	\
  81			  USB_PORT_STAT_C_OVERCURRENT | \
  82			  USB_PORT_STAT_C_RESET) << 16)
  83
  84#define MAX3421_GPOUT_COUNT	8
  85
  86enum max3421_rh_state {
  87	MAX3421_RH_RESET,
  88	MAX3421_RH_SUSPENDED,
  89	MAX3421_RH_RUNNING
  90};
  91
  92enum pkt_state {
  93	PKT_STATE_SETUP,	/* waiting to send setup packet to ctrl pipe */
  94	PKT_STATE_TRANSFER,	/* waiting to xfer transfer_buffer */
  95	PKT_STATE_TERMINATE	/* waiting to terminate control transfer */
  96};
  97
  98enum scheduling_pass {
  99	SCHED_PASS_PERIODIC,
 100	SCHED_PASS_NON_PERIODIC,
 101	SCHED_PASS_DONE
 102};
 103
 104/* Bit numbers for max3421_hcd->todo: */
 105enum {
 106	ENABLE_IRQ = 0,
 107	RESET_HCD,
 108	RESET_PORT,
 109	CHECK_UNLINK,
 110	IOPIN_UPDATE
 111};
 112
 113struct max3421_dma_buf {
 114	u8 data[2];
 115};
 116
 117struct max3421_hcd {
 118	spinlock_t lock;
 119
 120	struct task_struct *spi_thread;
 121
 122	enum max3421_rh_state rh_state;
 123	/* lower 16 bits contain port status, upper 16 bits the change mask: */
 124	u32 port_status;
 125
 126	unsigned active:1;
 127
 128	struct list_head ep_list;	/* list of EP's with work */
 129
 130	/*
 131	 * The following are owned by spi_thread (may be accessed by
 132	 * SPI-thread without acquiring the HCD lock:
 133	 */
 134	u8 rev;				/* chip revision */
 135	u16 frame_number;
 136	/*
 137	 * kmalloc'd buffers guaranteed to be in separate (DMA)
 138	 * cache-lines:
 139	 */
 140	struct max3421_dma_buf *tx;
 141	struct max3421_dma_buf *rx;
 142	/*
 143	 * URB we're currently processing.  Must not be reset to NULL
 144	 * unless MAX3421E chip is idle:
 145	 */
 146	struct urb *curr_urb;
 147	enum scheduling_pass sched_pass;
 148	int urb_done;			/* > 0 -> no errors, < 0: errno */
 149	size_t curr_len;
 150	u8 hien;
 151	u8 mode;
 152	u8 iopins[2];
 153	unsigned long todo;
 154#ifdef DEBUG
 155	unsigned long err_stat[16];
 156#endif
 157};
 158
 159struct max3421_ep {
 160	struct usb_host_endpoint *ep;
 161	struct list_head ep_list;
 162	u32 naks;
 163	u16 last_active;		/* frame # this ep was last active */
 164	enum pkt_state pkt_state;
 165	u8 retries;
 166	u8 retransmit;			/* packet needs retransmission */
 167};
 168
 169#define MAX3421_FIFO_SIZE	64
 170
 171#define MAX3421_SPI_DIR_RD	0	/* read register from MAX3421 */
 172#define MAX3421_SPI_DIR_WR	1	/* write register to MAX3421 */
 173
 174/* SPI commands: */
 175#define MAX3421_SPI_DIR_SHIFT	1
 176#define MAX3421_SPI_REG_SHIFT	3
 177
 178#define MAX3421_REG_RCVFIFO	1
 179#define MAX3421_REG_SNDFIFO	2
 180#define MAX3421_REG_SUDFIFO	4
 181#define MAX3421_REG_RCVBC	6
 182#define MAX3421_REG_SNDBC	7
 183#define MAX3421_REG_USBIRQ	13
 184#define MAX3421_REG_USBIEN	14
 185#define MAX3421_REG_USBCTL	15
 186#define MAX3421_REG_CPUCTL	16
 187#define MAX3421_REG_PINCTL	17
 188#define MAX3421_REG_REVISION	18
 189#define MAX3421_REG_IOPINS1	20
 190#define MAX3421_REG_IOPINS2	21
 191#define MAX3421_REG_GPINIRQ	22
 192#define MAX3421_REG_GPINIEN	23
 193#define MAX3421_REG_GPINPOL	24
 194#define MAX3421_REG_HIRQ	25
 195#define MAX3421_REG_HIEN	26
 196#define MAX3421_REG_MODE	27
 197#define MAX3421_REG_PERADDR	28
 198#define MAX3421_REG_HCTL	29
 199#define MAX3421_REG_HXFR	30
 200#define MAX3421_REG_HRSL	31
 201
 202enum {
 203	MAX3421_USBIRQ_OSCOKIRQ_BIT = 0,
 204	MAX3421_USBIRQ_NOVBUSIRQ_BIT = 5,
 205	MAX3421_USBIRQ_VBUSIRQ_BIT
 206};
 207
 208enum {
 209	MAX3421_CPUCTL_IE_BIT = 0,
 210	MAX3421_CPUCTL_PULSEWID0_BIT = 6,
 211	MAX3421_CPUCTL_PULSEWID1_BIT
 212};
 213
 214enum {
 215	MAX3421_USBCTL_PWRDOWN_BIT = 4,
 216	MAX3421_USBCTL_CHIPRES_BIT
 217};
 218
 219enum {
 220	MAX3421_PINCTL_GPXA_BIT	= 0,
 221	MAX3421_PINCTL_GPXB_BIT,
 222	MAX3421_PINCTL_POSINT_BIT,
 223	MAX3421_PINCTL_INTLEVEL_BIT,
 224	MAX3421_PINCTL_FDUPSPI_BIT,
 225	MAX3421_PINCTL_EP0INAK_BIT,
 226	MAX3421_PINCTL_EP2INAK_BIT,
 227	MAX3421_PINCTL_EP3INAK_BIT,
 228};
 229
 230enum {
 231	MAX3421_HI_BUSEVENT_BIT = 0,	/* bus-reset/-resume */
 232	MAX3421_HI_RWU_BIT,		/* remote wakeup */
 233	MAX3421_HI_RCVDAV_BIT,		/* receive FIFO data available */
 234	MAX3421_HI_SNDBAV_BIT,		/* send buffer available */
 235	MAX3421_HI_SUSDN_BIT,		/* suspend operation done */
 236	MAX3421_HI_CONDET_BIT,		/* peripheral connect/disconnect */
 237	MAX3421_HI_FRAME_BIT,		/* frame generator */
 238	MAX3421_HI_HXFRDN_BIT,		/* host transfer done */
 239};
 240
 241enum {
 242	MAX3421_HCTL_BUSRST_BIT = 0,
 243	MAX3421_HCTL_FRMRST_BIT,
 244	MAX3421_HCTL_SAMPLEBUS_BIT,
 245	MAX3421_HCTL_SIGRSM_BIT,
 246	MAX3421_HCTL_RCVTOG0_BIT,
 247	MAX3421_HCTL_RCVTOG1_BIT,
 248	MAX3421_HCTL_SNDTOG0_BIT,
 249	MAX3421_HCTL_SNDTOG1_BIT
 250};
 251
 252enum {
 253	MAX3421_MODE_HOST_BIT = 0,
 254	MAX3421_MODE_LOWSPEED_BIT,
 255	MAX3421_MODE_HUBPRE_BIT,
 256	MAX3421_MODE_SOFKAENAB_BIT,
 257	MAX3421_MODE_SEPIRQ_BIT,
 258	MAX3421_MODE_DELAYISO_BIT,
 259	MAX3421_MODE_DMPULLDN_BIT,
 260	MAX3421_MODE_DPPULLDN_BIT
 261};
 262
 263enum {
 264	MAX3421_HRSL_OK = 0,
 265	MAX3421_HRSL_BUSY,
 266	MAX3421_HRSL_BADREQ,
 267	MAX3421_HRSL_UNDEF,
 268	MAX3421_HRSL_NAK,
 269	MAX3421_HRSL_STALL,
 270	MAX3421_HRSL_TOGERR,
 271	MAX3421_HRSL_WRONGPID,
 272	MAX3421_HRSL_BADBC,
 273	MAX3421_HRSL_PIDERR,
 274	MAX3421_HRSL_PKTERR,
 275	MAX3421_HRSL_CRCERR,
 276	MAX3421_HRSL_KERR,
 277	MAX3421_HRSL_JERR,
 278	MAX3421_HRSL_TIMEOUT,
 279	MAX3421_HRSL_BABBLE,
 280	MAX3421_HRSL_RESULT_MASK = 0xf,
 281	MAX3421_HRSL_RCVTOGRD_BIT = 4,
 282	MAX3421_HRSL_SNDTOGRD_BIT,
 283	MAX3421_HRSL_KSTATUS_BIT,
 284	MAX3421_HRSL_JSTATUS_BIT
 285};
 286
 287/* Return same error-codes as ohci.h:cc_to_error: */
 288static const int hrsl_to_error[] = {
 289	[MAX3421_HRSL_OK] =		0,
 290	[MAX3421_HRSL_BUSY] =		-EINVAL,
 291	[MAX3421_HRSL_BADREQ] =		-EINVAL,
 292	[MAX3421_HRSL_UNDEF] =		-EINVAL,
 293	[MAX3421_HRSL_NAK] =		-EAGAIN,
 294	[MAX3421_HRSL_STALL] =		-EPIPE,
 295	[MAX3421_HRSL_TOGERR] =		-EILSEQ,
 296	[MAX3421_HRSL_WRONGPID] =	-EPROTO,
 297	[MAX3421_HRSL_BADBC] =		-EREMOTEIO,
 298	[MAX3421_HRSL_PIDERR] =		-EPROTO,
 299	[MAX3421_HRSL_PKTERR] =		-EPROTO,
 300	[MAX3421_HRSL_CRCERR] =		-EILSEQ,
 301	[MAX3421_HRSL_KERR] =		-EIO,
 302	[MAX3421_HRSL_JERR] =		-EIO,
 303	[MAX3421_HRSL_TIMEOUT] =	-ETIME,
 304	[MAX3421_HRSL_BABBLE] =		-EOVERFLOW
 305};
 306
 307/*
 308 * See https://www.beyondlogic.org/usbnutshell/usb4.shtml#Control for a
 309 * reasonable overview of how control transfers use the IN/OUT
 310 * tokens.
 311 */
 312#define MAX3421_HXFR_BULK_IN(ep)	(0x00 | (ep))	/* bulk or interrupt */
 313#define MAX3421_HXFR_SETUP		 0x10
 314#define MAX3421_HXFR_BULK_OUT(ep)	(0x20 | (ep))	/* bulk or interrupt */
 315#define MAX3421_HXFR_ISO_IN(ep)		(0x40 | (ep))
 316#define MAX3421_HXFR_ISO_OUT(ep)	(0x60 | (ep))
 317#define MAX3421_HXFR_HS_IN		 0x80		/* handshake in */
 318#define MAX3421_HXFR_HS_OUT		 0xa0		/* handshake out */
 319
 320#define field(val, bit)	((val) << (bit))
 321
 322static inline s16
 323frame_diff(u16 left, u16 right)
 324{
 325	return ((unsigned) (left - right)) % (USB_MAX_FRAME_NUMBER + 1);
 326}
 327
 328static inline struct max3421_hcd *
 329hcd_to_max3421(struct usb_hcd *hcd)
 330{
 331	return (struct max3421_hcd *) hcd->hcd_priv;
 332}
 333
 334static inline struct usb_hcd *
 335max3421_to_hcd(struct max3421_hcd *max3421_hcd)
 336{
 337	return container_of((void *) max3421_hcd, struct usb_hcd, hcd_priv);
 338}
 339
 340static u8
 341spi_rd8(struct usb_hcd *hcd, unsigned int reg)
 342{
 343	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 344	struct spi_device *spi = to_spi_device(hcd->self.controller);
 345	struct spi_transfer transfer;
 346	struct spi_message msg;
 347
 348	memset(&transfer, 0, sizeof(transfer));
 349
 350	spi_message_init(&msg);
 351
 352	max3421_hcd->tx->data[0] =
 353		(field(reg, MAX3421_SPI_REG_SHIFT) |
 354		 field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
 355
 356	transfer.tx_buf = max3421_hcd->tx->data;
 357	transfer.rx_buf = max3421_hcd->rx->data;
 358	transfer.len = 2;
 359
 360	spi_message_add_tail(&transfer, &msg);
 361	spi_sync(spi, &msg);
 362
 363	return max3421_hcd->rx->data[1];
 364}
 365
 366static void
 367spi_wr8(struct usb_hcd *hcd, unsigned int reg, u8 val)
 368{
 369	struct spi_device *spi = to_spi_device(hcd->self.controller);
 370	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 371	struct spi_transfer transfer;
 372	struct spi_message msg;
 373
 374	memset(&transfer, 0, sizeof(transfer));
 375
 376	spi_message_init(&msg);
 377
 378	max3421_hcd->tx->data[0] =
 379		(field(reg, MAX3421_SPI_REG_SHIFT) |
 380		 field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
 381	max3421_hcd->tx->data[1] = val;
 382
 383	transfer.tx_buf = max3421_hcd->tx->data;
 384	transfer.len = 2;
 385
 386	spi_message_add_tail(&transfer, &msg);
 387	spi_sync(spi, &msg);
 388}
 389
 390static void
 391spi_rd_buf(struct usb_hcd *hcd, unsigned int reg, void *buf, size_t len)
 392{
 393	struct spi_device *spi = to_spi_device(hcd->self.controller);
 394	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 395	struct spi_transfer transfer[2];
 396	struct spi_message msg;
 397
 398	memset(transfer, 0, sizeof(transfer));
 399
 400	spi_message_init(&msg);
 401
 402	max3421_hcd->tx->data[0] =
 403		(field(reg, MAX3421_SPI_REG_SHIFT) |
 404		 field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
 405	transfer[0].tx_buf = max3421_hcd->tx->data;
 406	transfer[0].len = 1;
 407
 408	transfer[1].rx_buf = buf;
 409	transfer[1].len = len;
 410
 411	spi_message_add_tail(&transfer[0], &msg);
 412	spi_message_add_tail(&transfer[1], &msg);
 413	spi_sync(spi, &msg);
 414}
 415
 416static void
 417spi_wr_buf(struct usb_hcd *hcd, unsigned int reg, void *buf, size_t len)
 418{
 419	struct spi_device *spi = to_spi_device(hcd->self.controller);
 420	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 421	struct spi_transfer transfer[2];
 422	struct spi_message msg;
 423
 424	memset(transfer, 0, sizeof(transfer));
 425
 426	spi_message_init(&msg);
 427
 428	max3421_hcd->tx->data[0] =
 429		(field(reg, MAX3421_SPI_REG_SHIFT) |
 430		 field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
 431
 432	transfer[0].tx_buf = max3421_hcd->tx->data;
 433	transfer[0].len = 1;
 434
 435	transfer[1].tx_buf = buf;
 436	transfer[1].len = len;
 437
 438	spi_message_add_tail(&transfer[0], &msg);
 439	spi_message_add_tail(&transfer[1], &msg);
 440	spi_sync(spi, &msg);
 441}
 442
 443/*
 444 * Figure out the correct setting for the LOWSPEED and HUBPRE mode
 445 * bits.  The HUBPRE bit needs to be set when MAX3421E operates at
 446 * full speed, but it's talking to a low-speed device (i.e., through a
 447 * hub).  Setting that bit ensures that every low-speed packet is
 448 * preceded by a full-speed PRE PID.  Possible configurations:
 449 *
 450 * Hub speed:	Device speed:	=>	LOWSPEED bit:	HUBPRE bit:
 451 *	FULL	FULL		=>	0		0
 452 *	FULL	LOW		=>	1		1
 453 *	LOW	LOW		=>	1		0
 454 *	LOW	FULL		=>	1		0
 455 */
 456static void
 457max3421_set_speed(struct usb_hcd *hcd, struct usb_device *dev)
 458{
 459	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 460	u8 mode_lowspeed, mode_hubpre, mode = max3421_hcd->mode;
 461
 462	mode_lowspeed = BIT(MAX3421_MODE_LOWSPEED_BIT);
 463	mode_hubpre   = BIT(MAX3421_MODE_HUBPRE_BIT);
 464	if (max3421_hcd->port_status & USB_PORT_STAT_LOW_SPEED) {
 465		mode |=  mode_lowspeed;
 466		mode &= ~mode_hubpre;
 467	} else if (dev->speed == USB_SPEED_LOW) {
 468		mode |= mode_lowspeed | mode_hubpre;
 469	} else {
 470		mode &= ~(mode_lowspeed | mode_hubpre);
 471	}
 472	if (mode != max3421_hcd->mode) {
 473		max3421_hcd->mode = mode;
 474		spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
 475	}
 476
 477}
 478
 479/*
 480 * Caller must NOT hold HCD spinlock.
 481 */
 482static void
 483max3421_set_address(struct usb_hcd *hcd, struct usb_device *dev, int epnum)
 484{
 485	int rcvtog, sndtog;
 486	u8 hctl;
 487
 488	/* setup new endpoint's toggle bits: */
 489	rcvtog = usb_gettoggle(dev, epnum, 0);
 490	sndtog = usb_gettoggle(dev, epnum, 1);
 491	hctl = (BIT(rcvtog + MAX3421_HCTL_RCVTOG0_BIT) |
 492		BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
 493
 494	spi_wr8(hcd, MAX3421_REG_HCTL, hctl);
 495
 496	/*
 497	 * Note: devnum for one and the same device can change during
 498	 * address-assignment so it's best to just always load the
 499	 * address whenever the end-point changed/was forced.
 500	 */
 501	spi_wr8(hcd, MAX3421_REG_PERADDR, dev->devnum);
 502}
 503
 504static int
 505max3421_ctrl_setup(struct usb_hcd *hcd, struct urb *urb)
 506{
 507	spi_wr_buf(hcd, MAX3421_REG_SUDFIFO, urb->setup_packet, 8);
 508	return MAX3421_HXFR_SETUP;
 509}
 510
 511static int
 512max3421_transfer_in(struct usb_hcd *hcd, struct urb *urb)
 513{
 514	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 515	int epnum = usb_pipeendpoint(urb->pipe);
 516
 517	max3421_hcd->curr_len = 0;
 518	max3421_hcd->hien |= BIT(MAX3421_HI_RCVDAV_BIT);
 519	return MAX3421_HXFR_BULK_IN(epnum);
 520}
 521
 522static int
 523max3421_transfer_out(struct usb_hcd *hcd, struct urb *urb, int fast_retransmit)
 524{
 525	struct spi_device *spi = to_spi_device(hcd->self.controller);
 526	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 527	int epnum = usb_pipeendpoint(urb->pipe);
 528	u32 max_packet;
 529	void *src;
 530
 531	src = urb->transfer_buffer + urb->actual_length;
 532
 533	if (fast_retransmit) {
 534		if (max3421_hcd->rev == 0x12) {
 535			/* work around rev 0x12 bug: */
 536			spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
 537			spi_wr8(hcd, MAX3421_REG_SNDFIFO, ((u8 *) src)[0]);
 538			spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
 539		}
 540		return MAX3421_HXFR_BULK_OUT(epnum);
 541	}
 542
 543	max_packet = usb_maxpacket(urb->dev, urb->pipe);
 544
 545	if (max_packet > MAX3421_FIFO_SIZE) {
 546		/*
 547		 * We do not support isochronous transfers at this
 548		 * time.
 549		 */
 550		dev_err(&spi->dev,
 551			"%s: packet-size of %u too big (limit is %u bytes)",
 552			__func__, max_packet, MAX3421_FIFO_SIZE);
 553		max3421_hcd->urb_done = -EMSGSIZE;
 554		return -EMSGSIZE;
 555	}
 556	max3421_hcd->curr_len = min((urb->transfer_buffer_length -
 557				     urb->actual_length), max_packet);
 558
 559	spi_wr_buf(hcd, MAX3421_REG_SNDFIFO, src, max3421_hcd->curr_len);
 560	spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
 561	return MAX3421_HXFR_BULK_OUT(epnum);
 562}
 563
 564/*
 565 * Issue the next host-transfer command.
 566 * Caller must NOT hold HCD spinlock.
 567 */
 568static void
 569max3421_next_transfer(struct usb_hcd *hcd, int fast_retransmit)
 570{
 571	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 572	struct urb *urb = max3421_hcd->curr_urb;
 573	struct max3421_ep *max3421_ep;
 574	int cmd = -EINVAL;
 575
 576	if (!urb)
 577		return;	/* nothing to do */
 578
 579	max3421_ep = urb->ep->hcpriv;
 580
 581	switch (max3421_ep->pkt_state) {
 582	case PKT_STATE_SETUP:
 583		cmd = max3421_ctrl_setup(hcd, urb);
 584		break;
 585
 586	case PKT_STATE_TRANSFER:
 587		if (usb_urb_dir_in(urb))
 588			cmd = max3421_transfer_in(hcd, urb);
 589		else
 590			cmd = max3421_transfer_out(hcd, urb, fast_retransmit);
 591		break;
 592
 593	case PKT_STATE_TERMINATE:
 594		/*
 595		 * IN transfers are terminated with HS_OUT token,
 596		 * OUT transfers with HS_IN:
 597		 */
 598		if (usb_urb_dir_in(urb))
 599			cmd = MAX3421_HXFR_HS_OUT;
 600		else
 601			cmd = MAX3421_HXFR_HS_IN;
 602		break;
 603	}
 604
 605	if (cmd < 0)
 606		return;
 607
 608	/* issue the command and wait for host-xfer-done interrupt: */
 609
 610	spi_wr8(hcd, MAX3421_REG_HXFR, cmd);
 611	max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT);
 612}
 613
 614/*
 615 * Find the next URB to process and start its execution.
 616 *
 617 * At this time, we do not anticipate ever connecting a USB hub to the
 618 * MAX3421 chip, so at most USB device can be connected and we can use
 619 * a simplistic scheduler: at the start of a frame, schedule all
 620 * periodic transfers.  Once that is done, use the remainder of the
 621 * frame to process non-periodic (bulk & control) transfers.
 622 *
 623 * Preconditions:
 624 * o Caller must NOT hold HCD spinlock.
 625 * o max3421_hcd->curr_urb MUST BE NULL.
 626 * o MAX3421E chip must be idle.
 627 */
 628static int
 629max3421_select_and_start_urb(struct usb_hcd *hcd)
 630{
 631	struct spi_device *spi = to_spi_device(hcd->self.controller);
 632	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 633	struct urb *urb, *curr_urb = NULL;
 634	struct max3421_ep *max3421_ep;
 635	int epnum;
 636	struct usb_host_endpoint *ep;
 637	struct list_head *pos;
 638	unsigned long flags;
 639
 640	spin_lock_irqsave(&max3421_hcd->lock, flags);
 641
 642	for (;
 643	     max3421_hcd->sched_pass < SCHED_PASS_DONE;
 644	     ++max3421_hcd->sched_pass)
 645		list_for_each(pos, &max3421_hcd->ep_list) {
 646			urb = NULL;
 647			max3421_ep = container_of(pos, struct max3421_ep,
 648						  ep_list);
 649			ep = max3421_ep->ep;
 650
 651			switch (usb_endpoint_type(&ep->desc)) {
 652			case USB_ENDPOINT_XFER_ISOC:
 653			case USB_ENDPOINT_XFER_INT:
 654				if (max3421_hcd->sched_pass !=
 655				    SCHED_PASS_PERIODIC)
 656					continue;
 657				break;
 658
 659			case USB_ENDPOINT_XFER_CONTROL:
 660			case USB_ENDPOINT_XFER_BULK:
 661				if (max3421_hcd->sched_pass !=
 662				    SCHED_PASS_NON_PERIODIC)
 663					continue;
 664				break;
 665			}
 666
 667			if (list_empty(&ep->urb_list))
 668				continue;	/* nothing to do */
 669			urb = list_first_entry(&ep->urb_list, struct urb,
 670					       urb_list);
 671			if (urb->unlinked) {
 672				dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
 673					__func__, urb, urb->unlinked);
 674				max3421_hcd->curr_urb = urb;
 675				max3421_hcd->urb_done = 1;
 676				spin_unlock_irqrestore(&max3421_hcd->lock,
 677						       flags);
 678				return 1;
 679			}
 680
 681			switch (usb_endpoint_type(&ep->desc)) {
 682			case USB_ENDPOINT_XFER_CONTROL:
 683				/*
 684				 * Allow one control transaction per
 685				 * frame per endpoint:
 686				 */
 687				if (frame_diff(max3421_ep->last_active,
 688					       max3421_hcd->frame_number) == 0)
 689					continue;
 690				break;
 691
 692			case USB_ENDPOINT_XFER_BULK:
 693				if (max3421_ep->retransmit
 694				    && (frame_diff(max3421_ep->last_active,
 695						   max3421_hcd->frame_number)
 696					== 0))
 697					/*
 698					 * We already tried this EP
 699					 * during this frame and got a
 700					 * NAK or error; wait for next frame
 701					 */
 702					continue;
 703				break;
 704
 705			case USB_ENDPOINT_XFER_ISOC:
 706			case USB_ENDPOINT_XFER_INT:
 707				if (frame_diff(max3421_hcd->frame_number,
 708					       max3421_ep->last_active)
 709				    < urb->interval)
 710					/*
 711					 * We already processed this
 712					 * end-point in the current
 713					 * frame
 714					 */
 715					continue;
 716				break;
 717			}
 718
 719			/* move current ep to tail: */
 720			list_move_tail(pos, &max3421_hcd->ep_list);
 721			curr_urb = urb;
 722			goto done;
 723		}
 724done:
 725	if (!curr_urb) {
 726		spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 727		return 0;
 728	}
 729
 730	urb = max3421_hcd->curr_urb = curr_urb;
 731	epnum = usb_endpoint_num(&urb->ep->desc);
 732	if (max3421_ep->retransmit)
 733		/* restart (part of) a USB transaction: */
 734		max3421_ep->retransmit = 0;
 735	else {
 736		/* start USB transaction: */
 737		if (usb_endpoint_xfer_control(&ep->desc)) {
 738			/*
 739			 * See USB 2.0 spec section 8.6.1
 740			 * Initialization via SETUP Token:
 741			 */
 742			usb_settoggle(urb->dev, epnum, 0, 1);
 743			usb_settoggle(urb->dev, epnum, 1, 1);
 744			max3421_ep->pkt_state = PKT_STATE_SETUP;
 745		} else
 746			max3421_ep->pkt_state = PKT_STATE_TRANSFER;
 747	}
 748
 749	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 750
 751	max3421_ep->last_active = max3421_hcd->frame_number;
 752	max3421_set_address(hcd, urb->dev, epnum);
 753	max3421_set_speed(hcd, urb->dev);
 754	max3421_next_transfer(hcd, 0);
 755	return 1;
 756}
 757
 758/*
 759 * Check all endpoints for URBs that got unlinked.
 760 *
 761 * Caller must NOT hold HCD spinlock.
 762 */
 763static int
 764max3421_check_unlink(struct usb_hcd *hcd)
 765{
 766	struct spi_device *spi = to_spi_device(hcd->self.controller);
 767	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 768	struct max3421_ep *max3421_ep;
 769	struct usb_host_endpoint *ep;
 770	struct urb *urb, *next;
 771	unsigned long flags;
 772	int retval = 0;
 773
 774	spin_lock_irqsave(&max3421_hcd->lock, flags);
 775	list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) {
 776		ep = max3421_ep->ep;
 777		list_for_each_entry_safe(urb, next, &ep->urb_list, urb_list) {
 778			if (urb->unlinked) {
 779				retval = 1;
 780				dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
 781					__func__, urb, urb->unlinked);
 782				usb_hcd_unlink_urb_from_ep(hcd, urb);
 783				spin_unlock_irqrestore(&max3421_hcd->lock,
 784						       flags);
 785				usb_hcd_giveback_urb(hcd, urb, 0);
 786				spin_lock_irqsave(&max3421_hcd->lock, flags);
 787			}
 788		}
 789	}
 790	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 791	return retval;
 792}
 793
 794/*
 795 * Caller must NOT hold HCD spinlock.
 796 */
 797static void
 798max3421_slow_retransmit(struct usb_hcd *hcd)
 799{
 800	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 801	struct urb *urb = max3421_hcd->curr_urb;
 802	struct max3421_ep *max3421_ep;
 803
 804	max3421_ep = urb->ep->hcpriv;
 805	max3421_ep->retransmit = 1;
 806	max3421_hcd->curr_urb = NULL;
 807}
 808
 809/*
 810 * Caller must NOT hold HCD spinlock.
 811 */
 812static void
 813max3421_recv_data_available(struct usb_hcd *hcd)
 814{
 815	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 816	struct urb *urb = max3421_hcd->curr_urb;
 817	size_t remaining, transfer_size;
 818	u8 rcvbc;
 819
 820	rcvbc = spi_rd8(hcd, MAX3421_REG_RCVBC);
 821
 822	if (rcvbc > MAX3421_FIFO_SIZE)
 823		rcvbc = MAX3421_FIFO_SIZE;
 824	if (urb->actual_length >= urb->transfer_buffer_length)
 825		remaining = 0;
 826	else
 827		remaining = urb->transfer_buffer_length - urb->actual_length;
 828	transfer_size = rcvbc;
 829	if (transfer_size > remaining)
 830		transfer_size = remaining;
 831	if (transfer_size > 0) {
 832		void *dst = urb->transfer_buffer + urb->actual_length;
 833
 834		spi_rd_buf(hcd, MAX3421_REG_RCVFIFO, dst, transfer_size);
 835		urb->actual_length += transfer_size;
 836		max3421_hcd->curr_len = transfer_size;
 837	}
 838
 839	/* ack the RCVDAV irq now that the FIFO has been read: */
 840	spi_wr8(hcd, MAX3421_REG_HIRQ, BIT(MAX3421_HI_RCVDAV_BIT));
 841}
 842
 843static void
 844max3421_handle_error(struct usb_hcd *hcd, u8 hrsl)
 845{
 846	struct spi_device *spi = to_spi_device(hcd->self.controller);
 847	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 848	u8 result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
 849	struct urb *urb = max3421_hcd->curr_urb;
 850	struct max3421_ep *max3421_ep = urb->ep->hcpriv;
 851	int switch_sndfifo;
 852
 853	/*
 854	 * If an OUT command results in any response other than OK
 855	 * (i.e., error or NAK), we have to perform a dummy-write to
 856	 * SNDBC so the FIFO gets switched back to us.  Otherwise, we
 857	 * get out of sync with the SNDFIFO double buffer.
 858	 */
 859	switch_sndfifo = (max3421_ep->pkt_state == PKT_STATE_TRANSFER &&
 860			  usb_urb_dir_out(urb));
 861
 862	switch (result_code) {
 863	case MAX3421_HRSL_OK:
 864		return;			/* this shouldn't happen */
 865
 866	case MAX3421_HRSL_WRONGPID:	/* received wrong PID */
 867	case MAX3421_HRSL_BUSY:		/* SIE busy */
 868	case MAX3421_HRSL_BADREQ:	/* bad val in HXFR */
 869	case MAX3421_HRSL_UNDEF:	/* reserved */
 870	case MAX3421_HRSL_KERR:		/* K-state instead of response */
 871	case MAX3421_HRSL_JERR:		/* J-state instead of response */
 872		/*
 873		 * packet experienced an error that we cannot recover
 874		 * from; report error
 875		 */
 876		max3421_hcd->urb_done = hrsl_to_error[result_code];
 877		dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
 878			__func__, hrsl);
 879		break;
 880
 881	case MAX3421_HRSL_TOGERR:
 882		if (usb_urb_dir_in(urb))
 883			; /* don't do anything (device will switch toggle) */
 884		else {
 885			/* flip the send toggle bit: */
 886			int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
 887
 888			sndtog ^= 1;
 889			spi_wr8(hcd, MAX3421_REG_HCTL,
 890				BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
 891		}
 892		fallthrough;
 893	case MAX3421_HRSL_BADBC:	/* bad byte count */
 894	case MAX3421_HRSL_PIDERR:	/* received PID is corrupted */
 895	case MAX3421_HRSL_PKTERR:	/* packet error (stuff, EOP) */
 896	case MAX3421_HRSL_CRCERR:	/* CRC error */
 897	case MAX3421_HRSL_BABBLE:	/* device talked too long */
 898	case MAX3421_HRSL_TIMEOUT:
 899		if (max3421_ep->retries++ < USB_MAX_RETRIES)
 900			/* retry the packet again in the next frame */
 901			max3421_slow_retransmit(hcd);
 902		else {
 903			/* Based on ohci.h cc_to_err[]: */
 904			max3421_hcd->urb_done = hrsl_to_error[result_code];
 905			dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
 906				__func__, hrsl);
 907		}
 908		break;
 909
 910	case MAX3421_HRSL_STALL:
 911		dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
 912			__func__, hrsl);
 913		max3421_hcd->urb_done = hrsl_to_error[result_code];
 914		break;
 915
 916	case MAX3421_HRSL_NAK:
 917		/*
 918		 * Device wasn't ready for data or has no data
 919		 * available: retry the packet again.
 920		 */
 921		max3421_next_transfer(hcd, 1);
 922		switch_sndfifo = 0;
 
 
 
 923		break;
 924	}
 925	if (switch_sndfifo)
 926		spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
 927}
 928
 929/*
 930 * Caller must NOT hold HCD spinlock.
 931 */
 932static int
 933max3421_transfer_in_done(struct usb_hcd *hcd, struct urb *urb)
 934{
 935	struct spi_device *spi = to_spi_device(hcd->self.controller);
 936	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 937	u32 max_packet;
 938
 939	if (urb->actual_length >= urb->transfer_buffer_length)
 940		return 1;	/* read is complete, so we're done */
 941
 942	/*
 943	 * USB 2.0 Section 5.3.2 Pipes: packets must be full size
 944	 * except for last one.
 945	 */
 946	max_packet = usb_maxpacket(urb->dev, urb->pipe);
 947	if (max_packet > MAX3421_FIFO_SIZE) {
 948		/*
 949		 * We do not support isochronous transfers at this
 950		 * time...
 951		 */
 952		dev_err(&spi->dev,
 953			"%s: packet-size of %u too big (limit is %u bytes)",
 954			__func__, max_packet, MAX3421_FIFO_SIZE);
 955		return -EINVAL;
 956	}
 957
 958	if (max3421_hcd->curr_len < max_packet) {
 959		if (urb->transfer_flags & URB_SHORT_NOT_OK) {
 960			/*
 961			 * remaining > 0 and received an
 962			 * unexpected partial packet ->
 963			 * error
 964			 */
 965			return -EREMOTEIO;
 966		} else
 967			/* short read, but it's OK */
 968			return 1;
 969	}
 970	return 0;	/* not done */
 971}
 972
 973/*
 974 * Caller must NOT hold HCD spinlock.
 975 */
 976static int
 977max3421_transfer_out_done(struct usb_hcd *hcd, struct urb *urb)
 978{
 979	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 980
 981	urb->actual_length += max3421_hcd->curr_len;
 982	if (urb->actual_length < urb->transfer_buffer_length)
 983		return 0;
 984	if (urb->transfer_flags & URB_ZERO_PACKET) {
 985		/*
 986		 * Some hardware needs a zero-size packet at the end
 987		 * of a bulk-out transfer if the last transfer was a
 988		 * full-sized packet (i.e., such hardware use <
 989		 * max_packet as an indicator that the end of the
 990		 * packet has been reached).
 991		 */
 992		u32 max_packet = usb_maxpacket(urb->dev, urb->pipe);
 993
 994		if (max3421_hcd->curr_len == max_packet)
 995			return 0;
 996	}
 997	return 1;
 998}
 999
1000/*
1001 * Caller must NOT hold HCD spinlock.
1002 */
1003static void
1004max3421_host_transfer_done(struct usb_hcd *hcd)
1005{
1006	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1007	struct urb *urb = max3421_hcd->curr_urb;
1008	struct max3421_ep *max3421_ep;
1009	u8 result_code, hrsl;
1010	int urb_done = 0;
1011
1012	max3421_hcd->hien &= ~(BIT(MAX3421_HI_HXFRDN_BIT) |
1013			       BIT(MAX3421_HI_RCVDAV_BIT));
1014
1015	hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1016	result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
1017
1018#ifdef DEBUG
1019	++max3421_hcd->err_stat[result_code];
1020#endif
1021
1022	max3421_ep = urb->ep->hcpriv;
1023
1024	if (unlikely(result_code != MAX3421_HRSL_OK)) {
1025		max3421_handle_error(hcd, hrsl);
1026		return;
1027	}
1028
1029	max3421_ep->naks = 0;
1030	max3421_ep->retries = 0;
1031	switch (max3421_ep->pkt_state) {
1032
1033	case PKT_STATE_SETUP:
1034		if (urb->transfer_buffer_length > 0)
1035			max3421_ep->pkt_state = PKT_STATE_TRANSFER;
1036		else
1037			max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1038		break;
1039
1040	case PKT_STATE_TRANSFER:
1041		if (usb_urb_dir_in(urb))
1042			urb_done = max3421_transfer_in_done(hcd, urb);
1043		else
1044			urb_done = max3421_transfer_out_done(hcd, urb);
1045		if (urb_done > 0 && usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1046			/*
1047			 * We aren't really done - we still need to
1048			 * terminate the control transfer:
1049			 */
1050			max3421_hcd->urb_done = urb_done = 0;
1051			max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1052		}
1053		break;
1054
1055	case PKT_STATE_TERMINATE:
1056		urb_done = 1;
1057		break;
1058	}
1059
1060	if (urb_done)
1061		max3421_hcd->urb_done = urb_done;
1062	else
1063		max3421_next_transfer(hcd, 0);
1064}
1065
1066/*
1067 * Caller must NOT hold HCD spinlock.
1068 */
1069static void
1070max3421_detect_conn(struct usb_hcd *hcd)
1071{
1072	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1073	unsigned int jk, have_conn = 0;
1074	u32 old_port_status, chg;
1075	unsigned long flags;
1076	u8 hrsl, mode;
1077
1078	hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1079
1080	jk = ((((hrsl >> MAX3421_HRSL_JSTATUS_BIT) & 1) << 0) |
1081	      (((hrsl >> MAX3421_HRSL_KSTATUS_BIT) & 1) << 1));
1082
1083	mode = max3421_hcd->mode;
1084
1085	switch (jk) {
1086	case 0x0: /* SE0: disconnect */
1087		/*
1088		 * Turn off SOFKAENAB bit to avoid getting interrupt
1089		 * every milli-second:
1090		 */
1091		mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT);
1092		break;
1093
1094	case 0x1: /* J=0,K=1: low-speed (in full-speed or vice versa) */
1095	case 0x2: /* J=1,K=0: full-speed (in full-speed or vice versa) */
1096		if (jk == 0x2)
1097			/* need to switch to the other speed: */
1098			mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT);
1099		/* turn on SOFKAENAB bit: */
1100		mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT);
1101		have_conn = 1;
1102		break;
1103
1104	case 0x3: /* illegal */
1105		break;
1106	}
1107
1108	max3421_hcd->mode = mode;
1109	spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1110
1111	spin_lock_irqsave(&max3421_hcd->lock, flags);
1112	old_port_status = max3421_hcd->port_status;
1113	if (have_conn)
1114		max3421_hcd->port_status |=  USB_PORT_STAT_CONNECTION;
1115	else
1116		max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION;
1117	if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT))
1118		max3421_hcd->port_status |=  USB_PORT_STAT_LOW_SPEED;
1119	else
1120		max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED;
1121	chg = (old_port_status ^ max3421_hcd->port_status);
1122	max3421_hcd->port_status |= chg << 16;
1123	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1124}
1125
1126static irqreturn_t
1127max3421_irq_handler(int irq, void *dev_id)
1128{
1129	struct usb_hcd *hcd = dev_id;
1130	struct spi_device *spi = to_spi_device(hcd->self.controller);
1131	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1132
1133	if (max3421_hcd->spi_thread)
1134		wake_up_process(max3421_hcd->spi_thread);
1135	if (!test_and_set_bit(ENABLE_IRQ, &max3421_hcd->todo))
1136		disable_irq_nosync(spi->irq);
1137	return IRQ_HANDLED;
1138}
1139
1140#ifdef DEBUG
1141
1142static void
1143dump_eps(struct usb_hcd *hcd)
1144{
1145	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1146	struct max3421_ep *max3421_ep;
1147	struct usb_host_endpoint *ep;
1148	char ubuf[512], *dp, *end;
1149	unsigned long flags;
1150	struct urb *urb;
1151	int epnum, ret;
1152
1153	spin_lock_irqsave(&max3421_hcd->lock, flags);
1154	list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) {
1155		ep = max3421_ep->ep;
1156
1157		dp = ubuf;
1158		end = dp + sizeof(ubuf);
1159		*dp = '\0';
1160		list_for_each_entry(urb, &ep->urb_list, urb_list) {
1161			ret = scnprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb,
1162					usb_pipetype(urb->pipe),
1163					usb_urb_dir_in(urb) ? "IN" : "OUT",
1164					urb->actual_length,
1165					urb->transfer_buffer_length);
1166			if (ret == end - dp - 1)
1167				break;	/* error or buffer full */
1168			dp += ret;
1169		}
1170
1171		epnum = usb_endpoint_num(&ep->desc);
1172		pr_info("EP%0u %u lst %04u rtr %u nak %6u rxmt %u: %s\n",
1173			epnum, max3421_ep->pkt_state, max3421_ep->last_active,
1174			max3421_ep->retries, max3421_ep->naks,
1175			max3421_ep->retransmit, ubuf);
1176	}
1177	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1178}
1179
1180#endif /* DEBUG */
1181
1182/* Return zero if no work was performed, 1 otherwise.  */
1183static int
1184max3421_handle_irqs(struct usb_hcd *hcd)
1185{
1186	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1187	u32 chg, old_port_status;
1188	unsigned long flags;
1189	u8 hirq;
1190
1191	/*
1192	 * Read and ack pending interrupts (CPU must never
1193	 * clear SNDBAV directly and RCVDAV must be cleared by
1194	 * max3421_recv_data_available()!):
1195	 */
1196	hirq = spi_rd8(hcd, MAX3421_REG_HIRQ);
1197	hirq &= max3421_hcd->hien;
1198	if (!hirq)
1199		return 0;
1200
1201	spi_wr8(hcd, MAX3421_REG_HIRQ,
1202		hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) |
1203			 BIT(MAX3421_HI_RCVDAV_BIT)));
1204
1205	if (hirq & BIT(MAX3421_HI_FRAME_BIT)) {
1206		max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1)
1207					     & USB_MAX_FRAME_NUMBER);
1208		max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1209	}
1210
1211	if (hirq & BIT(MAX3421_HI_RCVDAV_BIT))
1212		max3421_recv_data_available(hcd);
1213
1214	if (hirq & BIT(MAX3421_HI_HXFRDN_BIT))
1215		max3421_host_transfer_done(hcd);
1216
1217	if (hirq & BIT(MAX3421_HI_CONDET_BIT))
1218		max3421_detect_conn(hcd);
1219
1220	/*
1221	 * Now process interrupts that may affect HCD state
1222	 * other than the end-points:
1223	 */
1224	spin_lock_irqsave(&max3421_hcd->lock, flags);
1225
1226	old_port_status = max3421_hcd->port_status;
1227	if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) {
1228		if (max3421_hcd->port_status & USB_PORT_STAT_RESET) {
1229			/* BUSEVENT due to completion of Bus Reset */
1230			max3421_hcd->port_status &= ~USB_PORT_STAT_RESET;
1231			max3421_hcd->port_status |=  USB_PORT_STAT_ENABLE;
1232		} else {
1233			/* BUSEVENT due to completion of Bus Resume */
1234			pr_info("%s: BUSEVENT Bus Resume Done\n", __func__);
1235		}
1236	}
1237	if (hirq & BIT(MAX3421_HI_RWU_BIT))
1238		pr_info("%s: RWU\n", __func__);
1239	if (hirq & BIT(MAX3421_HI_SUSDN_BIT))
1240		pr_info("%s: SUSDN\n", __func__);
1241
1242	chg = (old_port_status ^ max3421_hcd->port_status);
1243	max3421_hcd->port_status |= chg << 16;
1244
1245	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1246
1247#ifdef DEBUG
1248	{
1249		static unsigned long last_time;
1250		char sbuf[16 * 16], *dp, *end;
1251		int i;
1252
1253		if (time_after(jiffies, last_time + 5*HZ)) {
1254			dp = sbuf;
1255			end = sbuf + sizeof(sbuf);
1256			*dp = '\0';
1257			for (i = 0; i < 16; ++i) {
1258				int ret = scnprintf(dp, end - dp, " %lu",
1259						    max3421_hcd->err_stat[i]);
1260				if (ret == end - dp - 1)
1261					break;	/* error or buffer full */
1262				dp += ret;
1263			}
1264			pr_info("%s: hrsl_stats %s\n", __func__, sbuf);
1265			memset(max3421_hcd->err_stat, 0,
1266			       sizeof(max3421_hcd->err_stat));
1267			last_time = jiffies;
1268
1269			dump_eps(hcd);
1270		}
1271	}
1272#endif
1273	return 1;
1274}
1275
1276static int
1277max3421_reset_hcd(struct usb_hcd *hcd)
1278{
1279	struct spi_device *spi = to_spi_device(hcd->self.controller);
1280	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1281	int timeout;
1282
1283	/* perform a chip reset and wait for OSCIRQ signal to appear: */
1284	spi_wr8(hcd, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT));
1285	/* clear reset: */
1286	spi_wr8(hcd, MAX3421_REG_USBCTL, 0);
1287	timeout = 1000;
1288	while (1) {
1289		if (spi_rd8(hcd, MAX3421_REG_USBIRQ)
1290		    & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT))
1291			break;
1292		if (--timeout < 0) {
1293			dev_err(&spi->dev,
1294				"timed out waiting for oscillator OK signal");
1295			return 1;
1296		}
1297		cond_resched();
1298	}
1299
1300	/*
1301	 * Turn on host mode, automatic generation of SOF packets, and
1302	 * enable pull-down registers on DM/DP:
1303	 */
1304	max3421_hcd->mode = (BIT(MAX3421_MODE_HOST_BIT) |
1305			     BIT(MAX3421_MODE_SOFKAENAB_BIT) |
1306			     BIT(MAX3421_MODE_DMPULLDN_BIT) |
1307			     BIT(MAX3421_MODE_DPPULLDN_BIT));
1308	spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1309
1310	/* reset frame-number: */
1311	max3421_hcd->frame_number = USB_MAX_FRAME_NUMBER;
1312	spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT));
1313
1314	/* sample the state of the D+ and D- lines */
1315	spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT));
1316	max3421_detect_conn(hcd);
1317
1318	/* enable frame, connection-detected, and bus-event interrupts: */
1319	max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) |
1320			     BIT(MAX3421_HI_CONDET_BIT) |
1321			     BIT(MAX3421_HI_BUSEVENT_BIT));
1322	spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1323
1324	/* enable interrupts: */
1325	spi_wr8(hcd, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT));
1326	return 1;
1327}
1328
1329static int
1330max3421_urb_done(struct usb_hcd *hcd)
1331{
1332	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1333	unsigned long flags;
1334	struct urb *urb;
1335	int status;
1336
1337	status = max3421_hcd->urb_done;
1338	max3421_hcd->urb_done = 0;
1339	if (status > 0)
1340		status = 0;
1341	urb = max3421_hcd->curr_urb;
1342	if (urb) {
1343		/* save the old end-points toggles: */
1344		u8 hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1345		int rcvtog = (hrsl >> MAX3421_HRSL_RCVTOGRD_BIT) & 1;
1346		int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
1347		int epnum = usb_endpoint_num(&urb->ep->desc);
1348
1349		/* no locking: HCD (i.e., we) own toggles, don't we? */
1350		usb_settoggle(urb->dev, epnum, 0, rcvtog);
1351		usb_settoggle(urb->dev, epnum, 1, sndtog);
1352
1353		max3421_hcd->curr_urb = NULL;
1354		spin_lock_irqsave(&max3421_hcd->lock, flags);
1355		usb_hcd_unlink_urb_from_ep(hcd, urb);
1356		spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1357
1358		/* must be called without the HCD spinlock: */
1359		usb_hcd_giveback_urb(hcd, urb, status);
1360	}
1361	return 1;
1362}
1363
1364static int
1365max3421_spi_thread(void *dev_id)
1366{
1367	struct usb_hcd *hcd = dev_id;
1368	struct spi_device *spi = to_spi_device(hcd->self.controller);
1369	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1370	int i, i_worked = 1;
1371
1372	/* set full-duplex SPI mode, low-active interrupt pin: */
1373	spi_wr8(hcd, MAX3421_REG_PINCTL,
1374		(BIT(MAX3421_PINCTL_FDUPSPI_BIT) |	/* full-duplex */
1375		 BIT(MAX3421_PINCTL_INTLEVEL_BIT)));	/* low-active irq */
1376
1377	while (!kthread_should_stop()) {
1378		max3421_hcd->rev = spi_rd8(hcd, MAX3421_REG_REVISION);
1379		if (max3421_hcd->rev == 0x12 || max3421_hcd->rev == 0x13)
1380			break;
1381		dev_err(&spi->dev, "bad rev 0x%02x", max3421_hcd->rev);
1382		msleep(10000);
1383	}
1384	dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n",
1385		 max3421_hcd->rev, spi->max_speed_hz, spi->bits_per_word,
1386		 spi->irq);
1387
1388	while (!kthread_should_stop()) {
1389		if (!i_worked) {
1390			/*
1391			 * We'll be waiting for wakeups from the hard
1392			 * interrupt handler, so now is a good time to
1393			 * sync our hien with the chip:
1394			 */
1395			spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1396
1397			set_current_state(TASK_INTERRUPTIBLE);
1398			if (test_and_clear_bit(ENABLE_IRQ, &max3421_hcd->todo))
1399				enable_irq(spi->irq);
1400			schedule();
1401			__set_current_state(TASK_RUNNING);
1402		}
1403
1404		i_worked = 0;
1405
1406		if (max3421_hcd->urb_done)
1407			i_worked |= max3421_urb_done(hcd);
1408		else if (max3421_handle_irqs(hcd))
1409			i_worked = 1;
1410		else if (!max3421_hcd->curr_urb)
1411			i_worked |= max3421_select_and_start_urb(hcd);
1412
1413		if (test_and_clear_bit(RESET_HCD, &max3421_hcd->todo))
1414			/* reset the HCD: */
1415			i_worked |= max3421_reset_hcd(hcd);
1416		if (test_and_clear_bit(RESET_PORT, &max3421_hcd->todo)) {
1417			/* perform a USB bus reset: */
1418			spi_wr8(hcd, MAX3421_REG_HCTL,
1419				BIT(MAX3421_HCTL_BUSRST_BIT));
1420			i_worked = 1;
1421		}
1422		if (test_and_clear_bit(CHECK_UNLINK, &max3421_hcd->todo))
1423			i_worked |= max3421_check_unlink(hcd);
1424		if (test_and_clear_bit(IOPIN_UPDATE, &max3421_hcd->todo)) {
1425			/*
1426			 * IOPINS1/IOPINS2 do not auto-increment, so we can't
1427			 * use spi_wr_buf().
1428			 */
1429			for (i = 0; i < ARRAY_SIZE(max3421_hcd->iopins); ++i) {
1430				u8 val = spi_rd8(hcd, MAX3421_REG_IOPINS1 + i);
1431
1432				val = ((val & 0xf0) |
1433				       (max3421_hcd->iopins[i] & 0x0f));
1434				spi_wr8(hcd, MAX3421_REG_IOPINS1 + i, val);
1435				max3421_hcd->iopins[i] = val;
1436			}
1437			i_worked = 1;
1438		}
1439	}
1440	set_current_state(TASK_RUNNING);
1441	dev_info(&spi->dev, "SPI thread exiting");
1442	return 0;
1443}
1444
1445static int
1446max3421_reset_port(struct usb_hcd *hcd)
1447{
1448	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1449
1450	max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
1451				      USB_PORT_STAT_LOW_SPEED);
1452	max3421_hcd->port_status |= USB_PORT_STAT_RESET;
1453	set_bit(RESET_PORT, &max3421_hcd->todo);
1454	wake_up_process(max3421_hcd->spi_thread);
1455	return 0;
1456}
1457
1458static int
1459max3421_reset(struct usb_hcd *hcd)
1460{
1461	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1462
1463	hcd->self.sg_tablesize = 0;
1464	hcd->speed = HCD_USB2;
1465	hcd->self.root_hub->speed = USB_SPEED_FULL;
1466	set_bit(RESET_HCD, &max3421_hcd->todo);
1467	wake_up_process(max3421_hcd->spi_thread);
1468	return 0;
1469}
1470
1471static int
1472max3421_start(struct usb_hcd *hcd)
1473{
1474	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1475
1476	spin_lock_init(&max3421_hcd->lock);
1477	max3421_hcd->rh_state = MAX3421_RH_RUNNING;
1478
1479	INIT_LIST_HEAD(&max3421_hcd->ep_list);
1480
1481	hcd->power_budget = POWER_BUDGET;
1482	hcd->state = HC_STATE_RUNNING;
1483	hcd->uses_new_polling = 1;
1484	return 0;
1485}
1486
1487static void
1488max3421_stop(struct usb_hcd *hcd)
1489{
1490}
1491
1492static int
1493max3421_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1494{
1495	struct spi_device *spi = to_spi_device(hcd->self.controller);
1496	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1497	struct max3421_ep *max3421_ep;
1498	unsigned long flags;
1499	int retval;
1500
1501	switch (usb_pipetype(urb->pipe)) {
1502	case PIPE_INTERRUPT:
1503	case PIPE_ISOCHRONOUS:
1504		if (urb->interval < 0) {
1505			dev_err(&spi->dev,
1506			  "%s: interval=%d for intr-/iso-pipe; expected > 0\n",
1507				__func__, urb->interval);
1508			return -EINVAL;
1509		}
1510		break;
1511	default:
1512		break;
1513	}
1514
1515	spin_lock_irqsave(&max3421_hcd->lock, flags);
1516
1517	max3421_ep = urb->ep->hcpriv;
1518	if (!max3421_ep) {
1519		/* gets freed in max3421_endpoint_disable: */
1520		max3421_ep = kzalloc(sizeof(struct max3421_ep), GFP_ATOMIC);
1521		if (!max3421_ep) {
1522			retval = -ENOMEM;
1523			goto out;
1524		}
1525		max3421_ep->ep = urb->ep;
1526		max3421_ep->last_active = max3421_hcd->frame_number;
1527		urb->ep->hcpriv = max3421_ep;
1528
1529		list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list);
1530	}
1531
1532	retval = usb_hcd_link_urb_to_ep(hcd, urb);
1533	if (retval == 0) {
1534		/* Since we added to the queue, restart scheduling: */
1535		max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1536		wake_up_process(max3421_hcd->spi_thread);
1537	}
1538
1539out:
1540	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1541	return retval;
1542}
1543
1544static int
1545max3421_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1546{
1547	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1548	unsigned long flags;
1549	int retval;
1550
1551	spin_lock_irqsave(&max3421_hcd->lock, flags);
1552
1553	/*
1554	 * This will set urb->unlinked which in turn causes the entry
1555	 * to be dropped at the next opportunity.
1556	 */
1557	retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1558	if (retval == 0) {
1559		set_bit(CHECK_UNLINK, &max3421_hcd->todo);
1560		wake_up_process(max3421_hcd->spi_thread);
1561	}
1562	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1563	return retval;
1564}
1565
1566static void
1567max3421_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1568{
1569	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1570	unsigned long flags;
1571
1572	spin_lock_irqsave(&max3421_hcd->lock, flags);
1573
1574	if (ep->hcpriv) {
1575		struct max3421_ep *max3421_ep = ep->hcpriv;
1576
1577		/* remove myself from the ep_list: */
1578		if (!list_empty(&max3421_ep->ep_list))
1579			list_del(&max3421_ep->ep_list);
1580		kfree(max3421_ep);
1581		ep->hcpriv = NULL;
1582	}
1583
1584	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1585}
1586
1587static int
1588max3421_get_frame_number(struct usb_hcd *hcd)
1589{
1590	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1591	return max3421_hcd->frame_number;
1592}
1593
1594/*
1595 * Should return a non-zero value when any port is undergoing a resume
1596 * transition while the root hub is suspended.
1597 */
1598static int
1599max3421_hub_status_data(struct usb_hcd *hcd, char *buf)
1600{
1601	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1602	unsigned long flags;
1603	int retval = 0;
1604
1605	spin_lock_irqsave(&max3421_hcd->lock, flags);
1606	if (!HCD_HW_ACCESSIBLE(hcd))
1607		goto done;
1608
1609	*buf = 0;
1610	if ((max3421_hcd->port_status & PORT_C_MASK) != 0) {
1611		*buf = (1 << 1); /* a hub over-current condition exists */
1612		dev_dbg(hcd->self.controller,
1613			"port status 0x%08x has changes\n",
1614			max3421_hcd->port_status);
1615		retval = 1;
1616		if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED)
1617			usb_hcd_resume_root_hub(hcd);
1618	}
1619done:
1620	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1621	return retval;
1622}
1623
1624static inline void
1625hub_descriptor(struct usb_hub_descriptor *desc)
1626{
1627	memset(desc, 0, sizeof(*desc));
1628	/*
1629	 * See Table 11-13: Hub Descriptor in USB 2.0 spec.
1630	 */
1631	desc->bDescriptorType = USB_DT_HUB; /* hub descriptor */
1632	desc->bDescLength = 9;
1633	desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM |
1634						HUB_CHAR_COMMON_OCPM);
1635	desc->bNbrPorts = 1;
1636}
1637
1638/*
1639 * Set the MAX3421E general-purpose output with number PIN_NUMBER to
1640 * VALUE (0 or 1).  PIN_NUMBER may be in the range from 1-8.  For
1641 * any other value, this function acts as a no-op.
1642 */
1643static void
1644max3421_gpout_set_value(struct usb_hcd *hcd, u8 pin_number, u8 value)
1645{
1646	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1647	u8 mask, idx;
1648
1649	--pin_number;
1650	if (pin_number >= MAX3421_GPOUT_COUNT)
1651		return;
1652
1653	mask = 1u << (pin_number % 4);
1654	idx = pin_number / 4;
1655
1656	if (value)
1657		max3421_hcd->iopins[idx] |=  mask;
1658	else
1659		max3421_hcd->iopins[idx] &= ~mask;
1660	set_bit(IOPIN_UPDATE, &max3421_hcd->todo);
1661	wake_up_process(max3421_hcd->spi_thread);
1662}
1663
1664static int
1665max3421_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
1666		    char *buf, u16 length)
1667{
1668	struct spi_device *spi = to_spi_device(hcd->self.controller);
1669	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1670	struct max3421_hcd_platform_data *pdata;
1671	unsigned long flags;
1672	int retval = 0;
1673
1674	pdata = spi->dev.platform_data;
1675
1676	spin_lock_irqsave(&max3421_hcd->lock, flags);
1677
1678	switch (type_req) {
1679	case ClearHubFeature:
1680		break;
1681	case ClearPortFeature:
1682		switch (value) {
1683		case USB_PORT_FEAT_SUSPEND:
1684			break;
1685		case USB_PORT_FEAT_POWER:
1686			dev_dbg(hcd->self.controller, "power-off\n");
1687			max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1688						!pdata->vbus_active_level);
1689			fallthrough;
1690		default:
1691			max3421_hcd->port_status &= ~(1 << value);
1692		}
1693		break;
1694	case GetHubDescriptor:
1695		hub_descriptor((struct usb_hub_descriptor *) buf);
1696		break;
1697
1698	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1699	case GetPortErrorCount:
1700	case SetHubDepth:
1701		/* USB3 only */
1702		goto error;
1703
1704	case GetHubStatus:
1705		*(__le32 *) buf = cpu_to_le32(0);
1706		break;
1707
1708	case GetPortStatus:
1709		if (index != 1) {
1710			retval = -EPIPE;
1711			goto error;
1712		}
1713		((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status);
1714		((__le16 *) buf)[1] =
1715			cpu_to_le16(max3421_hcd->port_status >> 16);
1716		break;
1717
1718	case SetHubFeature:
1719		retval = -EPIPE;
1720		break;
1721
1722	case SetPortFeature:
1723		switch (value) {
1724		case USB_PORT_FEAT_LINK_STATE:
1725		case USB_PORT_FEAT_U1_TIMEOUT:
1726		case USB_PORT_FEAT_U2_TIMEOUT:
1727		case USB_PORT_FEAT_BH_PORT_RESET:
1728			goto error;
1729		case USB_PORT_FEAT_SUSPEND:
1730			if (max3421_hcd->active)
1731				max3421_hcd->port_status |=
1732					USB_PORT_STAT_SUSPEND;
1733			break;
1734		case USB_PORT_FEAT_POWER:
1735			dev_dbg(hcd->self.controller, "power-on\n");
1736			max3421_hcd->port_status |= USB_PORT_STAT_POWER;
1737			max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1738						pdata->vbus_active_level);
1739			break;
1740		case USB_PORT_FEAT_RESET:
1741			max3421_reset_port(hcd);
1742			fallthrough;
1743		default:
1744			if ((max3421_hcd->port_status & USB_PORT_STAT_POWER)
1745			    != 0)
1746				max3421_hcd->port_status |= (1 << value);
1747		}
1748		break;
1749
1750	default:
1751		dev_dbg(hcd->self.controller,
1752			"hub control req%04x v%04x i%04x l%d\n",
1753			type_req, value, index, length);
1754error:		/* "protocol stall" on error */
1755		retval = -EPIPE;
1756	}
1757
1758	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1759	return retval;
1760}
1761
1762static int
1763max3421_bus_suspend(struct usb_hcd *hcd)
1764{
1765	return -1;
1766}
1767
1768static int
1769max3421_bus_resume(struct usb_hcd *hcd)
1770{
1771	return -1;
1772}
1773
1774static const struct hc_driver max3421_hcd_desc = {
1775	.description =		"max3421",
1776	.product_desc =		DRIVER_DESC,
1777	.hcd_priv_size =	sizeof(struct max3421_hcd),
1778	.flags =		HCD_USB11,
1779	.reset =		max3421_reset,
1780	.start =		max3421_start,
1781	.stop =			max3421_stop,
1782	.get_frame_number =	max3421_get_frame_number,
1783	.urb_enqueue =		max3421_urb_enqueue,
1784	.urb_dequeue =		max3421_urb_dequeue,
1785	.endpoint_disable =	max3421_endpoint_disable,
1786	.hub_status_data =	max3421_hub_status_data,
1787	.hub_control =		max3421_hub_control,
1788	.bus_suspend =		max3421_bus_suspend,
1789	.bus_resume =		max3421_bus_resume,
1790};
1791
1792static int
1793max3421_of_vbus_en_pin(struct device *dev, struct max3421_hcd_platform_data *pdata)
1794{
1795	int retval;
1796	uint32_t value[2];
1797
1798	if (!pdata)
1799		return -EINVAL;
1800
1801	retval = of_property_read_u32_array(dev->of_node, "maxim,vbus-en-pin", value, 2);
1802	if (retval) {
1803		dev_err(dev, "device tree node property 'maxim,vbus-en-pin' is missing\n");
1804		return retval;
1805	}
1806	dev_info(dev, "property 'maxim,vbus-en-pin' value is <%d %d>\n", value[0], value[1]);
1807
1808	pdata->vbus_gpout = value[0];
1809	pdata->vbus_active_level = value[1];
1810
1811	return 0;
1812}
1813
1814static int
1815max3421_probe(struct spi_device *spi)
1816{
1817	struct device *dev = &spi->dev;
1818	struct max3421_hcd *max3421_hcd;
1819	struct usb_hcd *hcd = NULL;
1820	struct max3421_hcd_platform_data *pdata = NULL;
1821	int retval;
1822
1823	if (spi_setup(spi) < 0) {
1824		dev_err(&spi->dev, "Unable to setup SPI bus");
1825		return -EFAULT;
1826	}
1827
1828	if (!spi->irq) {
1829		dev_err(dev, "Failed to get SPI IRQ");
1830		return -EFAULT;
1831	}
1832
1833	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1834		pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1835		if (!pdata) {
1836			retval = -ENOMEM;
1837			goto error;
1838		}
1839		retval = max3421_of_vbus_en_pin(dev, pdata);
1840		if (retval)
1841			goto error;
1842
1843		spi->dev.platform_data = pdata;
1844	}
1845
1846	pdata = spi->dev.platform_data;
1847	if (!pdata) {
1848		dev_err(&spi->dev, "driver configuration data is not provided\n");
1849		retval = -EFAULT;
1850		goto error;
1851	}
1852	if (pdata->vbus_active_level > 1) {
1853		dev_err(&spi->dev, "vbus active level value %d is out of range (0/1)\n", pdata->vbus_active_level);
1854		retval = -EINVAL;
1855		goto error;
1856	}
1857	if (pdata->vbus_gpout < 1 || pdata->vbus_gpout > MAX3421_GPOUT_COUNT) {
1858		dev_err(&spi->dev, "vbus gpout value %d is out of range (1..8)\n", pdata->vbus_gpout);
1859		retval = -EINVAL;
1860		goto error;
1861	}
1862
1863	retval = -ENOMEM;
1864	hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev,
1865			     dev_name(&spi->dev));
1866	if (!hcd) {
1867		dev_err(&spi->dev, "failed to create HCD structure\n");
1868		goto error;
1869	}
1870	set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1871	max3421_hcd = hcd_to_max3421(hcd);
1872	INIT_LIST_HEAD(&max3421_hcd->ep_list);
1873	spi_set_drvdata(spi, max3421_hcd);
1874
1875	max3421_hcd->tx = kmalloc(sizeof(*max3421_hcd->tx), GFP_KERNEL);
1876	if (!max3421_hcd->tx)
1877		goto error;
1878	max3421_hcd->rx = kmalloc(sizeof(*max3421_hcd->rx), GFP_KERNEL);
1879	if (!max3421_hcd->rx)
1880		goto error;
1881
1882	max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd,
1883					      "max3421_spi_thread");
1884	if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) {
1885		dev_err(&spi->dev,
1886			"failed to create SPI thread (out of memory)\n");
1887		goto error;
1888	}
1889
1890	retval = usb_add_hcd(hcd, 0, 0);
1891	if (retval) {
1892		dev_err(&spi->dev, "failed to add HCD\n");
1893		goto error;
1894	}
1895
1896	retval = request_irq(spi->irq, max3421_irq_handler,
1897			     IRQF_TRIGGER_LOW, "max3421", hcd);
1898	if (retval < 0) {
1899		dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
1900		goto error;
1901	}
1902	return 0;
1903
1904error:
1905	if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) {
1906		devm_kfree(&spi->dev, pdata);
1907		spi->dev.platform_data = NULL;
1908	}
1909
1910	if (hcd) {
1911		kfree(max3421_hcd->tx);
1912		kfree(max3421_hcd->rx);
1913		if (max3421_hcd->spi_thread)
1914			kthread_stop(max3421_hcd->spi_thread);
1915		usb_put_hcd(hcd);
1916	}
1917	return retval;
1918}
1919
1920static void
1921max3421_remove(struct spi_device *spi)
1922{
1923	struct max3421_hcd *max3421_hcd;
1924	struct usb_hcd *hcd;
1925	unsigned long flags;
1926
1927	max3421_hcd = spi_get_drvdata(spi);
1928	hcd = max3421_to_hcd(max3421_hcd);
1929
1930	usb_remove_hcd(hcd);
1931
1932	spin_lock_irqsave(&max3421_hcd->lock, flags);
1933
1934	kthread_stop(max3421_hcd->spi_thread);
1935
1936	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1937
1938	free_irq(spi->irq, hcd);
1939
1940	usb_put_hcd(hcd);
1941}
1942
1943static const struct of_device_id max3421_of_match_table[] = {
1944	{ .compatible = "maxim,max3421", },
1945	{},
1946};
1947MODULE_DEVICE_TABLE(of, max3421_of_match_table);
1948
1949static struct spi_driver max3421_driver = {
1950	.probe		= max3421_probe,
1951	.remove		= max3421_remove,
1952	.driver		= {
1953		.name	= "max3421-hcd",
1954		.of_match_table = max3421_of_match_table,
1955	},
1956};
1957
1958module_spi_driver(max3421_driver);
1959
1960MODULE_DESCRIPTION(DRIVER_DESC);
1961MODULE_AUTHOR("David Mosberger <davidm@egauge.net>");
1962MODULE_LICENSE("GPL");
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * MAX3421 Host Controller driver for USB.
   4 *
   5 * Author: David Mosberger-Tang <davidm@egauge.net>
   6 *
   7 * (C) Copyright 2014 David Mosberger-Tang <davidm@egauge.net>
   8 *
   9 * MAX3421 is a chip implementing a USB 2.0 Full-/Low-Speed host
  10 * controller on a SPI bus.
  11 *
  12 * Based on:
  13 *	o MAX3421E datasheet
  14 *		https://datasheets.maximintegrated.com/en/ds/MAX3421E.pdf
  15 *	o MAX3421E Programming Guide
  16 *		https://www.hdl.co.jp/ftpdata/utl-001/AN3785.pdf
  17 *	o gadget/dummy_hcd.c
  18 *		For USB HCD implementation.
  19 *	o Arduino MAX3421 driver
  20 *	     https://github.com/felis/USB_Host_Shield_2.0/blob/master/Usb.cpp
  21 *
  22 * This file is licenced under the GPL v2.
  23 *
  24 * Important note on worst-case (full-speed) packet size constraints
  25 * (See USB 2.0 Section 5.6.3 and following):
  26 *
  27 *	- control:	  64 bytes
  28 *	- isochronous:	1023 bytes
  29 *	- interrupt:	  64 bytes
  30 *	- bulk:		  64 bytes
  31 *
  32 * Since the MAX3421 FIFO size is 64 bytes, we do not have to work about
  33 * multi-FIFO writes/reads for a single USB packet *except* for isochronous
  34 * transfers.  We don't support isochronous transfers at this time, so we
  35 * just assume that a USB packet always fits into a single FIFO buffer.
  36 *
  37 * NOTE: The June 2006 version of "MAX3421E Programming Guide"
  38 * (AN3785) has conflicting info for the RCVDAVIRQ bit:
  39 *
  40 *	The description of RCVDAVIRQ says "The CPU *must* clear
  41 *	this IRQ bit (by writing a 1 to it) before reading the
  42 *	RCVFIFO data.
  43 *
  44 * However, the earlier section on "Programming BULK-IN
  45 * Transfers" says * that:
  46 *
  47 *	After the CPU retrieves the data, it clears the
  48 *	RCVDAVIRQ bit.
  49 *
  50 * The December 2006 version has been corrected and it consistently
  51 * states the second behavior is the correct one.
  52 *
  53 * Synchronous SPI transactions sleep so we can't perform any such
  54 * transactions while holding a spin-lock (and/or while interrupts are
  55 * masked).  To achieve this, all SPI transactions are issued from a
  56 * single thread (max3421_spi_thread).
  57 */
  58
  59#include <linux/jiffies.h>
  60#include <linux/module.h>
  61#include <linux/spi/spi.h>
  62#include <linux/usb.h>
  63#include <linux/usb/hcd.h>
  64#include <linux/of.h>
  65
  66#include <linux/platform_data/max3421-hcd.h>
  67
  68#define DRIVER_DESC	"MAX3421 USB Host-Controller Driver"
  69#define DRIVER_VERSION	"1.0"
  70
  71/* 11-bit counter that wraps around (USB 2.0 Section 8.3.3): */
  72#define USB_MAX_FRAME_NUMBER	0x7ff
  73#define USB_MAX_RETRIES		3 /* # of retries before error is reported */
  74
  75/*
  76 * Max. # of times we're willing to retransmit a request immediately in
  77 * resposne to a NAK.  Afterwards, we fall back on trying once a frame.
  78 */
  79#define NAK_MAX_FAST_RETRANSMITS	2
  80
  81#define POWER_BUDGET	500	/* in mA; use 8 for low-power port testing */
  82
  83/* Port-change mask: */
  84#define PORT_C_MASK	((USB_PORT_STAT_C_CONNECTION |	\
  85			  USB_PORT_STAT_C_ENABLE |	\
  86			  USB_PORT_STAT_C_SUSPEND |	\
  87			  USB_PORT_STAT_C_OVERCURRENT | \
  88			  USB_PORT_STAT_C_RESET) << 16)
  89
  90#define MAX3421_GPOUT_COUNT	8
  91
  92enum max3421_rh_state {
  93	MAX3421_RH_RESET,
  94	MAX3421_RH_SUSPENDED,
  95	MAX3421_RH_RUNNING
  96};
  97
  98enum pkt_state {
  99	PKT_STATE_SETUP,	/* waiting to send setup packet to ctrl pipe */
 100	PKT_STATE_TRANSFER,	/* waiting to xfer transfer_buffer */
 101	PKT_STATE_TERMINATE	/* waiting to terminate control transfer */
 102};
 103
 104enum scheduling_pass {
 105	SCHED_PASS_PERIODIC,
 106	SCHED_PASS_NON_PERIODIC,
 107	SCHED_PASS_DONE
 108};
 109
 110/* Bit numbers for max3421_hcd->todo: */
 111enum {
 112	ENABLE_IRQ = 0,
 113	RESET_HCD,
 114	RESET_PORT,
 115	CHECK_UNLINK,
 116	IOPIN_UPDATE
 117};
 118
 119struct max3421_dma_buf {
 120	u8 data[2];
 121};
 122
 123struct max3421_hcd {
 124	spinlock_t lock;
 125
 126	struct task_struct *spi_thread;
 127
 128	enum max3421_rh_state rh_state;
 129	/* lower 16 bits contain port status, upper 16 bits the change mask: */
 130	u32 port_status;
 131
 132	unsigned active:1;
 133
 134	struct list_head ep_list;	/* list of EP's with work */
 135
 136	/*
 137	 * The following are owned by spi_thread (may be accessed by
 138	 * SPI-thread without acquiring the HCD lock:
 139	 */
 140	u8 rev;				/* chip revision */
 141	u16 frame_number;
 142	/*
 143	 * kmalloc'd buffers guaranteed to be in separate (DMA)
 144	 * cache-lines:
 145	 */
 146	struct max3421_dma_buf *tx;
 147	struct max3421_dma_buf *rx;
 148	/*
 149	 * URB we're currently processing.  Must not be reset to NULL
 150	 * unless MAX3421E chip is idle:
 151	 */
 152	struct urb *curr_urb;
 153	enum scheduling_pass sched_pass;
 154	int urb_done;			/* > 0 -> no errors, < 0: errno */
 155	size_t curr_len;
 156	u8 hien;
 157	u8 mode;
 158	u8 iopins[2];
 159	unsigned long todo;
 160#ifdef DEBUG
 161	unsigned long err_stat[16];
 162#endif
 163};
 164
 165struct max3421_ep {
 166	struct usb_host_endpoint *ep;
 167	struct list_head ep_list;
 168	u32 naks;
 169	u16 last_active;		/* frame # this ep was last active */
 170	enum pkt_state pkt_state;
 171	u8 retries;
 172	u8 retransmit;			/* packet needs retransmission */
 173};
 174
 175#define MAX3421_FIFO_SIZE	64
 176
 177#define MAX3421_SPI_DIR_RD	0	/* read register from MAX3421 */
 178#define MAX3421_SPI_DIR_WR	1	/* write register to MAX3421 */
 179
 180/* SPI commands: */
 181#define MAX3421_SPI_DIR_SHIFT	1
 182#define MAX3421_SPI_REG_SHIFT	3
 183
 184#define MAX3421_REG_RCVFIFO	1
 185#define MAX3421_REG_SNDFIFO	2
 186#define MAX3421_REG_SUDFIFO	4
 187#define MAX3421_REG_RCVBC	6
 188#define MAX3421_REG_SNDBC	7
 189#define MAX3421_REG_USBIRQ	13
 190#define MAX3421_REG_USBIEN	14
 191#define MAX3421_REG_USBCTL	15
 192#define MAX3421_REG_CPUCTL	16
 193#define MAX3421_REG_PINCTL	17
 194#define MAX3421_REG_REVISION	18
 195#define MAX3421_REG_IOPINS1	20
 196#define MAX3421_REG_IOPINS2	21
 197#define MAX3421_REG_GPINIRQ	22
 198#define MAX3421_REG_GPINIEN	23
 199#define MAX3421_REG_GPINPOL	24
 200#define MAX3421_REG_HIRQ	25
 201#define MAX3421_REG_HIEN	26
 202#define MAX3421_REG_MODE	27
 203#define MAX3421_REG_PERADDR	28
 204#define MAX3421_REG_HCTL	29
 205#define MAX3421_REG_HXFR	30
 206#define MAX3421_REG_HRSL	31
 207
 208enum {
 209	MAX3421_USBIRQ_OSCOKIRQ_BIT = 0,
 210	MAX3421_USBIRQ_NOVBUSIRQ_BIT = 5,
 211	MAX3421_USBIRQ_VBUSIRQ_BIT
 212};
 213
 214enum {
 215	MAX3421_CPUCTL_IE_BIT = 0,
 216	MAX3421_CPUCTL_PULSEWID0_BIT = 6,
 217	MAX3421_CPUCTL_PULSEWID1_BIT
 218};
 219
 220enum {
 221	MAX3421_USBCTL_PWRDOWN_BIT = 4,
 222	MAX3421_USBCTL_CHIPRES_BIT
 223};
 224
 225enum {
 226	MAX3421_PINCTL_GPXA_BIT	= 0,
 227	MAX3421_PINCTL_GPXB_BIT,
 228	MAX3421_PINCTL_POSINT_BIT,
 229	MAX3421_PINCTL_INTLEVEL_BIT,
 230	MAX3421_PINCTL_FDUPSPI_BIT,
 231	MAX3421_PINCTL_EP0INAK_BIT,
 232	MAX3421_PINCTL_EP2INAK_BIT,
 233	MAX3421_PINCTL_EP3INAK_BIT,
 234};
 235
 236enum {
 237	MAX3421_HI_BUSEVENT_BIT = 0,	/* bus-reset/-resume */
 238	MAX3421_HI_RWU_BIT,		/* remote wakeup */
 239	MAX3421_HI_RCVDAV_BIT,		/* receive FIFO data available */
 240	MAX3421_HI_SNDBAV_BIT,		/* send buffer available */
 241	MAX3421_HI_SUSDN_BIT,		/* suspend operation done */
 242	MAX3421_HI_CONDET_BIT,		/* peripheral connect/disconnect */
 243	MAX3421_HI_FRAME_BIT,		/* frame generator */
 244	MAX3421_HI_HXFRDN_BIT,		/* host transfer done */
 245};
 246
 247enum {
 248	MAX3421_HCTL_BUSRST_BIT = 0,
 249	MAX3421_HCTL_FRMRST_BIT,
 250	MAX3421_HCTL_SAMPLEBUS_BIT,
 251	MAX3421_HCTL_SIGRSM_BIT,
 252	MAX3421_HCTL_RCVTOG0_BIT,
 253	MAX3421_HCTL_RCVTOG1_BIT,
 254	MAX3421_HCTL_SNDTOG0_BIT,
 255	MAX3421_HCTL_SNDTOG1_BIT
 256};
 257
 258enum {
 259	MAX3421_MODE_HOST_BIT = 0,
 260	MAX3421_MODE_LOWSPEED_BIT,
 261	MAX3421_MODE_HUBPRE_BIT,
 262	MAX3421_MODE_SOFKAENAB_BIT,
 263	MAX3421_MODE_SEPIRQ_BIT,
 264	MAX3421_MODE_DELAYISO_BIT,
 265	MAX3421_MODE_DMPULLDN_BIT,
 266	MAX3421_MODE_DPPULLDN_BIT
 267};
 268
 269enum {
 270	MAX3421_HRSL_OK = 0,
 271	MAX3421_HRSL_BUSY,
 272	MAX3421_HRSL_BADREQ,
 273	MAX3421_HRSL_UNDEF,
 274	MAX3421_HRSL_NAK,
 275	MAX3421_HRSL_STALL,
 276	MAX3421_HRSL_TOGERR,
 277	MAX3421_HRSL_WRONGPID,
 278	MAX3421_HRSL_BADBC,
 279	MAX3421_HRSL_PIDERR,
 280	MAX3421_HRSL_PKTERR,
 281	MAX3421_HRSL_CRCERR,
 282	MAX3421_HRSL_KERR,
 283	MAX3421_HRSL_JERR,
 284	MAX3421_HRSL_TIMEOUT,
 285	MAX3421_HRSL_BABBLE,
 286	MAX3421_HRSL_RESULT_MASK = 0xf,
 287	MAX3421_HRSL_RCVTOGRD_BIT = 4,
 288	MAX3421_HRSL_SNDTOGRD_BIT,
 289	MAX3421_HRSL_KSTATUS_BIT,
 290	MAX3421_HRSL_JSTATUS_BIT
 291};
 292
 293/* Return same error-codes as ohci.h:cc_to_error: */
 294static const int hrsl_to_error[] = {
 295	[MAX3421_HRSL_OK] =		0,
 296	[MAX3421_HRSL_BUSY] =		-EINVAL,
 297	[MAX3421_HRSL_BADREQ] =		-EINVAL,
 298	[MAX3421_HRSL_UNDEF] =		-EINVAL,
 299	[MAX3421_HRSL_NAK] =		-EAGAIN,
 300	[MAX3421_HRSL_STALL] =		-EPIPE,
 301	[MAX3421_HRSL_TOGERR] =		-EILSEQ,
 302	[MAX3421_HRSL_WRONGPID] =	-EPROTO,
 303	[MAX3421_HRSL_BADBC] =		-EREMOTEIO,
 304	[MAX3421_HRSL_PIDERR] =		-EPROTO,
 305	[MAX3421_HRSL_PKTERR] =		-EPROTO,
 306	[MAX3421_HRSL_CRCERR] =		-EILSEQ,
 307	[MAX3421_HRSL_KERR] =		-EIO,
 308	[MAX3421_HRSL_JERR] =		-EIO,
 309	[MAX3421_HRSL_TIMEOUT] =	-ETIME,
 310	[MAX3421_HRSL_BABBLE] =		-EOVERFLOW
 311};
 312
 313/*
 314 * See https://www.beyondlogic.org/usbnutshell/usb4.shtml#Control for a
 315 * reasonable overview of how control transfers use the IN/OUT
 316 * tokens.
 317 */
 318#define MAX3421_HXFR_BULK_IN(ep)	(0x00 | (ep))	/* bulk or interrupt */
 319#define MAX3421_HXFR_SETUP		 0x10
 320#define MAX3421_HXFR_BULK_OUT(ep)	(0x20 | (ep))	/* bulk or interrupt */
 321#define MAX3421_HXFR_ISO_IN(ep)		(0x40 | (ep))
 322#define MAX3421_HXFR_ISO_OUT(ep)	(0x60 | (ep))
 323#define MAX3421_HXFR_HS_IN		 0x80		/* handshake in */
 324#define MAX3421_HXFR_HS_OUT		 0xa0		/* handshake out */
 325
 326#define field(val, bit)	((val) << (bit))
 327
 328static inline s16
 329frame_diff(u16 left, u16 right)
 330{
 331	return ((unsigned) (left - right)) % (USB_MAX_FRAME_NUMBER + 1);
 332}
 333
 334static inline struct max3421_hcd *
 335hcd_to_max3421(struct usb_hcd *hcd)
 336{
 337	return (struct max3421_hcd *) hcd->hcd_priv;
 338}
 339
 340static inline struct usb_hcd *
 341max3421_to_hcd(struct max3421_hcd *max3421_hcd)
 342{
 343	return container_of((void *) max3421_hcd, struct usb_hcd, hcd_priv);
 344}
 345
 346static u8
 347spi_rd8(struct usb_hcd *hcd, unsigned int reg)
 348{
 349	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 350	struct spi_device *spi = to_spi_device(hcd->self.controller);
 351	struct spi_transfer transfer;
 352	struct spi_message msg;
 353
 354	memset(&transfer, 0, sizeof(transfer));
 355
 356	spi_message_init(&msg);
 357
 358	max3421_hcd->tx->data[0] =
 359		(field(reg, MAX3421_SPI_REG_SHIFT) |
 360		 field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
 361
 362	transfer.tx_buf = max3421_hcd->tx->data;
 363	transfer.rx_buf = max3421_hcd->rx->data;
 364	transfer.len = 2;
 365
 366	spi_message_add_tail(&transfer, &msg);
 367	spi_sync(spi, &msg);
 368
 369	return max3421_hcd->rx->data[1];
 370}
 371
 372static void
 373spi_wr8(struct usb_hcd *hcd, unsigned int reg, u8 val)
 374{
 375	struct spi_device *spi = to_spi_device(hcd->self.controller);
 376	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 377	struct spi_transfer transfer;
 378	struct spi_message msg;
 379
 380	memset(&transfer, 0, sizeof(transfer));
 381
 382	spi_message_init(&msg);
 383
 384	max3421_hcd->tx->data[0] =
 385		(field(reg, MAX3421_SPI_REG_SHIFT) |
 386		 field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
 387	max3421_hcd->tx->data[1] = val;
 388
 389	transfer.tx_buf = max3421_hcd->tx->data;
 390	transfer.len = 2;
 391
 392	spi_message_add_tail(&transfer, &msg);
 393	spi_sync(spi, &msg);
 394}
 395
 396static void
 397spi_rd_buf(struct usb_hcd *hcd, unsigned int reg, void *buf, size_t len)
 398{
 399	struct spi_device *spi = to_spi_device(hcd->self.controller);
 400	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 401	struct spi_transfer transfer[2];
 402	struct spi_message msg;
 403
 404	memset(transfer, 0, sizeof(transfer));
 405
 406	spi_message_init(&msg);
 407
 408	max3421_hcd->tx->data[0] =
 409		(field(reg, MAX3421_SPI_REG_SHIFT) |
 410		 field(MAX3421_SPI_DIR_RD, MAX3421_SPI_DIR_SHIFT));
 411	transfer[0].tx_buf = max3421_hcd->tx->data;
 412	transfer[0].len = 1;
 413
 414	transfer[1].rx_buf = buf;
 415	transfer[1].len = len;
 416
 417	spi_message_add_tail(&transfer[0], &msg);
 418	spi_message_add_tail(&transfer[1], &msg);
 419	spi_sync(spi, &msg);
 420}
 421
 422static void
 423spi_wr_buf(struct usb_hcd *hcd, unsigned int reg, void *buf, size_t len)
 424{
 425	struct spi_device *spi = to_spi_device(hcd->self.controller);
 426	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 427	struct spi_transfer transfer[2];
 428	struct spi_message msg;
 429
 430	memset(transfer, 0, sizeof(transfer));
 431
 432	spi_message_init(&msg);
 433
 434	max3421_hcd->tx->data[0] =
 435		(field(reg, MAX3421_SPI_REG_SHIFT) |
 436		 field(MAX3421_SPI_DIR_WR, MAX3421_SPI_DIR_SHIFT));
 437
 438	transfer[0].tx_buf = max3421_hcd->tx->data;
 439	transfer[0].len = 1;
 440
 441	transfer[1].tx_buf = buf;
 442	transfer[1].len = len;
 443
 444	spi_message_add_tail(&transfer[0], &msg);
 445	spi_message_add_tail(&transfer[1], &msg);
 446	spi_sync(spi, &msg);
 447}
 448
 449/*
 450 * Figure out the correct setting for the LOWSPEED and HUBPRE mode
 451 * bits.  The HUBPRE bit needs to be set when MAX3421E operates at
 452 * full speed, but it's talking to a low-speed device (i.e., through a
 453 * hub).  Setting that bit ensures that every low-speed packet is
 454 * preceded by a full-speed PRE PID.  Possible configurations:
 455 *
 456 * Hub speed:	Device speed:	=>	LOWSPEED bit:	HUBPRE bit:
 457 *	FULL	FULL		=>	0		0
 458 *	FULL	LOW		=>	1		1
 459 *	LOW	LOW		=>	1		0
 460 *	LOW	FULL		=>	1		0
 461 */
 462static void
 463max3421_set_speed(struct usb_hcd *hcd, struct usb_device *dev)
 464{
 465	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 466	u8 mode_lowspeed, mode_hubpre, mode = max3421_hcd->mode;
 467
 468	mode_lowspeed = BIT(MAX3421_MODE_LOWSPEED_BIT);
 469	mode_hubpre   = BIT(MAX3421_MODE_HUBPRE_BIT);
 470	if (max3421_hcd->port_status & USB_PORT_STAT_LOW_SPEED) {
 471		mode |=  mode_lowspeed;
 472		mode &= ~mode_hubpre;
 473	} else if (dev->speed == USB_SPEED_LOW) {
 474		mode |= mode_lowspeed | mode_hubpre;
 475	} else {
 476		mode &= ~(mode_lowspeed | mode_hubpre);
 477	}
 478	if (mode != max3421_hcd->mode) {
 479		max3421_hcd->mode = mode;
 480		spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
 481	}
 482
 483}
 484
 485/*
 486 * Caller must NOT hold HCD spinlock.
 487 */
 488static void
 489max3421_set_address(struct usb_hcd *hcd, struct usb_device *dev, int epnum)
 490{
 491	int rcvtog, sndtog;
 492	u8 hctl;
 493
 494	/* setup new endpoint's toggle bits: */
 495	rcvtog = usb_gettoggle(dev, epnum, 0);
 496	sndtog = usb_gettoggle(dev, epnum, 1);
 497	hctl = (BIT(rcvtog + MAX3421_HCTL_RCVTOG0_BIT) |
 498		BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
 499
 500	spi_wr8(hcd, MAX3421_REG_HCTL, hctl);
 501
 502	/*
 503	 * Note: devnum for one and the same device can change during
 504	 * address-assignment so it's best to just always load the
 505	 * address whenever the end-point changed/was forced.
 506	 */
 507	spi_wr8(hcd, MAX3421_REG_PERADDR, dev->devnum);
 508}
 509
 510static int
 511max3421_ctrl_setup(struct usb_hcd *hcd, struct urb *urb)
 512{
 513	spi_wr_buf(hcd, MAX3421_REG_SUDFIFO, urb->setup_packet, 8);
 514	return MAX3421_HXFR_SETUP;
 515}
 516
 517static int
 518max3421_transfer_in(struct usb_hcd *hcd, struct urb *urb)
 519{
 520	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 521	int epnum = usb_pipeendpoint(urb->pipe);
 522
 523	max3421_hcd->curr_len = 0;
 524	max3421_hcd->hien |= BIT(MAX3421_HI_RCVDAV_BIT);
 525	return MAX3421_HXFR_BULK_IN(epnum);
 526}
 527
 528static int
 529max3421_transfer_out(struct usb_hcd *hcd, struct urb *urb, int fast_retransmit)
 530{
 531	struct spi_device *spi = to_spi_device(hcd->self.controller);
 532	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 533	int epnum = usb_pipeendpoint(urb->pipe);
 534	u32 max_packet;
 535	void *src;
 536
 537	src = urb->transfer_buffer + urb->actual_length;
 538
 539	if (fast_retransmit) {
 540		if (max3421_hcd->rev == 0x12) {
 541			/* work around rev 0x12 bug: */
 542			spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
 543			spi_wr8(hcd, MAX3421_REG_SNDFIFO, ((u8 *) src)[0]);
 544			spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
 545		}
 546		return MAX3421_HXFR_BULK_OUT(epnum);
 547	}
 548
 549	max_packet = usb_maxpacket(urb->dev, urb->pipe);
 550
 551	if (max_packet > MAX3421_FIFO_SIZE) {
 552		/*
 553		 * We do not support isochronous transfers at this
 554		 * time.
 555		 */
 556		dev_err(&spi->dev,
 557			"%s: packet-size of %u too big (limit is %u bytes)",
 558			__func__, max_packet, MAX3421_FIFO_SIZE);
 559		max3421_hcd->urb_done = -EMSGSIZE;
 560		return -EMSGSIZE;
 561	}
 562	max3421_hcd->curr_len = min((urb->transfer_buffer_length -
 563				     urb->actual_length), max_packet);
 564
 565	spi_wr_buf(hcd, MAX3421_REG_SNDFIFO, src, max3421_hcd->curr_len);
 566	spi_wr8(hcd, MAX3421_REG_SNDBC, max3421_hcd->curr_len);
 567	return MAX3421_HXFR_BULK_OUT(epnum);
 568}
 569
 570/*
 571 * Issue the next host-transfer command.
 572 * Caller must NOT hold HCD spinlock.
 573 */
 574static void
 575max3421_next_transfer(struct usb_hcd *hcd, int fast_retransmit)
 576{
 577	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 578	struct urb *urb = max3421_hcd->curr_urb;
 579	struct max3421_ep *max3421_ep;
 580	int cmd = -EINVAL;
 581
 582	if (!urb)
 583		return;	/* nothing to do */
 584
 585	max3421_ep = urb->ep->hcpriv;
 586
 587	switch (max3421_ep->pkt_state) {
 588	case PKT_STATE_SETUP:
 589		cmd = max3421_ctrl_setup(hcd, urb);
 590		break;
 591
 592	case PKT_STATE_TRANSFER:
 593		if (usb_urb_dir_in(urb))
 594			cmd = max3421_transfer_in(hcd, urb);
 595		else
 596			cmd = max3421_transfer_out(hcd, urb, fast_retransmit);
 597		break;
 598
 599	case PKT_STATE_TERMINATE:
 600		/*
 601		 * IN transfers are terminated with HS_OUT token,
 602		 * OUT transfers with HS_IN:
 603		 */
 604		if (usb_urb_dir_in(urb))
 605			cmd = MAX3421_HXFR_HS_OUT;
 606		else
 607			cmd = MAX3421_HXFR_HS_IN;
 608		break;
 609	}
 610
 611	if (cmd < 0)
 612		return;
 613
 614	/* issue the command and wait for host-xfer-done interrupt: */
 615
 616	spi_wr8(hcd, MAX3421_REG_HXFR, cmd);
 617	max3421_hcd->hien |= BIT(MAX3421_HI_HXFRDN_BIT);
 618}
 619
 620/*
 621 * Find the next URB to process and start its execution.
 622 *
 623 * At this time, we do not anticipate ever connecting a USB hub to the
 624 * MAX3421 chip, so at most USB device can be connected and we can use
 625 * a simplistic scheduler: at the start of a frame, schedule all
 626 * periodic transfers.  Once that is done, use the remainder of the
 627 * frame to process non-periodic (bulk & control) transfers.
 628 *
 629 * Preconditions:
 630 * o Caller must NOT hold HCD spinlock.
 631 * o max3421_hcd->curr_urb MUST BE NULL.
 632 * o MAX3421E chip must be idle.
 633 */
 634static int
 635max3421_select_and_start_urb(struct usb_hcd *hcd)
 636{
 637	struct spi_device *spi = to_spi_device(hcd->self.controller);
 638	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 639	struct urb *urb, *curr_urb = NULL;
 640	struct max3421_ep *max3421_ep;
 641	int epnum;
 642	struct usb_host_endpoint *ep;
 643	struct list_head *pos;
 644	unsigned long flags;
 645
 646	spin_lock_irqsave(&max3421_hcd->lock, flags);
 647
 648	for (;
 649	     max3421_hcd->sched_pass < SCHED_PASS_DONE;
 650	     ++max3421_hcd->sched_pass)
 651		list_for_each(pos, &max3421_hcd->ep_list) {
 652			urb = NULL;
 653			max3421_ep = container_of(pos, struct max3421_ep,
 654						  ep_list);
 655			ep = max3421_ep->ep;
 656
 657			switch (usb_endpoint_type(&ep->desc)) {
 658			case USB_ENDPOINT_XFER_ISOC:
 659			case USB_ENDPOINT_XFER_INT:
 660				if (max3421_hcd->sched_pass !=
 661				    SCHED_PASS_PERIODIC)
 662					continue;
 663				break;
 664
 665			case USB_ENDPOINT_XFER_CONTROL:
 666			case USB_ENDPOINT_XFER_BULK:
 667				if (max3421_hcd->sched_pass !=
 668				    SCHED_PASS_NON_PERIODIC)
 669					continue;
 670				break;
 671			}
 672
 673			if (list_empty(&ep->urb_list))
 674				continue;	/* nothing to do */
 675			urb = list_first_entry(&ep->urb_list, struct urb,
 676					       urb_list);
 677			if (urb->unlinked) {
 678				dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
 679					__func__, urb, urb->unlinked);
 680				max3421_hcd->curr_urb = urb;
 681				max3421_hcd->urb_done = 1;
 682				spin_unlock_irqrestore(&max3421_hcd->lock,
 683						       flags);
 684				return 1;
 685			}
 686
 687			switch (usb_endpoint_type(&ep->desc)) {
 688			case USB_ENDPOINT_XFER_CONTROL:
 689				/*
 690				 * Allow one control transaction per
 691				 * frame per endpoint:
 692				 */
 693				if (frame_diff(max3421_ep->last_active,
 694					       max3421_hcd->frame_number) == 0)
 695					continue;
 696				break;
 697
 698			case USB_ENDPOINT_XFER_BULK:
 699				if (max3421_ep->retransmit
 700				    && (frame_diff(max3421_ep->last_active,
 701						   max3421_hcd->frame_number)
 702					== 0))
 703					/*
 704					 * We already tried this EP
 705					 * during this frame and got a
 706					 * NAK or error; wait for next frame
 707					 */
 708					continue;
 709				break;
 710
 711			case USB_ENDPOINT_XFER_ISOC:
 712			case USB_ENDPOINT_XFER_INT:
 713				if (frame_diff(max3421_hcd->frame_number,
 714					       max3421_ep->last_active)
 715				    < urb->interval)
 716					/*
 717					 * We already processed this
 718					 * end-point in the current
 719					 * frame
 720					 */
 721					continue;
 722				break;
 723			}
 724
 725			/* move current ep to tail: */
 726			list_move_tail(pos, &max3421_hcd->ep_list);
 727			curr_urb = urb;
 728			goto done;
 729		}
 730done:
 731	if (!curr_urb) {
 732		spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 733		return 0;
 734	}
 735
 736	urb = max3421_hcd->curr_urb = curr_urb;
 737	epnum = usb_endpoint_num(&urb->ep->desc);
 738	if (max3421_ep->retransmit)
 739		/* restart (part of) a USB transaction: */
 740		max3421_ep->retransmit = 0;
 741	else {
 742		/* start USB transaction: */
 743		if (usb_endpoint_xfer_control(&ep->desc)) {
 744			/*
 745			 * See USB 2.0 spec section 8.6.1
 746			 * Initialization via SETUP Token:
 747			 */
 748			usb_settoggle(urb->dev, epnum, 0, 1);
 749			usb_settoggle(urb->dev, epnum, 1, 1);
 750			max3421_ep->pkt_state = PKT_STATE_SETUP;
 751		} else
 752			max3421_ep->pkt_state = PKT_STATE_TRANSFER;
 753	}
 754
 755	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 756
 757	max3421_ep->last_active = max3421_hcd->frame_number;
 758	max3421_set_address(hcd, urb->dev, epnum);
 759	max3421_set_speed(hcd, urb->dev);
 760	max3421_next_transfer(hcd, 0);
 761	return 1;
 762}
 763
 764/*
 765 * Check all endpoints for URBs that got unlinked.
 766 *
 767 * Caller must NOT hold HCD spinlock.
 768 */
 769static int
 770max3421_check_unlink(struct usb_hcd *hcd)
 771{
 772	struct spi_device *spi = to_spi_device(hcd->self.controller);
 773	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 774	struct max3421_ep *max3421_ep;
 775	struct usb_host_endpoint *ep;
 776	struct urb *urb, *next;
 777	unsigned long flags;
 778	int retval = 0;
 779
 780	spin_lock_irqsave(&max3421_hcd->lock, flags);
 781	list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) {
 782		ep = max3421_ep->ep;
 783		list_for_each_entry_safe(urb, next, &ep->urb_list, urb_list) {
 784			if (urb->unlinked) {
 785				retval = 1;
 786				dev_dbg(&spi->dev, "%s: URB %p unlinked=%d",
 787					__func__, urb, urb->unlinked);
 788				usb_hcd_unlink_urb_from_ep(hcd, urb);
 789				spin_unlock_irqrestore(&max3421_hcd->lock,
 790						       flags);
 791				usb_hcd_giveback_urb(hcd, urb, 0);
 792				spin_lock_irqsave(&max3421_hcd->lock, flags);
 793			}
 794		}
 795	}
 796	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
 797	return retval;
 798}
 799
 800/*
 801 * Caller must NOT hold HCD spinlock.
 802 */
 803static void
 804max3421_slow_retransmit(struct usb_hcd *hcd)
 805{
 806	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 807	struct urb *urb = max3421_hcd->curr_urb;
 808	struct max3421_ep *max3421_ep;
 809
 810	max3421_ep = urb->ep->hcpriv;
 811	max3421_ep->retransmit = 1;
 812	max3421_hcd->curr_urb = NULL;
 813}
 814
 815/*
 816 * Caller must NOT hold HCD spinlock.
 817 */
 818static void
 819max3421_recv_data_available(struct usb_hcd *hcd)
 820{
 821	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 822	struct urb *urb = max3421_hcd->curr_urb;
 823	size_t remaining, transfer_size;
 824	u8 rcvbc;
 825
 826	rcvbc = spi_rd8(hcd, MAX3421_REG_RCVBC);
 827
 828	if (rcvbc > MAX3421_FIFO_SIZE)
 829		rcvbc = MAX3421_FIFO_SIZE;
 830	if (urb->actual_length >= urb->transfer_buffer_length)
 831		remaining = 0;
 832	else
 833		remaining = urb->transfer_buffer_length - urb->actual_length;
 834	transfer_size = rcvbc;
 835	if (transfer_size > remaining)
 836		transfer_size = remaining;
 837	if (transfer_size > 0) {
 838		void *dst = urb->transfer_buffer + urb->actual_length;
 839
 840		spi_rd_buf(hcd, MAX3421_REG_RCVFIFO, dst, transfer_size);
 841		urb->actual_length += transfer_size;
 842		max3421_hcd->curr_len = transfer_size;
 843	}
 844
 845	/* ack the RCVDAV irq now that the FIFO has been read: */
 846	spi_wr8(hcd, MAX3421_REG_HIRQ, BIT(MAX3421_HI_RCVDAV_BIT));
 847}
 848
 849static void
 850max3421_handle_error(struct usb_hcd *hcd, u8 hrsl)
 851{
 852	struct spi_device *spi = to_spi_device(hcd->self.controller);
 853	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 854	u8 result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
 855	struct urb *urb = max3421_hcd->curr_urb;
 856	struct max3421_ep *max3421_ep = urb->ep->hcpriv;
 857	int switch_sndfifo;
 858
 859	/*
 860	 * If an OUT command results in any response other than OK
 861	 * (i.e., error or NAK), we have to perform a dummy-write to
 862	 * SNDBC so the FIFO gets switched back to us.  Otherwise, we
 863	 * get out of sync with the SNDFIFO double buffer.
 864	 */
 865	switch_sndfifo = (max3421_ep->pkt_state == PKT_STATE_TRANSFER &&
 866			  usb_urb_dir_out(urb));
 867
 868	switch (result_code) {
 869	case MAX3421_HRSL_OK:
 870		return;			/* this shouldn't happen */
 871
 872	case MAX3421_HRSL_WRONGPID:	/* received wrong PID */
 873	case MAX3421_HRSL_BUSY:		/* SIE busy */
 874	case MAX3421_HRSL_BADREQ:	/* bad val in HXFR */
 875	case MAX3421_HRSL_UNDEF:	/* reserved */
 876	case MAX3421_HRSL_KERR:		/* K-state instead of response */
 877	case MAX3421_HRSL_JERR:		/* J-state instead of response */
 878		/*
 879		 * packet experienced an error that we cannot recover
 880		 * from; report error
 881		 */
 882		max3421_hcd->urb_done = hrsl_to_error[result_code];
 883		dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
 884			__func__, hrsl);
 885		break;
 886
 887	case MAX3421_HRSL_TOGERR:
 888		if (usb_urb_dir_in(urb))
 889			; /* don't do anything (device will switch toggle) */
 890		else {
 891			/* flip the send toggle bit: */
 892			int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
 893
 894			sndtog ^= 1;
 895			spi_wr8(hcd, MAX3421_REG_HCTL,
 896				BIT(sndtog + MAX3421_HCTL_SNDTOG0_BIT));
 897		}
 898		fallthrough;
 899	case MAX3421_HRSL_BADBC:	/* bad byte count */
 900	case MAX3421_HRSL_PIDERR:	/* received PID is corrupted */
 901	case MAX3421_HRSL_PKTERR:	/* packet error (stuff, EOP) */
 902	case MAX3421_HRSL_CRCERR:	/* CRC error */
 903	case MAX3421_HRSL_BABBLE:	/* device talked too long */
 904	case MAX3421_HRSL_TIMEOUT:
 905		if (max3421_ep->retries++ < USB_MAX_RETRIES)
 906			/* retry the packet again in the next frame */
 907			max3421_slow_retransmit(hcd);
 908		else {
 909			/* Based on ohci.h cc_to_err[]: */
 910			max3421_hcd->urb_done = hrsl_to_error[result_code];
 911			dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
 912				__func__, hrsl);
 913		}
 914		break;
 915
 916	case MAX3421_HRSL_STALL:
 917		dev_dbg(&spi->dev, "%s: unexpected error HRSL=0x%02x",
 918			__func__, hrsl);
 919		max3421_hcd->urb_done = hrsl_to_error[result_code];
 920		break;
 921
 922	case MAX3421_HRSL_NAK:
 923		/*
 924		 * Device wasn't ready for data or has no data
 925		 * available: retry the packet again.
 926		 */
 927		if (max3421_ep->naks++ < NAK_MAX_FAST_RETRANSMITS) {
 928			max3421_next_transfer(hcd, 1);
 929			switch_sndfifo = 0;
 930		} else
 931			max3421_slow_retransmit(hcd);
 932		break;
 933	}
 934	if (switch_sndfifo)
 935		spi_wr8(hcd, MAX3421_REG_SNDBC, 0);
 936}
 937
 938/*
 939 * Caller must NOT hold HCD spinlock.
 940 */
 941static int
 942max3421_transfer_in_done(struct usb_hcd *hcd, struct urb *urb)
 943{
 944	struct spi_device *spi = to_spi_device(hcd->self.controller);
 945	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 946	u32 max_packet;
 947
 948	if (urb->actual_length >= urb->transfer_buffer_length)
 949		return 1;	/* read is complete, so we're done */
 950
 951	/*
 952	 * USB 2.0 Section 5.3.2 Pipes: packets must be full size
 953	 * except for last one.
 954	 */
 955	max_packet = usb_maxpacket(urb->dev, urb->pipe);
 956	if (max_packet > MAX3421_FIFO_SIZE) {
 957		/*
 958		 * We do not support isochronous transfers at this
 959		 * time...
 960		 */
 961		dev_err(&spi->dev,
 962			"%s: packet-size of %u too big (limit is %u bytes)",
 963			__func__, max_packet, MAX3421_FIFO_SIZE);
 964		return -EINVAL;
 965	}
 966
 967	if (max3421_hcd->curr_len < max_packet) {
 968		if (urb->transfer_flags & URB_SHORT_NOT_OK) {
 969			/*
 970			 * remaining > 0 and received an
 971			 * unexpected partial packet ->
 972			 * error
 973			 */
 974			return -EREMOTEIO;
 975		} else
 976			/* short read, but it's OK */
 977			return 1;
 978	}
 979	return 0;	/* not done */
 980}
 981
 982/*
 983 * Caller must NOT hold HCD spinlock.
 984 */
 985static int
 986max3421_transfer_out_done(struct usb_hcd *hcd, struct urb *urb)
 987{
 988	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
 989
 990	urb->actual_length += max3421_hcd->curr_len;
 991	if (urb->actual_length < urb->transfer_buffer_length)
 992		return 0;
 993	if (urb->transfer_flags & URB_ZERO_PACKET) {
 994		/*
 995		 * Some hardware needs a zero-size packet at the end
 996		 * of a bulk-out transfer if the last transfer was a
 997		 * full-sized packet (i.e., such hardware use <
 998		 * max_packet as an indicator that the end of the
 999		 * packet has been reached).
1000		 */
1001		u32 max_packet = usb_maxpacket(urb->dev, urb->pipe);
1002
1003		if (max3421_hcd->curr_len == max_packet)
1004			return 0;
1005	}
1006	return 1;
1007}
1008
1009/*
1010 * Caller must NOT hold HCD spinlock.
1011 */
1012static void
1013max3421_host_transfer_done(struct usb_hcd *hcd)
1014{
1015	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1016	struct urb *urb = max3421_hcd->curr_urb;
1017	struct max3421_ep *max3421_ep;
1018	u8 result_code, hrsl;
1019	int urb_done = 0;
1020
1021	max3421_hcd->hien &= ~(BIT(MAX3421_HI_HXFRDN_BIT) |
1022			       BIT(MAX3421_HI_RCVDAV_BIT));
1023
1024	hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1025	result_code = hrsl & MAX3421_HRSL_RESULT_MASK;
1026
1027#ifdef DEBUG
1028	++max3421_hcd->err_stat[result_code];
1029#endif
1030
1031	max3421_ep = urb->ep->hcpriv;
1032
1033	if (unlikely(result_code != MAX3421_HRSL_OK)) {
1034		max3421_handle_error(hcd, hrsl);
1035		return;
1036	}
1037
1038	max3421_ep->naks = 0;
1039	max3421_ep->retries = 0;
1040	switch (max3421_ep->pkt_state) {
1041
1042	case PKT_STATE_SETUP:
1043		if (urb->transfer_buffer_length > 0)
1044			max3421_ep->pkt_state = PKT_STATE_TRANSFER;
1045		else
1046			max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1047		break;
1048
1049	case PKT_STATE_TRANSFER:
1050		if (usb_urb_dir_in(urb))
1051			urb_done = max3421_transfer_in_done(hcd, urb);
1052		else
1053			urb_done = max3421_transfer_out_done(hcd, urb);
1054		if (urb_done > 0 && usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1055			/*
1056			 * We aren't really done - we still need to
1057			 * terminate the control transfer:
1058			 */
1059			max3421_hcd->urb_done = urb_done = 0;
1060			max3421_ep->pkt_state = PKT_STATE_TERMINATE;
1061		}
1062		break;
1063
1064	case PKT_STATE_TERMINATE:
1065		urb_done = 1;
1066		break;
1067	}
1068
1069	if (urb_done)
1070		max3421_hcd->urb_done = urb_done;
1071	else
1072		max3421_next_transfer(hcd, 0);
1073}
1074
1075/*
1076 * Caller must NOT hold HCD spinlock.
1077 */
1078static void
1079max3421_detect_conn(struct usb_hcd *hcd)
1080{
1081	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1082	unsigned int jk, have_conn = 0;
1083	u32 old_port_status, chg;
1084	unsigned long flags;
1085	u8 hrsl, mode;
1086
1087	hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1088
1089	jk = ((((hrsl >> MAX3421_HRSL_JSTATUS_BIT) & 1) << 0) |
1090	      (((hrsl >> MAX3421_HRSL_KSTATUS_BIT) & 1) << 1));
1091
1092	mode = max3421_hcd->mode;
1093
1094	switch (jk) {
1095	case 0x0: /* SE0: disconnect */
1096		/*
1097		 * Turn off SOFKAENAB bit to avoid getting interrupt
1098		 * every milli-second:
1099		 */
1100		mode &= ~BIT(MAX3421_MODE_SOFKAENAB_BIT);
1101		break;
1102
1103	case 0x1: /* J=0,K=1: low-speed (in full-speed or vice versa) */
1104	case 0x2: /* J=1,K=0: full-speed (in full-speed or vice versa) */
1105		if (jk == 0x2)
1106			/* need to switch to the other speed: */
1107			mode ^= BIT(MAX3421_MODE_LOWSPEED_BIT);
1108		/* turn on SOFKAENAB bit: */
1109		mode |= BIT(MAX3421_MODE_SOFKAENAB_BIT);
1110		have_conn = 1;
1111		break;
1112
1113	case 0x3: /* illegal */
1114		break;
1115	}
1116
1117	max3421_hcd->mode = mode;
1118	spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1119
1120	spin_lock_irqsave(&max3421_hcd->lock, flags);
1121	old_port_status = max3421_hcd->port_status;
1122	if (have_conn)
1123		max3421_hcd->port_status |=  USB_PORT_STAT_CONNECTION;
1124	else
1125		max3421_hcd->port_status &= ~USB_PORT_STAT_CONNECTION;
1126	if (mode & BIT(MAX3421_MODE_LOWSPEED_BIT))
1127		max3421_hcd->port_status |=  USB_PORT_STAT_LOW_SPEED;
1128	else
1129		max3421_hcd->port_status &= ~USB_PORT_STAT_LOW_SPEED;
1130	chg = (old_port_status ^ max3421_hcd->port_status);
1131	max3421_hcd->port_status |= chg << 16;
1132	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1133}
1134
1135static irqreturn_t
1136max3421_irq_handler(int irq, void *dev_id)
1137{
1138	struct usb_hcd *hcd = dev_id;
1139	struct spi_device *spi = to_spi_device(hcd->self.controller);
1140	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1141
1142	if (max3421_hcd->spi_thread)
1143		wake_up_process(max3421_hcd->spi_thread);
1144	if (!test_and_set_bit(ENABLE_IRQ, &max3421_hcd->todo))
1145		disable_irq_nosync(spi->irq);
1146	return IRQ_HANDLED;
1147}
1148
1149#ifdef DEBUG
1150
1151static void
1152dump_eps(struct usb_hcd *hcd)
1153{
1154	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1155	struct max3421_ep *max3421_ep;
1156	struct usb_host_endpoint *ep;
1157	char ubuf[512], *dp, *end;
1158	unsigned long flags;
1159	struct urb *urb;
1160	int epnum, ret;
1161
1162	spin_lock_irqsave(&max3421_hcd->lock, flags);
1163	list_for_each_entry(max3421_ep, &max3421_hcd->ep_list, ep_list) {
1164		ep = max3421_ep->ep;
1165
1166		dp = ubuf;
1167		end = dp + sizeof(ubuf);
1168		*dp = '\0';
1169		list_for_each_entry(urb, &ep->urb_list, urb_list) {
1170			ret = snprintf(dp, end - dp, " %p(%d.%s %d/%d)", urb,
1171				       usb_pipetype(urb->pipe),
1172				       usb_urb_dir_in(urb) ? "IN" : "OUT",
1173				       urb->actual_length,
1174				       urb->transfer_buffer_length);
1175			if (ret < 0 || ret >= end - dp)
1176				break;	/* error or buffer full */
1177			dp += ret;
1178		}
1179
1180		epnum = usb_endpoint_num(&ep->desc);
1181		pr_info("EP%0u %u lst %04u rtr %u nak %6u rxmt %u: %s\n",
1182			epnum, max3421_ep->pkt_state, max3421_ep->last_active,
1183			max3421_ep->retries, max3421_ep->naks,
1184			max3421_ep->retransmit, ubuf);
1185	}
1186	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1187}
1188
1189#endif /* DEBUG */
1190
1191/* Return zero if no work was performed, 1 otherwise.  */
1192static int
1193max3421_handle_irqs(struct usb_hcd *hcd)
1194{
1195	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1196	u32 chg, old_port_status;
1197	unsigned long flags;
1198	u8 hirq;
1199
1200	/*
1201	 * Read and ack pending interrupts (CPU must never
1202	 * clear SNDBAV directly and RCVDAV must be cleared by
1203	 * max3421_recv_data_available()!):
1204	 */
1205	hirq = spi_rd8(hcd, MAX3421_REG_HIRQ);
1206	hirq &= max3421_hcd->hien;
1207	if (!hirq)
1208		return 0;
1209
1210	spi_wr8(hcd, MAX3421_REG_HIRQ,
1211		hirq & ~(BIT(MAX3421_HI_SNDBAV_BIT) |
1212			 BIT(MAX3421_HI_RCVDAV_BIT)));
1213
1214	if (hirq & BIT(MAX3421_HI_FRAME_BIT)) {
1215		max3421_hcd->frame_number = ((max3421_hcd->frame_number + 1)
1216					     & USB_MAX_FRAME_NUMBER);
1217		max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1218	}
1219
1220	if (hirq & BIT(MAX3421_HI_RCVDAV_BIT))
1221		max3421_recv_data_available(hcd);
1222
1223	if (hirq & BIT(MAX3421_HI_HXFRDN_BIT))
1224		max3421_host_transfer_done(hcd);
1225
1226	if (hirq & BIT(MAX3421_HI_CONDET_BIT))
1227		max3421_detect_conn(hcd);
1228
1229	/*
1230	 * Now process interrupts that may affect HCD state
1231	 * other than the end-points:
1232	 */
1233	spin_lock_irqsave(&max3421_hcd->lock, flags);
1234
1235	old_port_status = max3421_hcd->port_status;
1236	if (hirq & BIT(MAX3421_HI_BUSEVENT_BIT)) {
1237		if (max3421_hcd->port_status & USB_PORT_STAT_RESET) {
1238			/* BUSEVENT due to completion of Bus Reset */
1239			max3421_hcd->port_status &= ~USB_PORT_STAT_RESET;
1240			max3421_hcd->port_status |=  USB_PORT_STAT_ENABLE;
1241		} else {
1242			/* BUSEVENT due to completion of Bus Resume */
1243			pr_info("%s: BUSEVENT Bus Resume Done\n", __func__);
1244		}
1245	}
1246	if (hirq & BIT(MAX3421_HI_RWU_BIT))
1247		pr_info("%s: RWU\n", __func__);
1248	if (hirq & BIT(MAX3421_HI_SUSDN_BIT))
1249		pr_info("%s: SUSDN\n", __func__);
1250
1251	chg = (old_port_status ^ max3421_hcd->port_status);
1252	max3421_hcd->port_status |= chg << 16;
1253
1254	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1255
1256#ifdef DEBUG
1257	{
1258		static unsigned long last_time;
1259		char sbuf[16 * 16], *dp, *end;
1260		int i;
1261
1262		if (time_after(jiffies, last_time + 5*HZ)) {
1263			dp = sbuf;
1264			end = sbuf + sizeof(sbuf);
1265			*dp = '\0';
1266			for (i = 0; i < 16; ++i) {
1267				int ret = snprintf(dp, end - dp, " %lu",
1268						   max3421_hcd->err_stat[i]);
1269				if (ret < 0 || ret >= end - dp)
1270					break;	/* error or buffer full */
1271				dp += ret;
1272			}
1273			pr_info("%s: hrsl_stats %s\n", __func__, sbuf);
1274			memset(max3421_hcd->err_stat, 0,
1275			       sizeof(max3421_hcd->err_stat));
1276			last_time = jiffies;
1277
1278			dump_eps(hcd);
1279		}
1280	}
1281#endif
1282	return 1;
1283}
1284
1285static int
1286max3421_reset_hcd(struct usb_hcd *hcd)
1287{
1288	struct spi_device *spi = to_spi_device(hcd->self.controller);
1289	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1290	int timeout;
1291
1292	/* perform a chip reset and wait for OSCIRQ signal to appear: */
1293	spi_wr8(hcd, MAX3421_REG_USBCTL, BIT(MAX3421_USBCTL_CHIPRES_BIT));
1294	/* clear reset: */
1295	spi_wr8(hcd, MAX3421_REG_USBCTL, 0);
1296	timeout = 1000;
1297	while (1) {
1298		if (spi_rd8(hcd, MAX3421_REG_USBIRQ)
1299		    & BIT(MAX3421_USBIRQ_OSCOKIRQ_BIT))
1300			break;
1301		if (--timeout < 0) {
1302			dev_err(&spi->dev,
1303				"timed out waiting for oscillator OK signal");
1304			return 1;
1305		}
1306		cond_resched();
1307	}
1308
1309	/*
1310	 * Turn on host mode, automatic generation of SOF packets, and
1311	 * enable pull-down registers on DM/DP:
1312	 */
1313	max3421_hcd->mode = (BIT(MAX3421_MODE_HOST_BIT) |
1314			     BIT(MAX3421_MODE_SOFKAENAB_BIT) |
1315			     BIT(MAX3421_MODE_DMPULLDN_BIT) |
1316			     BIT(MAX3421_MODE_DPPULLDN_BIT));
1317	spi_wr8(hcd, MAX3421_REG_MODE, max3421_hcd->mode);
1318
1319	/* reset frame-number: */
1320	max3421_hcd->frame_number = USB_MAX_FRAME_NUMBER;
1321	spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_FRMRST_BIT));
1322
1323	/* sample the state of the D+ and D- lines */
1324	spi_wr8(hcd, MAX3421_REG_HCTL, BIT(MAX3421_HCTL_SAMPLEBUS_BIT));
1325	max3421_detect_conn(hcd);
1326
1327	/* enable frame, connection-detected, and bus-event interrupts: */
1328	max3421_hcd->hien = (BIT(MAX3421_HI_FRAME_BIT) |
1329			     BIT(MAX3421_HI_CONDET_BIT) |
1330			     BIT(MAX3421_HI_BUSEVENT_BIT));
1331	spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1332
1333	/* enable interrupts: */
1334	spi_wr8(hcd, MAX3421_REG_CPUCTL, BIT(MAX3421_CPUCTL_IE_BIT));
1335	return 1;
1336}
1337
1338static int
1339max3421_urb_done(struct usb_hcd *hcd)
1340{
1341	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1342	unsigned long flags;
1343	struct urb *urb;
1344	int status;
1345
1346	status = max3421_hcd->urb_done;
1347	max3421_hcd->urb_done = 0;
1348	if (status > 0)
1349		status = 0;
1350	urb = max3421_hcd->curr_urb;
1351	if (urb) {
1352		/* save the old end-points toggles: */
1353		u8 hrsl = spi_rd8(hcd, MAX3421_REG_HRSL);
1354		int rcvtog = (hrsl >> MAX3421_HRSL_RCVTOGRD_BIT) & 1;
1355		int sndtog = (hrsl >> MAX3421_HRSL_SNDTOGRD_BIT) & 1;
1356		int epnum = usb_endpoint_num(&urb->ep->desc);
1357
1358		/* no locking: HCD (i.e., we) own toggles, don't we? */
1359		usb_settoggle(urb->dev, epnum, 0, rcvtog);
1360		usb_settoggle(urb->dev, epnum, 1, sndtog);
1361
1362		max3421_hcd->curr_urb = NULL;
1363		spin_lock_irqsave(&max3421_hcd->lock, flags);
1364		usb_hcd_unlink_urb_from_ep(hcd, urb);
1365		spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1366
1367		/* must be called without the HCD spinlock: */
1368		usb_hcd_giveback_urb(hcd, urb, status);
1369	}
1370	return 1;
1371}
1372
1373static int
1374max3421_spi_thread(void *dev_id)
1375{
1376	struct usb_hcd *hcd = dev_id;
1377	struct spi_device *spi = to_spi_device(hcd->self.controller);
1378	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1379	int i, i_worked = 1;
1380
1381	/* set full-duplex SPI mode, low-active interrupt pin: */
1382	spi_wr8(hcd, MAX3421_REG_PINCTL,
1383		(BIT(MAX3421_PINCTL_FDUPSPI_BIT) |	/* full-duplex */
1384		 BIT(MAX3421_PINCTL_INTLEVEL_BIT)));	/* low-active irq */
1385
1386	while (!kthread_should_stop()) {
1387		max3421_hcd->rev = spi_rd8(hcd, MAX3421_REG_REVISION);
1388		if (max3421_hcd->rev == 0x12 || max3421_hcd->rev == 0x13)
1389			break;
1390		dev_err(&spi->dev, "bad rev 0x%02x", max3421_hcd->rev);
1391		msleep(10000);
1392	}
1393	dev_info(&spi->dev, "rev 0x%x, SPI clk %dHz, bpw %u, irq %d\n",
1394		 max3421_hcd->rev, spi->max_speed_hz, spi->bits_per_word,
1395		 spi->irq);
1396
1397	while (!kthread_should_stop()) {
1398		if (!i_worked) {
1399			/*
1400			 * We'll be waiting for wakeups from the hard
1401			 * interrupt handler, so now is a good time to
1402			 * sync our hien with the chip:
1403			 */
1404			spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
1405
1406			set_current_state(TASK_INTERRUPTIBLE);
1407			if (test_and_clear_bit(ENABLE_IRQ, &max3421_hcd->todo))
1408				enable_irq(spi->irq);
1409			schedule();
1410			__set_current_state(TASK_RUNNING);
1411		}
1412
1413		i_worked = 0;
1414
1415		if (max3421_hcd->urb_done)
1416			i_worked |= max3421_urb_done(hcd);
1417		else if (max3421_handle_irqs(hcd))
1418			i_worked = 1;
1419		else if (!max3421_hcd->curr_urb)
1420			i_worked |= max3421_select_and_start_urb(hcd);
1421
1422		if (test_and_clear_bit(RESET_HCD, &max3421_hcd->todo))
1423			/* reset the HCD: */
1424			i_worked |= max3421_reset_hcd(hcd);
1425		if (test_and_clear_bit(RESET_PORT, &max3421_hcd->todo)) {
1426			/* perform a USB bus reset: */
1427			spi_wr8(hcd, MAX3421_REG_HCTL,
1428				BIT(MAX3421_HCTL_BUSRST_BIT));
1429			i_worked = 1;
1430		}
1431		if (test_and_clear_bit(CHECK_UNLINK, &max3421_hcd->todo))
1432			i_worked |= max3421_check_unlink(hcd);
1433		if (test_and_clear_bit(IOPIN_UPDATE, &max3421_hcd->todo)) {
1434			/*
1435			 * IOPINS1/IOPINS2 do not auto-increment, so we can't
1436			 * use spi_wr_buf().
1437			 */
1438			for (i = 0; i < ARRAY_SIZE(max3421_hcd->iopins); ++i) {
1439				u8 val = spi_rd8(hcd, MAX3421_REG_IOPINS1);
1440
1441				val = ((val & 0xf0) |
1442				       (max3421_hcd->iopins[i] & 0x0f));
1443				spi_wr8(hcd, MAX3421_REG_IOPINS1 + i, val);
1444				max3421_hcd->iopins[i] = val;
1445			}
1446			i_worked = 1;
1447		}
1448	}
1449	set_current_state(TASK_RUNNING);
1450	dev_info(&spi->dev, "SPI thread exiting");
1451	return 0;
1452}
1453
1454static int
1455max3421_reset_port(struct usb_hcd *hcd)
1456{
1457	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1458
1459	max3421_hcd->port_status &= ~(USB_PORT_STAT_ENABLE |
1460				      USB_PORT_STAT_LOW_SPEED);
1461	max3421_hcd->port_status |= USB_PORT_STAT_RESET;
1462	set_bit(RESET_PORT, &max3421_hcd->todo);
1463	wake_up_process(max3421_hcd->spi_thread);
1464	return 0;
1465}
1466
1467static int
1468max3421_reset(struct usb_hcd *hcd)
1469{
1470	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1471
1472	hcd->self.sg_tablesize = 0;
1473	hcd->speed = HCD_USB2;
1474	hcd->self.root_hub->speed = USB_SPEED_FULL;
1475	set_bit(RESET_HCD, &max3421_hcd->todo);
1476	wake_up_process(max3421_hcd->spi_thread);
1477	return 0;
1478}
1479
1480static int
1481max3421_start(struct usb_hcd *hcd)
1482{
1483	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1484
1485	spin_lock_init(&max3421_hcd->lock);
1486	max3421_hcd->rh_state = MAX3421_RH_RUNNING;
1487
1488	INIT_LIST_HEAD(&max3421_hcd->ep_list);
1489
1490	hcd->power_budget = POWER_BUDGET;
1491	hcd->state = HC_STATE_RUNNING;
1492	hcd->uses_new_polling = 1;
1493	return 0;
1494}
1495
1496static void
1497max3421_stop(struct usb_hcd *hcd)
1498{
1499}
1500
1501static int
1502max3421_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1503{
1504	struct spi_device *spi = to_spi_device(hcd->self.controller);
1505	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1506	struct max3421_ep *max3421_ep;
1507	unsigned long flags;
1508	int retval;
1509
1510	switch (usb_pipetype(urb->pipe)) {
1511	case PIPE_INTERRUPT:
1512	case PIPE_ISOCHRONOUS:
1513		if (urb->interval < 0) {
1514			dev_err(&spi->dev,
1515			  "%s: interval=%d for intr-/iso-pipe; expected > 0\n",
1516				__func__, urb->interval);
1517			return -EINVAL;
1518		}
1519		break;
1520	default:
1521		break;
1522	}
1523
1524	spin_lock_irqsave(&max3421_hcd->lock, flags);
1525
1526	max3421_ep = urb->ep->hcpriv;
1527	if (!max3421_ep) {
1528		/* gets freed in max3421_endpoint_disable: */
1529		max3421_ep = kzalloc(sizeof(struct max3421_ep), GFP_ATOMIC);
1530		if (!max3421_ep) {
1531			retval = -ENOMEM;
1532			goto out;
1533		}
1534		max3421_ep->ep = urb->ep;
1535		max3421_ep->last_active = max3421_hcd->frame_number;
1536		urb->ep->hcpriv = max3421_ep;
1537
1538		list_add_tail(&max3421_ep->ep_list, &max3421_hcd->ep_list);
1539	}
1540
1541	retval = usb_hcd_link_urb_to_ep(hcd, urb);
1542	if (retval == 0) {
1543		/* Since we added to the queue, restart scheduling: */
1544		max3421_hcd->sched_pass = SCHED_PASS_PERIODIC;
1545		wake_up_process(max3421_hcd->spi_thread);
1546	}
1547
1548out:
1549	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1550	return retval;
1551}
1552
1553static int
1554max3421_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1555{
1556	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1557	unsigned long flags;
1558	int retval;
1559
1560	spin_lock_irqsave(&max3421_hcd->lock, flags);
1561
1562	/*
1563	 * This will set urb->unlinked which in turn causes the entry
1564	 * to be dropped at the next opportunity.
1565	 */
1566	retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1567	if (retval == 0) {
1568		set_bit(CHECK_UNLINK, &max3421_hcd->todo);
1569		wake_up_process(max3421_hcd->spi_thread);
1570	}
1571	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1572	return retval;
1573}
1574
1575static void
1576max3421_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1577{
1578	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1579	unsigned long flags;
1580
1581	spin_lock_irqsave(&max3421_hcd->lock, flags);
1582
1583	if (ep->hcpriv) {
1584		struct max3421_ep *max3421_ep = ep->hcpriv;
1585
1586		/* remove myself from the ep_list: */
1587		if (!list_empty(&max3421_ep->ep_list))
1588			list_del(&max3421_ep->ep_list);
1589		kfree(max3421_ep);
1590		ep->hcpriv = NULL;
1591	}
1592
1593	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1594}
1595
1596static int
1597max3421_get_frame_number(struct usb_hcd *hcd)
1598{
1599	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1600	return max3421_hcd->frame_number;
1601}
1602
1603/*
1604 * Should return a non-zero value when any port is undergoing a resume
1605 * transition while the root hub is suspended.
1606 */
1607static int
1608max3421_hub_status_data(struct usb_hcd *hcd, char *buf)
1609{
1610	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1611	unsigned long flags;
1612	int retval = 0;
1613
1614	spin_lock_irqsave(&max3421_hcd->lock, flags);
1615	if (!HCD_HW_ACCESSIBLE(hcd))
1616		goto done;
1617
1618	*buf = 0;
1619	if ((max3421_hcd->port_status & PORT_C_MASK) != 0) {
1620		*buf = (1 << 1); /* a hub over-current condition exists */
1621		dev_dbg(hcd->self.controller,
1622			"port status 0x%08x has changes\n",
1623			max3421_hcd->port_status);
1624		retval = 1;
1625		if (max3421_hcd->rh_state == MAX3421_RH_SUSPENDED)
1626			usb_hcd_resume_root_hub(hcd);
1627	}
1628done:
1629	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1630	return retval;
1631}
1632
1633static inline void
1634hub_descriptor(struct usb_hub_descriptor *desc)
1635{
1636	memset(desc, 0, sizeof(*desc));
1637	/*
1638	 * See Table 11-13: Hub Descriptor in USB 2.0 spec.
1639	 */
1640	desc->bDescriptorType = USB_DT_HUB; /* hub descriptor */
1641	desc->bDescLength = 9;
1642	desc->wHubCharacteristics = cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM |
1643						HUB_CHAR_COMMON_OCPM);
1644	desc->bNbrPorts = 1;
1645}
1646
1647/*
1648 * Set the MAX3421E general-purpose output with number PIN_NUMBER to
1649 * VALUE (0 or 1).  PIN_NUMBER may be in the range from 1-8.  For
1650 * any other value, this function acts as a no-op.
1651 */
1652static void
1653max3421_gpout_set_value(struct usb_hcd *hcd, u8 pin_number, u8 value)
1654{
1655	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1656	u8 mask, idx;
1657
1658	--pin_number;
1659	if (pin_number >= MAX3421_GPOUT_COUNT)
1660		return;
1661
1662	mask = 1u << (pin_number % 4);
1663	idx = pin_number / 4;
1664
1665	if (value)
1666		max3421_hcd->iopins[idx] |=  mask;
1667	else
1668		max3421_hcd->iopins[idx] &= ~mask;
1669	set_bit(IOPIN_UPDATE, &max3421_hcd->todo);
1670	wake_up_process(max3421_hcd->spi_thread);
1671}
1672
1673static int
1674max3421_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
1675		    char *buf, u16 length)
1676{
1677	struct spi_device *spi = to_spi_device(hcd->self.controller);
1678	struct max3421_hcd *max3421_hcd = hcd_to_max3421(hcd);
1679	struct max3421_hcd_platform_data *pdata;
1680	unsigned long flags;
1681	int retval = 0;
1682
1683	pdata = spi->dev.platform_data;
1684
1685	spin_lock_irqsave(&max3421_hcd->lock, flags);
1686
1687	switch (type_req) {
1688	case ClearHubFeature:
1689		break;
1690	case ClearPortFeature:
1691		switch (value) {
1692		case USB_PORT_FEAT_SUSPEND:
1693			break;
1694		case USB_PORT_FEAT_POWER:
1695			dev_dbg(hcd->self.controller, "power-off\n");
1696			max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1697						!pdata->vbus_active_level);
1698			fallthrough;
1699		default:
1700			max3421_hcd->port_status &= ~(1 << value);
1701		}
1702		break;
1703	case GetHubDescriptor:
1704		hub_descriptor((struct usb_hub_descriptor *) buf);
1705		break;
1706
1707	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1708	case GetPortErrorCount:
1709	case SetHubDepth:
1710		/* USB3 only */
1711		goto error;
1712
1713	case GetHubStatus:
1714		*(__le32 *) buf = cpu_to_le32(0);
1715		break;
1716
1717	case GetPortStatus:
1718		if (index != 1) {
1719			retval = -EPIPE;
1720			goto error;
1721		}
1722		((__le16 *) buf)[0] = cpu_to_le16(max3421_hcd->port_status);
1723		((__le16 *) buf)[1] =
1724			cpu_to_le16(max3421_hcd->port_status >> 16);
1725		break;
1726
1727	case SetHubFeature:
1728		retval = -EPIPE;
1729		break;
1730
1731	case SetPortFeature:
1732		switch (value) {
1733		case USB_PORT_FEAT_LINK_STATE:
1734		case USB_PORT_FEAT_U1_TIMEOUT:
1735		case USB_PORT_FEAT_U2_TIMEOUT:
1736		case USB_PORT_FEAT_BH_PORT_RESET:
1737			goto error;
1738		case USB_PORT_FEAT_SUSPEND:
1739			if (max3421_hcd->active)
1740				max3421_hcd->port_status |=
1741					USB_PORT_STAT_SUSPEND;
1742			break;
1743		case USB_PORT_FEAT_POWER:
1744			dev_dbg(hcd->self.controller, "power-on\n");
1745			max3421_hcd->port_status |= USB_PORT_STAT_POWER;
1746			max3421_gpout_set_value(hcd, pdata->vbus_gpout,
1747						pdata->vbus_active_level);
1748			break;
1749		case USB_PORT_FEAT_RESET:
1750			max3421_reset_port(hcd);
1751			fallthrough;
1752		default:
1753			if ((max3421_hcd->port_status & USB_PORT_STAT_POWER)
1754			    != 0)
1755				max3421_hcd->port_status |= (1 << value);
1756		}
1757		break;
1758
1759	default:
1760		dev_dbg(hcd->self.controller,
1761			"hub control req%04x v%04x i%04x l%d\n",
1762			type_req, value, index, length);
1763error:		/* "protocol stall" on error */
1764		retval = -EPIPE;
1765	}
1766
1767	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1768	return retval;
1769}
1770
1771static int
1772max3421_bus_suspend(struct usb_hcd *hcd)
1773{
1774	return -1;
1775}
1776
1777static int
1778max3421_bus_resume(struct usb_hcd *hcd)
1779{
1780	return -1;
1781}
1782
1783static const struct hc_driver max3421_hcd_desc = {
1784	.description =		"max3421",
1785	.product_desc =		DRIVER_DESC,
1786	.hcd_priv_size =	sizeof(struct max3421_hcd),
1787	.flags =		HCD_USB11,
1788	.reset =		max3421_reset,
1789	.start =		max3421_start,
1790	.stop =			max3421_stop,
1791	.get_frame_number =	max3421_get_frame_number,
1792	.urb_enqueue =		max3421_urb_enqueue,
1793	.urb_dequeue =		max3421_urb_dequeue,
1794	.endpoint_disable =	max3421_endpoint_disable,
1795	.hub_status_data =	max3421_hub_status_data,
1796	.hub_control =		max3421_hub_control,
1797	.bus_suspend =		max3421_bus_suspend,
1798	.bus_resume =		max3421_bus_resume,
1799};
1800
1801static int
1802max3421_of_vbus_en_pin(struct device *dev, struct max3421_hcd_platform_data *pdata)
1803{
1804	int retval;
1805	uint32_t value[2];
1806
1807	if (!pdata)
1808		return -EINVAL;
1809
1810	retval = of_property_read_u32_array(dev->of_node, "maxim,vbus-en-pin", value, 2);
1811	if (retval) {
1812		dev_err(dev, "device tree node property 'maxim,vbus-en-pin' is missing\n");
1813		return retval;
1814	}
1815	dev_info(dev, "property 'maxim,vbus-en-pin' value is <%d %d>\n", value[0], value[1]);
1816
1817	pdata->vbus_gpout = value[0];
1818	pdata->vbus_active_level = value[1];
1819
1820	return 0;
1821}
1822
1823static int
1824max3421_probe(struct spi_device *spi)
1825{
1826	struct device *dev = &spi->dev;
1827	struct max3421_hcd *max3421_hcd;
1828	struct usb_hcd *hcd = NULL;
1829	struct max3421_hcd_platform_data *pdata = NULL;
1830	int retval;
1831
1832	if (spi_setup(spi) < 0) {
1833		dev_err(&spi->dev, "Unable to setup SPI bus");
1834		return -EFAULT;
1835	}
1836
1837	if (!spi->irq) {
1838		dev_err(dev, "Failed to get SPI IRQ");
1839		return -EFAULT;
1840	}
1841
1842	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
1843		pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1844		if (!pdata) {
1845			retval = -ENOMEM;
1846			goto error;
1847		}
1848		retval = max3421_of_vbus_en_pin(dev, pdata);
1849		if (retval)
1850			goto error;
1851
1852		spi->dev.platform_data = pdata;
1853	}
1854
1855	pdata = spi->dev.platform_data;
1856	if (!pdata) {
1857		dev_err(&spi->dev, "driver configuration data is not provided\n");
1858		retval = -EFAULT;
1859		goto error;
1860	}
1861	if (pdata->vbus_active_level > 1) {
1862		dev_err(&spi->dev, "vbus active level value %d is out of range (0/1)\n", pdata->vbus_active_level);
1863		retval = -EINVAL;
1864		goto error;
1865	}
1866	if (pdata->vbus_gpout < 1 || pdata->vbus_gpout > MAX3421_GPOUT_COUNT) {
1867		dev_err(&spi->dev, "vbus gpout value %d is out of range (1..8)\n", pdata->vbus_gpout);
1868		retval = -EINVAL;
1869		goto error;
1870	}
1871
1872	retval = -ENOMEM;
1873	hcd = usb_create_hcd(&max3421_hcd_desc, &spi->dev,
1874			     dev_name(&spi->dev));
1875	if (!hcd) {
1876		dev_err(&spi->dev, "failed to create HCD structure\n");
1877		goto error;
1878	}
1879	set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1880	max3421_hcd = hcd_to_max3421(hcd);
1881	INIT_LIST_HEAD(&max3421_hcd->ep_list);
1882	spi_set_drvdata(spi, max3421_hcd);
1883
1884	max3421_hcd->tx = kmalloc(sizeof(*max3421_hcd->tx), GFP_KERNEL);
1885	if (!max3421_hcd->tx)
1886		goto error;
1887	max3421_hcd->rx = kmalloc(sizeof(*max3421_hcd->rx), GFP_KERNEL);
1888	if (!max3421_hcd->rx)
1889		goto error;
1890
1891	max3421_hcd->spi_thread = kthread_run(max3421_spi_thread, hcd,
1892					      "max3421_spi_thread");
1893	if (max3421_hcd->spi_thread == ERR_PTR(-ENOMEM)) {
1894		dev_err(&spi->dev,
1895			"failed to create SPI thread (out of memory)\n");
1896		goto error;
1897	}
1898
1899	retval = usb_add_hcd(hcd, 0, 0);
1900	if (retval) {
1901		dev_err(&spi->dev, "failed to add HCD\n");
1902		goto error;
1903	}
1904
1905	retval = request_irq(spi->irq, max3421_irq_handler,
1906			     IRQF_TRIGGER_LOW, "max3421", hcd);
1907	if (retval < 0) {
1908		dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
1909		goto error;
1910	}
1911	return 0;
1912
1913error:
1914	if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) {
1915		devm_kfree(&spi->dev, pdata);
1916		spi->dev.platform_data = NULL;
1917	}
1918
1919	if (hcd) {
1920		kfree(max3421_hcd->tx);
1921		kfree(max3421_hcd->rx);
1922		if (max3421_hcd->spi_thread)
1923			kthread_stop(max3421_hcd->spi_thread);
1924		usb_put_hcd(hcd);
1925	}
1926	return retval;
1927}
1928
1929static void
1930max3421_remove(struct spi_device *spi)
1931{
1932	struct max3421_hcd *max3421_hcd;
1933	struct usb_hcd *hcd;
1934	unsigned long flags;
1935
1936	max3421_hcd = spi_get_drvdata(spi);
1937	hcd = max3421_to_hcd(max3421_hcd);
1938
1939	usb_remove_hcd(hcd);
1940
1941	spin_lock_irqsave(&max3421_hcd->lock, flags);
1942
1943	kthread_stop(max3421_hcd->spi_thread);
1944
1945	spin_unlock_irqrestore(&max3421_hcd->lock, flags);
1946
1947	free_irq(spi->irq, hcd);
1948
1949	usb_put_hcd(hcd);
1950}
1951
1952static const struct of_device_id max3421_of_match_table[] = {
1953	{ .compatible = "maxim,max3421", },
1954	{},
1955};
1956MODULE_DEVICE_TABLE(of, max3421_of_match_table);
1957
1958static struct spi_driver max3421_driver = {
1959	.probe		= max3421_probe,
1960	.remove		= max3421_remove,
1961	.driver		= {
1962		.name	= "max3421-hcd",
1963		.of_match_table = of_match_ptr(max3421_of_match_table),
1964	},
1965};
1966
1967module_spi_driver(max3421_driver);
1968
1969MODULE_DESCRIPTION(DRIVER_DESC);
1970MODULE_AUTHOR("David Mosberger <davidm@egauge.net>");
1971MODULE_LICENSE("GPL");