1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Intel Wireless WiMAX Connection 2400m
3*4882a593Smuzhiyun * USB-specific i2400m driver definitions
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
10*4882a593Smuzhiyun * are met:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
13*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
14*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above copyright
15*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in
16*4882a593Smuzhiyun * the documentation and/or other materials provided with the
17*4882a593Smuzhiyun * distribution.
18*4882a593Smuzhiyun * * Neither the name of Intel Corporation nor the names of its
19*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived
20*4882a593Smuzhiyun * from this software without specific prior written permission.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * Intel Corporation <linux-wimax@intel.com>
36*4882a593Smuzhiyun * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
37*4882a593Smuzhiyun * Yanir Lubetkin <yanirx.lubetkin@intel.com>
38*4882a593Smuzhiyun * - Initial implementation
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * This driver implements the bus-specific part of the i2400m for
42*4882a593Smuzhiyun * USB. Check i2400m.h for a generic driver description.
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * ARCHITECTURE
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * This driver listens to notifications sent from the notification
47*4882a593Smuzhiyun * endpoint (in usb-notif.c); when data is ready to read, the code in
48*4882a593Smuzhiyun * there schedules a read from the device (usb-rx.c) and then passes
49*4882a593Smuzhiyun * the data to the generic RX code (rx.c).
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * When the generic driver needs to send data (network or control), it
52*4882a593Smuzhiyun * queues up in the TX FIFO (tx.c) and that will notify the driver
53*4882a593Smuzhiyun * through the i2400m->bus_tx_kick() callback
54*4882a593Smuzhiyun * (usb-tx.c:i2400mu_bus_tx_kick) which will send the items in the
55*4882a593Smuzhiyun * FIFO queue.
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * This driver, as well, implements the USB-specific ops for the generic
58*4882a593Smuzhiyun * driver to be able to setup/teardown communication with the device
59*4882a593Smuzhiyun * [i2400m_bus_dev_start() and i2400m_bus_dev_stop()], reseting the
60*4882a593Smuzhiyun * device [i2400m_bus_reset()] and performing firmware upload
61*4882a593Smuzhiyun * [i2400m_bus_bm_cmd() and i2400_bus_bm_wait_for_ack()].
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #ifndef __I2400M_USB_H__
65*4882a593Smuzhiyun #define __I2400M_USB_H__
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #include "i2400m.h"
68*4882a593Smuzhiyun #include <linux/kthread.h>
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * Error Density Count: cheapo error density (over time) counter
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * Originally by Reinette Chatre <reinette.chatre@intel.com>
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * Embed an 'struct edc' somewhere. Each time there is a soft or
77*4882a593Smuzhiyun * retryable error, call edc_inc() and check if the error top
78*4882a593Smuzhiyun * watermark has been reached.
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun enum {
81*4882a593Smuzhiyun EDC_MAX_ERRORS = 10,
82*4882a593Smuzhiyun EDC_ERROR_TIMEFRAME = HZ,
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* error density counter */
86*4882a593Smuzhiyun struct edc {
87*4882a593Smuzhiyun unsigned long timestart;
88*4882a593Smuzhiyun u16 errorcount;
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun struct i2400m_endpoint_cfg {
92*4882a593Smuzhiyun unsigned char bulk_out;
93*4882a593Smuzhiyun unsigned char notification;
94*4882a593Smuzhiyun unsigned char reset_cold;
95*4882a593Smuzhiyun unsigned char bulk_in;
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun
edc_init(struct edc * edc)98*4882a593Smuzhiyun static inline void edc_init(struct edc *edc)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun edc->timestart = jiffies;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /**
104*4882a593Smuzhiyun * edc_inc - report a soft error and check if we are over the watermark
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * @edc: pointer to error density counter.
107*4882a593Smuzhiyun * @max_err: maximum number of errors we can accept over the timeframe
108*4882a593Smuzhiyun * @timeframe: length of the timeframe (in jiffies).
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * Returns: !0 1 if maximum acceptable errors per timeframe has been
111*4882a593Smuzhiyun * exceeded. 0 otherwise.
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * This is way to determine if the number of acceptable errors per time
114*4882a593Smuzhiyun * period has been exceeded. It is not accurate as there are cases in which
115*4882a593Smuzhiyun * this scheme will not work, for example if there are periodic occurrences
116*4882a593Smuzhiyun * of errors that straddle updates to the start time. This scheme is
117*4882a593Smuzhiyun * sufficient for our usage.
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * To use, embed a 'struct edc' somewhere, initialize it with
120*4882a593Smuzhiyun * edc_init() and when an error hits:
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * if (do_something_fails_with_a_soft_error) {
123*4882a593Smuzhiyun * if (edc_inc(&my->edc, MAX_ERRORS, MAX_TIMEFRAME))
124*4882a593Smuzhiyun * Ops, hard error, do something about it
125*4882a593Smuzhiyun * else
126*4882a593Smuzhiyun * Retry or ignore, depending on whatever
127*4882a593Smuzhiyun * }
128*4882a593Smuzhiyun */
edc_inc(struct edc * edc,u16 max_err,u16 timeframe)129*4882a593Smuzhiyun static inline int edc_inc(struct edc *edc, u16 max_err, u16 timeframe)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun unsigned long now;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun now = jiffies;
134*4882a593Smuzhiyun if (time_after(now, edc->timestart + timeframe)) {
135*4882a593Smuzhiyun edc->errorcount = 1;
136*4882a593Smuzhiyun edc->timestart = now;
137*4882a593Smuzhiyun } else if (++edc->errorcount > max_err) {
138*4882a593Smuzhiyun edc->errorcount = 0;
139*4882a593Smuzhiyun edc->timestart = now;
140*4882a593Smuzhiyun return 1;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun return 0;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /* Host-Device interface for USB */
146*4882a593Smuzhiyun enum {
147*4882a593Smuzhiyun I2400M_USB_BOOT_RETRIES = 3,
148*4882a593Smuzhiyun I2400MU_MAX_NOTIFICATION_LEN = 256,
149*4882a593Smuzhiyun I2400MU_BLK_SIZE = 16,
150*4882a593Smuzhiyun I2400MU_PL_SIZE_MAX = 0x3EFF,
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* Device IDs */
153*4882a593Smuzhiyun USB_DEVICE_ID_I6050 = 0x0186,
154*4882a593Smuzhiyun USB_DEVICE_ID_I6050_2 = 0x0188,
155*4882a593Smuzhiyun USB_DEVICE_ID_I6150 = 0x07d6,
156*4882a593Smuzhiyun USB_DEVICE_ID_I6150_2 = 0x07d7,
157*4882a593Smuzhiyun USB_DEVICE_ID_I6150_3 = 0x07d9,
158*4882a593Smuzhiyun USB_DEVICE_ID_I6250 = 0x0187,
159*4882a593Smuzhiyun };
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /**
163*4882a593Smuzhiyun * struct i2400mu - descriptor for a USB connected i2400m
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * @i2400m: bus-generic i2400m implementation; has to be first (see
166*4882a593Smuzhiyun * it's documentation in i2400m.h).
167*4882a593Smuzhiyun *
168*4882a593Smuzhiyun * @usb_dev: pointer to our USB device
169*4882a593Smuzhiyun *
170*4882a593Smuzhiyun * @usb_iface: pointer to our USB interface
171*4882a593Smuzhiyun *
172*4882a593Smuzhiyun * @urb_edc: error density counter; used to keep a density-on-time tab
173*4882a593Smuzhiyun * on how many soft (retryable or ignorable) errors we get. If we
174*4882a593Smuzhiyun * go over the threshold, we consider the bus transport is failing
175*4882a593Smuzhiyun * too much and reset.
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * @notif_urb: URB for receiving notifications from the device.
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * @tx_kthread: thread we use for data TX. We use a thread because in
180*4882a593Smuzhiyun * order to do deep power saving and put the device to sleep, we
181*4882a593Smuzhiyun * need to call usb_autopm_*() [blocking functions].
182*4882a593Smuzhiyun *
183*4882a593Smuzhiyun * @tx_wq: waitqueue for the TX kthread to sleep when there is no data
184*4882a593Smuzhiyun * to be sent; when more data is available, it is woken up by
185*4882a593Smuzhiyun * i2400mu_bus_tx_kick().
186*4882a593Smuzhiyun *
187*4882a593Smuzhiyun * @rx_kthread: thread we use for data RX. We use a thread because in
188*4882a593Smuzhiyun * order to do deep power saving and put the device to sleep, we
189*4882a593Smuzhiyun * need to call usb_autopm_*() [blocking functions].
190*4882a593Smuzhiyun *
191*4882a593Smuzhiyun * @rx_wq: waitqueue for the RX kthread to sleep when there is no data
192*4882a593Smuzhiyun * to receive. When data is available, it is woken up by
193*4882a593Smuzhiyun * usb-notif.c:i2400mu_notification_grok().
194*4882a593Smuzhiyun *
195*4882a593Smuzhiyun * @rx_pending_count: number of rx-data-ready notifications that were
196*4882a593Smuzhiyun * still not handled by the RX kthread.
197*4882a593Smuzhiyun *
198*4882a593Smuzhiyun * @rx_size: current RX buffer size that is being used.
199*4882a593Smuzhiyun *
200*4882a593Smuzhiyun * @rx_size_acc: accumulator of the sizes of the previous read
201*4882a593Smuzhiyun * transactions.
202*4882a593Smuzhiyun *
203*4882a593Smuzhiyun * @rx_size_cnt: number of read transactions accumulated in
204*4882a593Smuzhiyun * @rx_size_acc.
205*4882a593Smuzhiyun *
206*4882a593Smuzhiyun * @do_autopm: disable(0)/enable(>0) calling the
207*4882a593Smuzhiyun * usb_autopm_get/put_interface() barriers when executing
208*4882a593Smuzhiyun * commands. See doc in i2400mu_suspend() for more information.
209*4882a593Smuzhiyun *
210*4882a593Smuzhiyun * @rx_size_auto_shrink: if true, the rx_size is shrunk
211*4882a593Smuzhiyun * automatically based on the average size of the received
212*4882a593Smuzhiyun * transactions. This allows the receive code to allocate smaller
213*4882a593Smuzhiyun * chunks of memory and thus reduce pressure on the memory
214*4882a593Smuzhiyun * allocator by not wasting so much space. By default it is
215*4882a593Smuzhiyun * enabled.
216*4882a593Smuzhiyun *
217*4882a593Smuzhiyun * @debugfs_dentry: hookup for debugfs files.
218*4882a593Smuzhiyun * These have to be in a separate directory, a child of
219*4882a593Smuzhiyun * (wimax_dev->debugfs_dentry) so they can be removed when the
220*4882a593Smuzhiyun * module unloads, as we don't keep each dentry.
221*4882a593Smuzhiyun */
222*4882a593Smuzhiyun struct i2400mu {
223*4882a593Smuzhiyun struct i2400m i2400m; /* FIRST! See doc */
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun struct usb_device *usb_dev;
226*4882a593Smuzhiyun struct usb_interface *usb_iface;
227*4882a593Smuzhiyun struct edc urb_edc; /* Error density counter */
228*4882a593Smuzhiyun struct i2400m_endpoint_cfg endpoint_cfg;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun struct urb *notif_urb;
231*4882a593Smuzhiyun struct task_struct *tx_kthread;
232*4882a593Smuzhiyun wait_queue_head_t tx_wq;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun struct task_struct *rx_kthread;
235*4882a593Smuzhiyun wait_queue_head_t rx_wq;
236*4882a593Smuzhiyun atomic_t rx_pending_count;
237*4882a593Smuzhiyun size_t rx_size, rx_size_acc, rx_size_cnt;
238*4882a593Smuzhiyun atomic_t do_autopm;
239*4882a593Smuzhiyun u8 rx_size_auto_shrink;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun struct dentry *debugfs_dentry;
242*4882a593Smuzhiyun unsigned i6050:1; /* 1 if this is a 6050 based SKU */
243*4882a593Smuzhiyun };
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun static inline
i2400mu_init(struct i2400mu * i2400mu)247*4882a593Smuzhiyun void i2400mu_init(struct i2400mu *i2400mu)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun i2400m_init(&i2400mu->i2400m);
250*4882a593Smuzhiyun edc_init(&i2400mu->urb_edc);
251*4882a593Smuzhiyun init_waitqueue_head(&i2400mu->tx_wq);
252*4882a593Smuzhiyun atomic_set(&i2400mu->rx_pending_count, 0);
253*4882a593Smuzhiyun init_waitqueue_head(&i2400mu->rx_wq);
254*4882a593Smuzhiyun i2400mu->rx_size = PAGE_SIZE - sizeof(struct skb_shared_info);
255*4882a593Smuzhiyun atomic_set(&i2400mu->do_autopm, 1);
256*4882a593Smuzhiyun i2400mu->rx_size_auto_shrink = 1;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun int i2400mu_notification_setup(struct i2400mu *);
260*4882a593Smuzhiyun void i2400mu_notification_release(struct i2400mu *);
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun int i2400mu_rx_setup(struct i2400mu *);
263*4882a593Smuzhiyun void i2400mu_rx_release(struct i2400mu *);
264*4882a593Smuzhiyun void i2400mu_rx_kick(struct i2400mu *);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun int i2400mu_tx_setup(struct i2400mu *);
267*4882a593Smuzhiyun void i2400mu_tx_release(struct i2400mu *);
268*4882a593Smuzhiyun void i2400mu_bus_tx_kick(struct i2400m *);
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun ssize_t i2400mu_bus_bm_cmd_send(struct i2400m *,
271*4882a593Smuzhiyun const struct i2400m_bootrom_header *, size_t,
272*4882a593Smuzhiyun int);
273*4882a593Smuzhiyun ssize_t i2400mu_bus_bm_wait_for_ack(struct i2400m *,
274*4882a593Smuzhiyun struct i2400m_bootrom_header *, size_t);
275*4882a593Smuzhiyun #endif /* #ifndef __I2400M_USB_H__ */
276