18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Intel Wireless WiMAX Connection 2400m
38c2ecf20Sopenharmony_ci * USB-specific i2400m driver definitions
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
108c2ecf20Sopenharmony_ci * are met:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *   * Redistributions of source code must retain the above copyright
138c2ecf20Sopenharmony_ci *     notice, this list of conditions and the following disclaimer.
148c2ecf20Sopenharmony_ci *   * Redistributions in binary form must reproduce the above copyright
158c2ecf20Sopenharmony_ci *     notice, this list of conditions and the following disclaimer in
168c2ecf20Sopenharmony_ci *     the documentation and/or other materials provided with the
178c2ecf20Sopenharmony_ci *     distribution.
188c2ecf20Sopenharmony_ci *   * Neither the name of Intel Corporation nor the names of its
198c2ecf20Sopenharmony_ci *     contributors may be used to endorse or promote products derived
208c2ecf20Sopenharmony_ci *     from this software without specific prior written permission.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
238c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
248c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
258c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
268c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
278c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
288c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
298c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
308c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
318c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
328c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Intel Corporation <linux-wimax@intel.com>
368c2ecf20Sopenharmony_ci * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
378c2ecf20Sopenharmony_ci * Yanir Lubetkin <yanirx.lubetkin@intel.com>
388c2ecf20Sopenharmony_ci *  - Initial implementation
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * This driver implements the bus-specific part of the i2400m for
428c2ecf20Sopenharmony_ci * USB. Check i2400m.h for a generic driver description.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * ARCHITECTURE
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * This driver listens to notifications sent from the notification
478c2ecf20Sopenharmony_ci * endpoint (in usb-notif.c); when data is ready to read, the code in
488c2ecf20Sopenharmony_ci * there schedules a read from the device (usb-rx.c) and then passes
498c2ecf20Sopenharmony_ci * the data to the generic RX code (rx.c).
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * When the generic driver needs to send data (network or control), it
528c2ecf20Sopenharmony_ci * queues up in the TX FIFO (tx.c) and that will notify the driver
538c2ecf20Sopenharmony_ci * through the i2400m->bus_tx_kick() callback
548c2ecf20Sopenharmony_ci * (usb-tx.c:i2400mu_bus_tx_kick) which will send the items in the
558c2ecf20Sopenharmony_ci * FIFO queue.
568c2ecf20Sopenharmony_ci *
578c2ecf20Sopenharmony_ci * This driver, as well, implements the USB-specific ops for the generic
588c2ecf20Sopenharmony_ci * driver to be able to setup/teardown communication with the device
598c2ecf20Sopenharmony_ci * [i2400m_bus_dev_start() and i2400m_bus_dev_stop()], reseting the
608c2ecf20Sopenharmony_ci * device [i2400m_bus_reset()] and performing firmware upload
618c2ecf20Sopenharmony_ci * [i2400m_bus_bm_cmd() and i2400_bus_bm_wait_for_ack()].
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#ifndef __I2400M_USB_H__
658c2ecf20Sopenharmony_ci#define __I2400M_USB_H__
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#include "i2400m.h"
688c2ecf20Sopenharmony_ci#include <linux/kthread.h>
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/*
728c2ecf20Sopenharmony_ci * Error Density Count: cheapo error density (over time) counter
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci * Originally by Reinette Chatre <reinette.chatre@intel.com>
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci * Embed an 'struct edc' somewhere. Each time there is a soft or
778c2ecf20Sopenharmony_ci * retryable error, call edc_inc() and check if the error top
788c2ecf20Sopenharmony_ci * watermark has been reached.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_cienum {
818c2ecf20Sopenharmony_ci	EDC_MAX_ERRORS = 10,
828c2ecf20Sopenharmony_ci	EDC_ERROR_TIMEFRAME = HZ,
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* error density counter */
868c2ecf20Sopenharmony_cistruct edc {
878c2ecf20Sopenharmony_ci	unsigned long timestart;
888c2ecf20Sopenharmony_ci	u16 errorcount;
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistruct i2400m_endpoint_cfg {
928c2ecf20Sopenharmony_ci	unsigned char bulk_out;
938c2ecf20Sopenharmony_ci	unsigned char notification;
948c2ecf20Sopenharmony_ci	unsigned char reset_cold;
958c2ecf20Sopenharmony_ci	unsigned char bulk_in;
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic inline void edc_init(struct edc *edc)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	edc->timestart = jiffies;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/**
1048c2ecf20Sopenharmony_ci * edc_inc - report a soft error and check if we are over the watermark
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci * @edc: pointer to error density counter.
1078c2ecf20Sopenharmony_ci * @max_err: maximum number of errors we can accept over the timeframe
1088c2ecf20Sopenharmony_ci * @timeframe: length of the timeframe (in jiffies).
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * Returns: !0 1 if maximum acceptable errors per timeframe has been
1118c2ecf20Sopenharmony_ci *     exceeded. 0 otherwise.
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci * This is way to determine if the number of acceptable errors per time
1148c2ecf20Sopenharmony_ci * period has been exceeded. It is not accurate as there are cases in which
1158c2ecf20Sopenharmony_ci * this scheme will not work, for example if there are periodic occurrences
1168c2ecf20Sopenharmony_ci * of errors that straddle updates to the start time. This scheme is
1178c2ecf20Sopenharmony_ci * sufficient for our usage.
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci * To use, embed a 'struct edc' somewhere, initialize it with
1208c2ecf20Sopenharmony_ci * edc_init() and when an error hits:
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * if (do_something_fails_with_a_soft_error) {
1238c2ecf20Sopenharmony_ci *        if (edc_inc(&my->edc, MAX_ERRORS, MAX_TIMEFRAME))
1248c2ecf20Sopenharmony_ci * 	           Ops, hard error, do something about it
1258c2ecf20Sopenharmony_ci *        else
1268c2ecf20Sopenharmony_ci *                 Retry or ignore, depending on whatever
1278c2ecf20Sopenharmony_ci * }
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_cistatic inline int edc_inc(struct edc *edc, u16 max_err, u16 timeframe)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	unsigned long now;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	now = jiffies;
1348c2ecf20Sopenharmony_ci	if (time_after(now, edc->timestart + timeframe)) {
1358c2ecf20Sopenharmony_ci		edc->errorcount = 1;
1368c2ecf20Sopenharmony_ci		edc->timestart = now;
1378c2ecf20Sopenharmony_ci	} else if (++edc->errorcount > max_err) {
1388c2ecf20Sopenharmony_ci		edc->errorcount = 0;
1398c2ecf20Sopenharmony_ci		edc->timestart = now;
1408c2ecf20Sopenharmony_ci		return 1;
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci	return 0;
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* Host-Device interface for USB */
1468c2ecf20Sopenharmony_cienum {
1478c2ecf20Sopenharmony_ci	I2400M_USB_BOOT_RETRIES = 3,
1488c2ecf20Sopenharmony_ci	I2400MU_MAX_NOTIFICATION_LEN = 256,
1498c2ecf20Sopenharmony_ci	I2400MU_BLK_SIZE = 16,
1508c2ecf20Sopenharmony_ci	I2400MU_PL_SIZE_MAX = 0x3EFF,
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/* Device IDs */
1538c2ecf20Sopenharmony_ci	USB_DEVICE_ID_I6050 = 0x0186,
1548c2ecf20Sopenharmony_ci	USB_DEVICE_ID_I6050_2 = 0x0188,
1558c2ecf20Sopenharmony_ci	USB_DEVICE_ID_I6150 = 0x07d6,
1568c2ecf20Sopenharmony_ci	USB_DEVICE_ID_I6150_2 = 0x07d7,
1578c2ecf20Sopenharmony_ci	USB_DEVICE_ID_I6150_3 = 0x07d9,
1588c2ecf20Sopenharmony_ci	USB_DEVICE_ID_I6250 = 0x0187,
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/**
1638c2ecf20Sopenharmony_ci * struct i2400mu - descriptor for a USB connected i2400m
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * @i2400m: bus-generic i2400m implementation; has to be first (see
1668c2ecf20Sopenharmony_ci *     it's documentation in i2400m.h).
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * @usb_dev: pointer to our USB device
1698c2ecf20Sopenharmony_ci *
1708c2ecf20Sopenharmony_ci * @usb_iface: pointer to our USB interface
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * @urb_edc: error density counter; used to keep a density-on-time tab
1738c2ecf20Sopenharmony_ci *     on how many soft (retryable or ignorable) errors we get. If we
1748c2ecf20Sopenharmony_ci *     go over the threshold, we consider the bus transport is failing
1758c2ecf20Sopenharmony_ci *     too much and reset.
1768c2ecf20Sopenharmony_ci *
1778c2ecf20Sopenharmony_ci * @notif_urb: URB for receiving notifications from the device.
1788c2ecf20Sopenharmony_ci *
1798c2ecf20Sopenharmony_ci * @tx_kthread: thread we use for data TX. We use a thread because in
1808c2ecf20Sopenharmony_ci *     order to do deep power saving and put the device to sleep, we
1818c2ecf20Sopenharmony_ci *     need to call usb_autopm_*() [blocking functions].
1828c2ecf20Sopenharmony_ci *
1838c2ecf20Sopenharmony_ci * @tx_wq: waitqueue for the TX kthread to sleep when there is no data
1848c2ecf20Sopenharmony_ci *     to be sent; when more data is available, it is woken up by
1858c2ecf20Sopenharmony_ci *     i2400mu_bus_tx_kick().
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * @rx_kthread: thread we use for data RX. We use a thread because in
1888c2ecf20Sopenharmony_ci *     order to do deep power saving and put the device to sleep, we
1898c2ecf20Sopenharmony_ci *     need to call usb_autopm_*() [blocking functions].
1908c2ecf20Sopenharmony_ci *
1918c2ecf20Sopenharmony_ci * @rx_wq: waitqueue for the RX kthread to sleep when there is no data
1928c2ecf20Sopenharmony_ci *     to receive. When data is available, it is woken up by
1938c2ecf20Sopenharmony_ci *     usb-notif.c:i2400mu_notification_grok().
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * @rx_pending_count: number of rx-data-ready notifications that were
1968c2ecf20Sopenharmony_ci *     still not handled by the RX kthread.
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * @rx_size: current RX buffer size that is being used.
1998c2ecf20Sopenharmony_ci *
2008c2ecf20Sopenharmony_ci * @rx_size_acc: accumulator of the sizes of the previous read
2018c2ecf20Sopenharmony_ci *     transactions.
2028c2ecf20Sopenharmony_ci *
2038c2ecf20Sopenharmony_ci * @rx_size_cnt: number of read transactions accumulated in
2048c2ecf20Sopenharmony_ci *     @rx_size_acc.
2058c2ecf20Sopenharmony_ci *
2068c2ecf20Sopenharmony_ci * @do_autopm: disable(0)/enable(>0) calling the
2078c2ecf20Sopenharmony_ci *     usb_autopm_get/put_interface() barriers when executing
2088c2ecf20Sopenharmony_ci *     commands. See doc in i2400mu_suspend() for more information.
2098c2ecf20Sopenharmony_ci *
2108c2ecf20Sopenharmony_ci * @rx_size_auto_shrink: if true, the rx_size is shrunk
2118c2ecf20Sopenharmony_ci *     automatically based on the average size of the received
2128c2ecf20Sopenharmony_ci *     transactions. This allows the receive code to allocate smaller
2138c2ecf20Sopenharmony_ci *     chunks of memory and thus reduce pressure on the memory
2148c2ecf20Sopenharmony_ci *     allocator by not wasting so much space. By default it is
2158c2ecf20Sopenharmony_ci *     enabled.
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * @debugfs_dentry: hookup for debugfs files.
2188c2ecf20Sopenharmony_ci *     These have to be in a separate directory, a child of
2198c2ecf20Sopenharmony_ci *     (wimax_dev->debugfs_dentry) so they can be removed when the
2208c2ecf20Sopenharmony_ci *     module unloads, as we don't keep each dentry.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_cistruct i2400mu {
2238c2ecf20Sopenharmony_ci	struct i2400m i2400m;		/* FIRST! See doc */
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	struct usb_device *usb_dev;
2268c2ecf20Sopenharmony_ci	struct usb_interface *usb_iface;
2278c2ecf20Sopenharmony_ci	struct edc urb_edc;		/* Error density counter */
2288c2ecf20Sopenharmony_ci	struct i2400m_endpoint_cfg endpoint_cfg;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	struct urb *notif_urb;
2318c2ecf20Sopenharmony_ci	struct task_struct *tx_kthread;
2328c2ecf20Sopenharmony_ci	wait_queue_head_t tx_wq;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	struct task_struct *rx_kthread;
2358c2ecf20Sopenharmony_ci	wait_queue_head_t rx_wq;
2368c2ecf20Sopenharmony_ci	atomic_t rx_pending_count;
2378c2ecf20Sopenharmony_ci	size_t rx_size, rx_size_acc, rx_size_cnt;
2388c2ecf20Sopenharmony_ci	atomic_t do_autopm;
2398c2ecf20Sopenharmony_ci	u8 rx_size_auto_shrink;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	struct dentry *debugfs_dentry;
2428c2ecf20Sopenharmony_ci	unsigned i6050:1;	/* 1 if this is a 6050 based SKU */
2438c2ecf20Sopenharmony_ci};
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic inline
2478c2ecf20Sopenharmony_civoid i2400mu_init(struct i2400mu *i2400mu)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	i2400m_init(&i2400mu->i2400m);
2508c2ecf20Sopenharmony_ci	edc_init(&i2400mu->urb_edc);
2518c2ecf20Sopenharmony_ci	init_waitqueue_head(&i2400mu->tx_wq);
2528c2ecf20Sopenharmony_ci	atomic_set(&i2400mu->rx_pending_count, 0);
2538c2ecf20Sopenharmony_ci	init_waitqueue_head(&i2400mu->rx_wq);
2548c2ecf20Sopenharmony_ci	i2400mu->rx_size = PAGE_SIZE - sizeof(struct skb_shared_info);
2558c2ecf20Sopenharmony_ci	atomic_set(&i2400mu->do_autopm, 1);
2568c2ecf20Sopenharmony_ci	i2400mu->rx_size_auto_shrink = 1;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ciint i2400mu_notification_setup(struct i2400mu *);
2608c2ecf20Sopenharmony_civoid i2400mu_notification_release(struct i2400mu *);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ciint i2400mu_rx_setup(struct i2400mu *);
2638c2ecf20Sopenharmony_civoid i2400mu_rx_release(struct i2400mu *);
2648c2ecf20Sopenharmony_civoid i2400mu_rx_kick(struct i2400mu *);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ciint i2400mu_tx_setup(struct i2400mu *);
2678c2ecf20Sopenharmony_civoid i2400mu_tx_release(struct i2400mu *);
2688c2ecf20Sopenharmony_civoid i2400mu_bus_tx_kick(struct i2400m *);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cissize_t i2400mu_bus_bm_cmd_send(struct i2400m *,
2718c2ecf20Sopenharmony_ci				const struct i2400m_bootrom_header *, size_t,
2728c2ecf20Sopenharmony_ci				int);
2738c2ecf20Sopenharmony_cissize_t i2400mu_bus_bm_wait_for_ack(struct i2400m *,
2748c2ecf20Sopenharmony_ci				    struct i2400m_bootrom_header *, size_t);
2758c2ecf20Sopenharmony_ci#endif /* #ifndef __I2400M_USB_H__ */
276