1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *   Driver for KeyStream, KS7010 based SDIO cards.
4  *
5  *   Copyright (C) 2006-2008 KeyStream Corp.
6  *   Copyright (C) 2009 Renesas Technology Corp.
7  *   Copyright (C) 2016 Sang Engineering, Wolfram Sang
8  */
9 
10 #include <linux/atomic.h>
11 #include <linux/firmware.h>
12 #include <linux/jiffies.h>
13 #include <linux/mmc/card.h>
14 #include <linux/mmc/sdio_func.h>
15 #include <linux/module.h>
16 #include <linux/workqueue.h>
17 #include "ks_wlan.h"
18 #include "ks_hostif.h"
19 
20 #define ROM_FILE "ks7010sd.rom"
21 
22 /*  SDIO KeyStream vendor and device */
23 #define SDIO_VENDOR_ID_KS_CODE_A	0x005b
24 #define SDIO_VENDOR_ID_KS_CODE_B	0x0023
25 
26 /* Older sources suggest earlier versions were named 7910 or 79xx */
27 #define SDIO_DEVICE_ID_KS_7010		0x7910
28 
29 /* Read/Write Status Register */
30 #define READ_STATUS_REG		0x000000
31 #define WRITE_STATUS_REG	0x00000C
32 enum reg_status_type {
33 	REG_STATUS_BUSY,
34 	REG_STATUS_IDLE
35 };
36 
37 /* Read Index Register */
38 #define READ_INDEX_REG		0x000004
39 
40 /* Read Data Size Register */
41 #define READ_DATA_SIZE_REG	0x000008
42 
43 /* Write Index Register */
44 #define WRITE_INDEX_REG		0x000010
45 
46 /*
47  * Write Status/Read Data Size Register
48  * for network packet (less than 2048 bytes data)
49  */
50 #define WSTATUS_RSIZE_REG	0x000014
51 
52 /* Write Status Register value */
53 #define WSTATUS_MASK		0x80
54 
55 /* Read Data Size Register value [10:4] */
56 #define RSIZE_MASK		0x7F
57 
58 /* ARM to SD interrupt Enable */
59 #define INT_ENABLE_REG		0x000020
60 /* ARM to SD interrupt Pending */
61 #define INT_PENDING_REG		0x000024
62 
63 #define INT_GCR_B              BIT(7)
64 #define INT_GCR_A              BIT(6)
65 #define INT_WRITE_STATUS       BIT(5)
66 #define INT_WRITE_INDEX        BIT(4)
67 #define INT_WRITE_SIZE         BIT(3)
68 #define INT_READ_STATUS        BIT(2)
69 #define INT_READ_INDEX         BIT(1)
70 #define INT_READ_SIZE          BIT(0)
71 
72 /* General Communication Register A */
73 #define GCR_A_REG		0x000028
74 enum gen_com_reg_a {
75 	GCR_A_INIT,
76 	GCR_A_REMAP,
77 	GCR_A_RUN
78 };
79 
80 /* General Communication Register B */
81 #define GCR_B_REG		0x00002C
82 enum gen_com_reg_b {
83 	GCR_B_ACTIVE,
84 	GCR_B_DOZE
85 };
86 
87 /* Wakeup Register */
88 #define WAKEUP_REG		0x008018
89 #define WAKEUP_REQ		0x5a
90 
91 /* AHB Data Window  0x010000-0x01FFFF */
92 #define DATA_WINDOW		0x010000
93 #define WINDOW_SIZE		(64 * 1024)
94 
95 #define KS7010_IRAM_ADDRESS	0x06000000
96 
97 #define KS7010_IO_BLOCK_SIZE 512
98 
99 /**
100  * struct ks_sdio_card - SDIO device data.
101  *
102  * Structure is used as the &struct sdio_func private data.
103  *
104  * @func: Pointer to the SDIO function device.
105  * @priv: Pointer to the &struct net_device private data.
106  */
107 struct ks_sdio_card {
108 	struct sdio_func *func;
109 	struct ks_wlan_private *priv;
110 };
111 
ks7010_to_func(struct ks_wlan_private * priv)112 static struct sdio_func *ks7010_to_func(struct ks_wlan_private *priv)
113 {
114 	struct ks_sdio_card *ks_sdio = priv->if_hw;
115 
116 	return ks_sdio->func;
117 }
118 
119 /* Read single byte from device address into byte (CMD52) */
ks7010_sdio_readb(struct ks_wlan_private * priv,u32 address,u8 * byte)120 static int ks7010_sdio_readb(struct ks_wlan_private *priv,
121 			     u32 address, u8 *byte)
122 {
123 	struct sdio_func *func = ks7010_to_func(priv);
124 	int ret;
125 
126 	*byte = sdio_readb(func, address, &ret);
127 
128 	return ret;
129 }
130 
131 /* Read length bytes from device address into buffer (CMD53) */
ks7010_sdio_read(struct ks_wlan_private * priv,u32 address,u8 * buffer,unsigned int length)132 static int ks7010_sdio_read(struct ks_wlan_private *priv, u32 address,
133 			    u8 *buffer, unsigned int length)
134 {
135 	struct sdio_func *func = ks7010_to_func(priv);
136 
137 	return sdio_memcpy_fromio(func, buffer, address, length);
138 }
139 
140 /* Write single byte to device address (CMD52) */
ks7010_sdio_writeb(struct ks_wlan_private * priv,u32 address,u8 byte)141 static int ks7010_sdio_writeb(struct ks_wlan_private *priv,
142 			      u32 address, u8 byte)
143 {
144 	struct sdio_func *func = ks7010_to_func(priv);
145 	int ret;
146 
147 	sdio_writeb(func, byte, address, &ret);
148 
149 	return ret;
150 }
151 
152 /* Write length bytes to device address from buffer (CMD53) */
ks7010_sdio_write(struct ks_wlan_private * priv,u32 address,u8 * buffer,unsigned int length)153 static int ks7010_sdio_write(struct ks_wlan_private *priv, u32 address,
154 			     u8 *buffer, unsigned int length)
155 {
156 	struct sdio_func *func = ks7010_to_func(priv);
157 
158 	return sdio_memcpy_toio(func, address, buffer, length);
159 }
160 
ks_wlan_hw_sleep_doze_request(struct ks_wlan_private * priv)161 static void ks_wlan_hw_sleep_doze_request(struct ks_wlan_private *priv)
162 {
163 	int ret;
164 
165 	/* clear request */
166 	atomic_set(&priv->sleepstatus.doze_request, 0);
167 
168 	if (atomic_read(&priv->sleepstatus.status) == 0) {
169 		ret = ks7010_sdio_writeb(priv, GCR_B_REG, GCR_B_DOZE);
170 		if (ret) {
171 			netdev_err(priv->net_dev, "write GCR_B_REG\n");
172 			goto set_sleep_mode;
173 		}
174 		atomic_set(&priv->sleepstatus.status, 1);
175 		priv->last_doze = jiffies;
176 	}
177 
178 set_sleep_mode:
179 	priv->sleep_mode = atomic_read(&priv->sleepstatus.status);
180 }
181 
ks_wlan_hw_sleep_wakeup_request(struct ks_wlan_private * priv)182 static void ks_wlan_hw_sleep_wakeup_request(struct ks_wlan_private *priv)
183 {
184 	int ret;
185 
186 	/* clear request */
187 	atomic_set(&priv->sleepstatus.wakeup_request, 0);
188 
189 	if (atomic_read(&priv->sleepstatus.status) == 1) {
190 		ret = ks7010_sdio_writeb(priv, WAKEUP_REG, WAKEUP_REQ);
191 		if (ret) {
192 			netdev_err(priv->net_dev, "write WAKEUP_REG\n");
193 			goto set_sleep_mode;
194 		}
195 		atomic_set(&priv->sleepstatus.status, 0);
196 		priv->last_wakeup = jiffies;
197 		++priv->wakeup_count;
198 	}
199 
200 set_sleep_mode:
201 	priv->sleep_mode = atomic_read(&priv->sleepstatus.status);
202 }
203 
ks_wlan_hw_wakeup_request(struct ks_wlan_private * priv)204 void ks_wlan_hw_wakeup_request(struct ks_wlan_private *priv)
205 {
206 	int ret;
207 
208 	if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
209 		ret = ks7010_sdio_writeb(priv, WAKEUP_REG, WAKEUP_REQ);
210 		if (ret)
211 			netdev_err(priv->net_dev, "write WAKEUP_REG\n");
212 
213 		priv->last_wakeup = jiffies;
214 		++priv->wakeup_count;
215 	}
216 }
217 
_ks_wlan_hw_power_save(struct ks_wlan_private * priv)218 static void _ks_wlan_hw_power_save(struct ks_wlan_private *priv)
219 {
220 	u8 byte;
221 	int ret;
222 
223 	if (priv->reg.power_mgmt == POWER_MGMT_ACTIVE)
224 		return;
225 
226 	if (priv->reg.operation_mode != MODE_INFRASTRUCTURE)
227 		return;
228 
229 	if (!is_connect_status(priv->connect_status))
230 		return;
231 
232 	if (priv->dev_state != DEVICE_STATE_SLEEP)
233 		return;
234 
235 	if (atomic_read(&priv->psstatus.status) == PS_SNOOZE)
236 		return;
237 
238 	netdev_dbg(priv->net_dev,
239 		   "STATUS:\n"
240 		   "- psstatus.status = %d\n"
241 		   "- psstatus.confirm_wait = %d\n"
242 		   "- psstatus.snooze_guard = %d\n"
243 		   "- txq_count = %d\n",
244 		   atomic_read(&priv->psstatus.status),
245 		   atomic_read(&priv->psstatus.confirm_wait),
246 		   atomic_read(&priv->psstatus.snooze_guard),
247 		   txq_count(priv));
248 
249 	if (atomic_read(&priv->psstatus.confirm_wait) ||
250 	    atomic_read(&priv->psstatus.snooze_guard) ||
251 	    txq_has_space(priv)) {
252 		queue_delayed_work(priv->wq, &priv->rw_dwork, 0);
253 		return;
254 	}
255 
256 	ret = ks7010_sdio_readb(priv, INT_PENDING_REG, &byte);
257 	if (ret) {
258 		netdev_err(priv->net_dev, "read INT_PENDING_REG\n");
259 		goto queue_delayed_work;
260 	}
261 	if (byte)
262 		goto queue_delayed_work;
263 
264 	ret = ks7010_sdio_writeb(priv, GCR_B_REG, GCR_B_DOZE);
265 	if (ret) {
266 		netdev_err(priv->net_dev, "write GCR_B_REG\n");
267 		goto queue_delayed_work;
268 	}
269 	atomic_set(&priv->psstatus.status, PS_SNOOZE);
270 
271 	return;
272 
273 queue_delayed_work:
274 	queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
275 }
276 
ks_wlan_hw_power_save(struct ks_wlan_private * priv)277 int ks_wlan_hw_power_save(struct ks_wlan_private *priv)
278 {
279 	queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
280 	return 0;
281 }
282 
enqueue_txdev(struct ks_wlan_private * priv,unsigned char * p,unsigned long size,void (* complete_handler)(struct ks_wlan_private * priv,struct sk_buff * skb),struct sk_buff * skb)283 static int enqueue_txdev(struct ks_wlan_private *priv, unsigned char *p,
284 			 unsigned long size,
285 			 void (*complete_handler)(struct ks_wlan_private *priv,
286 						  struct sk_buff *skb),
287 			 struct sk_buff *skb)
288 {
289 	struct tx_device_buffer *sp;
290 	int ret;
291 
292 	if (priv->dev_state < DEVICE_STATE_BOOT) {
293 		ret = -EPERM;
294 		goto err_complete;
295 	}
296 
297 	if ((TX_DEVICE_BUFF_SIZE - 1) <= txq_count(priv)) {
298 		netdev_err(priv->net_dev, "tx buffer overflow\n");
299 		ret = -EOVERFLOW;
300 		goto err_complete;
301 	}
302 
303 	sp = &priv->tx_dev.tx_dev_buff[priv->tx_dev.qtail];
304 	sp->sendp = p;
305 	sp->size = size;
306 	sp->complete_handler = complete_handler;
307 	sp->skb = skb;
308 	inc_txqtail(priv);
309 
310 	return 0;
311 
312 err_complete:
313 	kfree(p);
314 	if (complete_handler)
315 		(*complete_handler)(priv, skb);
316 
317 	return ret;
318 }
319 
320 /* write data */
write_to_device(struct ks_wlan_private * priv,u8 * buffer,unsigned long size)321 static int write_to_device(struct ks_wlan_private *priv, u8 *buffer,
322 			   unsigned long size)
323 {
324 	struct hostif_hdr *hdr;
325 	int ret;
326 
327 	hdr = (struct hostif_hdr *)buffer;
328 
329 	if (le16_to_cpu(hdr->event) < HIF_DATA_REQ ||
330 	    le16_to_cpu(hdr->event) > HIF_REQ_MAX) {
331 		netdev_err(priv->net_dev, "unknown event=%04X\n", hdr->event);
332 		return 0;
333 	}
334 
335 	ret = ks7010_sdio_write(priv, DATA_WINDOW, buffer, size);
336 	if (ret) {
337 		netdev_err(priv->net_dev, "write DATA_WINDOW\n");
338 		return ret;
339 	}
340 
341 	ret = ks7010_sdio_writeb(priv, WRITE_STATUS_REG, REG_STATUS_BUSY);
342 	if (ret) {
343 		netdev_err(priv->net_dev, "write WRITE_STATUS_REG\n");
344 		return ret;
345 	}
346 
347 	return 0;
348 }
349 
tx_device_task(struct ks_wlan_private * priv)350 static void tx_device_task(struct ks_wlan_private *priv)
351 {
352 	struct tx_device_buffer *sp;
353 	int ret;
354 
355 	if (!txq_has_space(priv) ||
356 	    atomic_read(&priv->psstatus.status) == PS_SNOOZE)
357 		return;
358 
359 	sp = &priv->tx_dev.tx_dev_buff[priv->tx_dev.qhead];
360 	if (priv->dev_state >= DEVICE_STATE_BOOT) {
361 		ret = write_to_device(priv, sp->sendp, sp->size);
362 		if (ret) {
363 			netdev_err(priv->net_dev,
364 				   "write_to_device error !!(%d)\n", ret);
365 			queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
366 			return;
367 		}
368 	}
369 	kfree(sp->sendp);
370 	if (sp->complete_handler)	/* TX Complete */
371 		(*sp->complete_handler)(priv, sp->skb);
372 	inc_txqhead(priv);
373 
374 	if (txq_has_space(priv))
375 		queue_delayed_work(priv->wq, &priv->rw_dwork, 0);
376 }
377 
ks_wlan_hw_tx(struct ks_wlan_private * priv,void * p,unsigned long size,void (* complete_handler)(struct ks_wlan_private * priv,struct sk_buff * skb),struct sk_buff * skb)378 int ks_wlan_hw_tx(struct ks_wlan_private *priv, void *p, unsigned long size,
379 		  void (*complete_handler)(struct ks_wlan_private *priv,
380 					   struct sk_buff *skb),
381 		  struct sk_buff *skb)
382 {
383 	int result;
384 	struct hostif_hdr *hdr;
385 
386 	hdr = (struct hostif_hdr *)p;
387 
388 	if (le16_to_cpu(hdr->event) < HIF_DATA_REQ ||
389 	    le16_to_cpu(hdr->event) > HIF_REQ_MAX) {
390 		netdev_err(priv->net_dev, "unknown event=%04X\n", hdr->event);
391 		return 0;
392 	}
393 
394 	/* add event to hostt buffer */
395 	priv->hostt.buff[priv->hostt.qtail] = le16_to_cpu(hdr->event);
396 	priv->hostt.qtail = (priv->hostt.qtail + 1) % SME_EVENT_BUFF_SIZE;
397 
398 	spin_lock_bh(&priv->tx_dev.tx_dev_lock);
399 	result = enqueue_txdev(priv, p, size, complete_handler, skb);
400 	spin_unlock_bh(&priv->tx_dev.tx_dev_lock);
401 
402 	if (txq_has_space(priv))
403 		queue_delayed_work(priv->wq, &priv->rw_dwork, 0);
404 
405 	return result;
406 }
407 
rx_event_task(struct tasklet_struct * t)408 static void rx_event_task(struct tasklet_struct *t)
409 {
410 	struct ks_wlan_private *priv = from_tasklet(priv, t, rx_bh_task);
411 	struct rx_device_buffer *rp;
412 
413 	if (rxq_has_space(priv) && priv->dev_state >= DEVICE_STATE_BOOT) {
414 		rp = &priv->rx_dev.rx_dev_buff[priv->rx_dev.qhead];
415 		hostif_receive(priv, rp->data, rp->size);
416 		inc_rxqhead(priv);
417 
418 		if (rxq_has_space(priv))
419 			tasklet_schedule(&priv->rx_bh_task);
420 	}
421 }
422 
ks_wlan_hw_rx(struct ks_wlan_private * priv,size_t size)423 static void ks_wlan_hw_rx(struct ks_wlan_private *priv, size_t size)
424 {
425 	int ret;
426 	struct rx_device_buffer *rx_buffer;
427 	struct hostif_hdr *hdr;
428 	u16 event = 0;
429 
430 	/* receive data */
431 	if (rxq_count(priv) >= (RX_DEVICE_BUFF_SIZE - 1)) {
432 		netdev_err(priv->net_dev, "rx buffer overflow\n");
433 		return;
434 	}
435 	rx_buffer = &priv->rx_dev.rx_dev_buff[priv->rx_dev.qtail];
436 
437 	ret = ks7010_sdio_read(priv, DATA_WINDOW, &rx_buffer->data[0],
438 			       hif_align_size(size));
439 	if (ret)
440 		return;
441 
442 	/* length check */
443 	if (size > 2046 || size == 0) {
444 #ifdef DEBUG
445 		print_hex_dump_bytes("INVALID DATA dump: ",
446 				     DUMP_PREFIX_OFFSET,
447 				     rx_buffer->data, 32);
448 #endif
449 		ret = ks7010_sdio_writeb(priv, READ_STATUS_REG,
450 					 REG_STATUS_IDLE);
451 		if (ret)
452 			netdev_err(priv->net_dev, "write READ_STATUS_REG\n");
453 
454 		/* length check fail */
455 		return;
456 	}
457 
458 	hdr = (struct hostif_hdr *)&rx_buffer->data[0];
459 	rx_buffer->size = le16_to_cpu(hdr->size) + sizeof(hdr->size);
460 	event = le16_to_cpu(hdr->event);
461 	inc_rxqtail(priv);
462 
463 	ret = ks7010_sdio_writeb(priv, READ_STATUS_REG, REG_STATUS_IDLE);
464 	if (ret)
465 		netdev_err(priv->net_dev, "write READ_STATUS_REG\n");
466 
467 	if (atomic_read(&priv->psstatus.confirm_wait) && is_hif_conf(event)) {
468 		netdev_dbg(priv->net_dev, "IS_HIF_CONF true !!\n");
469 		atomic_dec(&priv->psstatus.confirm_wait);
470 	}
471 
472 	tasklet_schedule(&priv->rx_bh_task);
473 }
474 
ks7010_rw_function(struct work_struct * work)475 static void ks7010_rw_function(struct work_struct *work)
476 {
477 	struct ks_wlan_private *priv = container_of(work,
478 						    struct ks_wlan_private,
479 						    rw_dwork.work);
480 	struct sdio_func *func = ks7010_to_func(priv);
481 	u8 byte;
482 	int ret;
483 
484 	/* wait after DOZE */
485 	if (time_after(priv->last_doze + msecs_to_jiffies(30), jiffies)) {
486 		netdev_dbg(priv->net_dev, "wait after DOZE\n");
487 		queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
488 		return;
489 	}
490 
491 	/* wait after WAKEUP */
492 	while (time_after(priv->last_wakeup + msecs_to_jiffies(30), jiffies)) {
493 		netdev_dbg(priv->net_dev, "wait after WAKEUP\n");
494 		dev_info(&func->dev, "wake: %lu %lu\n",
495 			 priv->last_wakeup + msecs_to_jiffies(30), jiffies);
496 		msleep(30);
497 	}
498 
499 	sdio_claim_host(func);
500 
501 	/* power save wakeup */
502 	if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
503 		if (txq_has_space(priv)) {
504 			ks_wlan_hw_wakeup_request(priv);
505 			queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
506 		}
507 		goto release_host;
508 	}
509 
510 	/* sleep mode doze */
511 	if (atomic_read(&priv->sleepstatus.doze_request) == 1) {
512 		ks_wlan_hw_sleep_doze_request(priv);
513 		goto release_host;
514 	}
515 	/* sleep mode wakeup */
516 	if (atomic_read(&priv->sleepstatus.wakeup_request) == 1) {
517 		ks_wlan_hw_sleep_wakeup_request(priv);
518 		goto release_host;
519 	}
520 
521 	/* read (WriteStatus/ReadDataSize FN1:00_0014) */
522 	ret = ks7010_sdio_readb(priv, WSTATUS_RSIZE_REG, &byte);
523 	if (ret) {
524 		netdev_err(priv->net_dev, "read WSTATUS_RSIZE_REG psstatus=%d\n",
525 			   atomic_read(&priv->psstatus.status));
526 		goto release_host;
527 	}
528 
529 	if (byte & RSIZE_MASK) {	/* Read schedule */
530 		ks_wlan_hw_rx(priv, (size_t)((byte & RSIZE_MASK) << 4));
531 	}
532 	if ((byte & WSTATUS_MASK))
533 		tx_device_task(priv);
534 
535 	_ks_wlan_hw_power_save(priv);
536 
537 release_host:
538 	sdio_release_host(func);
539 }
540 
ks_sdio_interrupt(struct sdio_func * func)541 static void ks_sdio_interrupt(struct sdio_func *func)
542 {
543 	int ret;
544 	struct ks_sdio_card *card;
545 	struct ks_wlan_private *priv;
546 	u8 status, rsize, byte;
547 
548 	card = sdio_get_drvdata(func);
549 	priv = card->priv;
550 
551 	if (priv->dev_state < DEVICE_STATE_BOOT)
552 		goto queue_delayed_work;
553 
554 	ret = ks7010_sdio_readb(priv, INT_PENDING_REG, &status);
555 	if (ret) {
556 		netdev_err(priv->net_dev, "read INT_PENDING_REG\n");
557 		goto queue_delayed_work;
558 	}
559 
560 	/* schedule task for interrupt status */
561 	/* bit7 -> Write General Communication B register */
562 	/* read (General Communication B register) */
563 	/* bit5 -> Write Status Idle */
564 	/* bit2 -> Read Status Busy  */
565 	if (status & INT_GCR_B ||
566 	    atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
567 		ret = ks7010_sdio_readb(priv, GCR_B_REG, &byte);
568 		if (ret) {
569 			netdev_err(priv->net_dev, "read GCR_B_REG\n");
570 			goto queue_delayed_work;
571 		}
572 		if (byte == GCR_B_ACTIVE) {
573 			if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
574 				atomic_set(&priv->psstatus.status, PS_WAKEUP);
575 				priv->wakeup_count = 0;
576 			}
577 			complete(&priv->psstatus.wakeup_wait);
578 		}
579 	}
580 
581 	do {
582 		/* read (WriteStatus/ReadDataSize FN1:00_0014) */
583 		ret = ks7010_sdio_readb(priv, WSTATUS_RSIZE_REG, &byte);
584 		if (ret) {
585 			netdev_err(priv->net_dev, "read WSTATUS_RSIZE_REG\n");
586 			goto queue_delayed_work;
587 		}
588 		rsize = byte & RSIZE_MASK;
589 		if (rsize != 0)		/* Read schedule */
590 			ks_wlan_hw_rx(priv, (size_t)(rsize << 4));
591 
592 		if (byte & WSTATUS_MASK) {
593 			if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
594 				if (txq_has_space(priv)) {
595 					ks_wlan_hw_wakeup_request(priv);
596 					queue_delayed_work(priv->wq,
597 							   &priv->rw_dwork, 1);
598 					return;
599 				}
600 			} else {
601 				tx_device_task(priv);
602 			}
603 		}
604 	} while (rsize);
605 
606 queue_delayed_work:
607 	queue_delayed_work(priv->wq, &priv->rw_dwork, 0);
608 }
609 
trx_device_init(struct ks_wlan_private * priv)610 static int trx_device_init(struct ks_wlan_private *priv)
611 {
612 	priv->tx_dev.qhead = 0;
613 	priv->tx_dev.qtail = 0;
614 
615 	priv->rx_dev.qhead = 0;
616 	priv->rx_dev.qtail = 0;
617 
618 	spin_lock_init(&priv->tx_dev.tx_dev_lock);
619 	spin_lock_init(&priv->rx_dev.rx_dev_lock);
620 
621 	tasklet_setup(&priv->rx_bh_task, rx_event_task);
622 
623 	return 0;
624 }
625 
trx_device_exit(struct ks_wlan_private * priv)626 static void trx_device_exit(struct ks_wlan_private *priv)
627 {
628 	struct tx_device_buffer *sp;
629 
630 	/* tx buffer clear */
631 	while (txq_has_space(priv)) {
632 		sp = &priv->tx_dev.tx_dev_buff[priv->tx_dev.qhead];
633 		kfree(sp->sendp);
634 		if (sp->complete_handler)	/* TX Complete */
635 			(*sp->complete_handler)(priv, sp->skb);
636 		inc_txqhead(priv);
637 	}
638 
639 	tasklet_kill(&priv->rx_bh_task);
640 }
641 
ks7010_sdio_update_index(struct ks_wlan_private * priv,u32 index)642 static int ks7010_sdio_update_index(struct ks_wlan_private *priv, u32 index)
643 {
644 	int ret;
645 	unsigned char *data_buf;
646 
647 	data_buf = kmemdup(&index, sizeof(u32), GFP_KERNEL);
648 	if (!data_buf)
649 		return -ENOMEM;
650 
651 	ret = ks7010_sdio_write(priv, WRITE_INDEX_REG, data_buf, sizeof(index));
652 	if (ret)
653 		goto err_free_data_buf;
654 
655 	ret = ks7010_sdio_write(priv, READ_INDEX_REG, data_buf, sizeof(index));
656 	if (ret)
657 		goto err_free_data_buf;
658 
659 	return 0;
660 
661 err_free_data_buf:
662 	kfree(data_buf);
663 
664 	return ret;
665 }
666 
667 #define ROM_BUFF_SIZE (64 * 1024)
ks7010_sdio_data_compare(struct ks_wlan_private * priv,u32 address,u8 * data,unsigned int size)668 static int ks7010_sdio_data_compare(struct ks_wlan_private *priv, u32 address,
669 				    u8 *data, unsigned int size)
670 {
671 	int ret;
672 	u8 *read_buf;
673 
674 	read_buf = kmalloc(ROM_BUFF_SIZE, GFP_KERNEL);
675 	if (!read_buf)
676 		return -ENOMEM;
677 
678 	ret = ks7010_sdio_read(priv, address, read_buf, size);
679 	if (ret)
680 		goto err_free_read_buf;
681 
682 	if (memcmp(data, read_buf, size) != 0) {
683 		ret = -EIO;
684 		netdev_err(priv->net_dev, "data compare error (%d)\n", ret);
685 		goto err_free_read_buf;
686 	}
687 
688 	return 0;
689 
690 err_free_read_buf:
691 	kfree(read_buf);
692 
693 	return ret;
694 }
695 
ks7010_copy_firmware(struct ks_wlan_private * priv,const struct firmware * fw_entry)696 static int ks7010_copy_firmware(struct ks_wlan_private *priv,
697 				const struct firmware *fw_entry)
698 {
699 	unsigned int length;
700 	unsigned int size;
701 	unsigned int offset;
702 	unsigned int n = 0;
703 	u8 *rom_buf;
704 	int ret;
705 
706 	rom_buf = kmalloc(ROM_BUFF_SIZE, GFP_KERNEL);
707 	if (!rom_buf)
708 		return -ENOMEM;
709 
710 	length = fw_entry->size;
711 
712 	do {
713 		if (length >= ROM_BUFF_SIZE) {
714 			size = ROM_BUFF_SIZE;
715 			length = length - ROM_BUFF_SIZE;
716 		} else {
717 			size = length;
718 			length = 0;
719 		}
720 		if (size == 0)
721 			break;
722 
723 		memcpy(rom_buf, fw_entry->data + n, size);
724 
725 		offset = n;
726 		ret = ks7010_sdio_update_index(priv,
727 					       KS7010_IRAM_ADDRESS + offset);
728 		if (ret)
729 			goto free_rom_buf;
730 
731 		ret = ks7010_sdio_write(priv, DATA_WINDOW, rom_buf, size);
732 		if (ret)
733 			goto free_rom_buf;
734 
735 		ret = ks7010_sdio_data_compare(priv,
736 					       DATA_WINDOW, rom_buf, size);
737 		if (ret)
738 			goto free_rom_buf;
739 
740 		n += size;
741 
742 	} while (size);
743 
744 	ret = ks7010_sdio_writeb(priv, GCR_A_REG, GCR_A_REMAP);
745 
746 free_rom_buf:
747 	kfree(rom_buf);
748 	return ret;
749 }
750 
ks7010_upload_firmware(struct ks_sdio_card * card)751 static int ks7010_upload_firmware(struct ks_sdio_card *card)
752 {
753 	struct ks_wlan_private *priv = card->priv;
754 	struct sdio_func *func = ks7010_to_func(priv);
755 	unsigned int n;
756 	u8 byte = 0;
757 	int ret;
758 	const struct firmware *fw_entry = NULL;
759 
760 	sdio_claim_host(func);
761 
762 	/* Firmware running ? */
763 	ret = ks7010_sdio_readb(priv, GCR_A_REG, &byte);
764 	if (ret)
765 		goto release_host;
766 	if (byte == GCR_A_RUN) {
767 		netdev_dbg(priv->net_dev, "MAC firmware running ...\n");
768 		ret = -EBUSY;
769 		goto release_host;
770 	}
771 
772 	ret = request_firmware(&fw_entry, ROM_FILE,
773 			       &func->dev);
774 	if (ret)
775 		goto release_host;
776 
777 	ret = ks7010_copy_firmware(priv, fw_entry);
778 	if (ret)
779 		goto release_firmware;
780 
781 	/* Firmware running check */
782 	for (n = 0; n < 50; ++n) {
783 		usleep_range(10000, 11000);	/* wait_ms(10); */
784 		ret = ks7010_sdio_readb(priv, GCR_A_REG, &byte);
785 		if (ret)
786 			goto release_firmware;
787 
788 		if (byte == GCR_A_RUN)
789 			break;
790 	}
791 	if ((50) <= n) {
792 		netdev_err(priv->net_dev, "firmware can't start\n");
793 		ret = -EIO;
794 		goto release_firmware;
795 	}
796 
797 	ret = 0;
798 
799  release_firmware:
800 	release_firmware(fw_entry);
801  release_host:
802 	sdio_release_host(func);
803 
804 	return ret;
805 }
806 
ks7010_sme_enqueue_events(struct ks_wlan_private * priv)807 static void ks7010_sme_enqueue_events(struct ks_wlan_private *priv)
808 {
809 	static const u16 init_events[] = {
810 		SME_GET_EEPROM_CKSUM, SME_STOP_REQUEST,
811 		SME_RTS_THRESHOLD_REQUEST, SME_FRAGMENTATION_THRESHOLD_REQUEST,
812 		SME_WEP_INDEX_REQUEST, SME_WEP_KEY1_REQUEST,
813 		SME_WEP_KEY2_REQUEST, SME_WEP_KEY3_REQUEST,
814 		SME_WEP_KEY4_REQUEST, SME_WEP_FLAG_REQUEST,
815 		SME_RSN_ENABLED_REQUEST, SME_MODE_SET_REQUEST,
816 		SME_START_REQUEST
817 	};
818 	int ev;
819 
820 	for (ev = 0; ev < ARRAY_SIZE(init_events); ev++)
821 		hostif_sme_enqueue(priv, init_events[ev]);
822 }
823 
ks7010_card_init(struct ks_wlan_private * priv)824 static void ks7010_card_init(struct ks_wlan_private *priv)
825 {
826 	init_completion(&priv->confirm_wait);
827 
828 	/* get mac address & firmware version */
829 	hostif_sme_enqueue(priv, SME_START);
830 
831 	if (!wait_for_completion_interruptible_timeout
832 	    (&priv->confirm_wait, 5 * HZ)) {
833 		netdev_dbg(priv->net_dev, "wait time out!! SME_START\n");
834 	}
835 
836 	if (priv->mac_address_valid && priv->version_size != 0)
837 		priv->dev_state = DEVICE_STATE_PREINIT;
838 
839 	ks7010_sme_enqueue_events(priv);
840 
841 	if (!wait_for_completion_interruptible_timeout
842 	    (&priv->confirm_wait, 5 * HZ)) {
843 		netdev_dbg(priv->net_dev, "wait time out!! wireless parameter set\n");
844 	}
845 
846 	if (priv->dev_state >= DEVICE_STATE_PREINIT) {
847 		netdev_dbg(priv->net_dev, "DEVICE READY!!\n");
848 		priv->dev_state = DEVICE_STATE_READY;
849 	}
850 }
851 
ks7010_init_defaults(struct ks_wlan_private * priv)852 static void ks7010_init_defaults(struct ks_wlan_private *priv)
853 {
854 	priv->reg.tx_rate = TX_RATE_AUTO;
855 	priv->reg.preamble = LONG_PREAMBLE;
856 	priv->reg.power_mgmt = POWER_MGMT_ACTIVE;
857 	priv->reg.scan_type = ACTIVE_SCAN;
858 	priv->reg.beacon_lost_count = 20;
859 	priv->reg.rts = 2347UL;
860 	priv->reg.fragment = 2346UL;
861 	priv->reg.phy_type = D_11BG_COMPATIBLE_MODE;
862 	priv->reg.cts_mode = CTS_MODE_FALSE;
863 	priv->reg.rate_set.body[11] = TX_RATE_54M;
864 	priv->reg.rate_set.body[10] = TX_RATE_48M;
865 	priv->reg.rate_set.body[9] = TX_RATE_36M;
866 	priv->reg.rate_set.body[8] = TX_RATE_18M;
867 	priv->reg.rate_set.body[7] = TX_RATE_9M;
868 	priv->reg.rate_set.body[6] = TX_RATE_24M | BASIC_RATE;
869 	priv->reg.rate_set.body[5] = TX_RATE_12M | BASIC_RATE;
870 	priv->reg.rate_set.body[4] = TX_RATE_6M | BASIC_RATE;
871 	priv->reg.rate_set.body[3] = TX_RATE_11M | BASIC_RATE;
872 	priv->reg.rate_set.body[2] = TX_RATE_5M | BASIC_RATE;
873 	priv->reg.rate_set.body[1] = TX_RATE_2M | BASIC_RATE;
874 	priv->reg.rate_set.body[0] = TX_RATE_1M | BASIC_RATE;
875 	priv->reg.tx_rate = TX_RATE_FULL_AUTO;
876 	priv->reg.rate_set.size = 12;
877 }
878 
ks7010_sdio_setup_irqs(struct sdio_func * func)879 static int ks7010_sdio_setup_irqs(struct sdio_func *func)
880 {
881 	int ret;
882 
883 	/* interrupt disable */
884 	sdio_writeb(func, 0, INT_ENABLE_REG, &ret);
885 	if (ret)
886 		goto irq_error;
887 
888 	sdio_writeb(func, 0xff, INT_PENDING_REG, &ret);
889 	if (ret)
890 		goto irq_error;
891 
892 	/* setup interrupt handler */
893 	ret = sdio_claim_irq(func, ks_sdio_interrupt);
894 
895 irq_error:
896 	return ret;
897 }
898 
ks7010_sdio_init_irqs(struct sdio_func * func,struct ks_wlan_private * priv)899 static void ks7010_sdio_init_irqs(struct sdio_func *func,
900 				  struct ks_wlan_private *priv)
901 {
902 	u8 byte;
903 	int ret;
904 
905 	/*
906 	 * interrupt setting
907 	 * clear Interrupt status write
908 	 * (ARMtoSD_InterruptPending FN1:00_0024)
909 	 */
910 	sdio_claim_host(func);
911 	ret = ks7010_sdio_writeb(priv, INT_PENDING_REG, 0xff);
912 	sdio_release_host(func);
913 	if (ret)
914 		netdev_err(priv->net_dev, "write INT_PENDING_REG\n");
915 
916 	/* enable ks7010sdio interrupt */
917 	byte = (INT_GCR_B | INT_READ_STATUS | INT_WRITE_STATUS);
918 	sdio_claim_host(func);
919 	ret = ks7010_sdio_writeb(priv, INT_ENABLE_REG, byte);
920 	sdio_release_host(func);
921 	if (ret)
922 		netdev_err(priv->net_dev, "write INT_ENABLE_REG\n");
923 }
924 
ks7010_private_init(struct ks_wlan_private * priv,struct ks_sdio_card * card,struct net_device * netdev)925 static void ks7010_private_init(struct ks_wlan_private *priv,
926 				struct ks_sdio_card *card,
927 				struct net_device *netdev)
928 {
929 	/* private memory initialize */
930 	priv->if_hw = card;
931 
932 	priv->dev_state = DEVICE_STATE_PREBOOT;
933 	priv->net_dev = netdev;
934 	priv->firmware_version[0] = '\0';
935 	priv->version_size = 0;
936 	priv->last_doze = jiffies;
937 	priv->last_wakeup = jiffies;
938 	memset(&priv->nstats, 0, sizeof(priv->nstats));
939 	memset(&priv->wstats, 0, sizeof(priv->wstats));
940 
941 	/* sleep mode */
942 	atomic_set(&priv->sleepstatus.status, 0);
943 	atomic_set(&priv->sleepstatus.doze_request, 0);
944 	atomic_set(&priv->sleepstatus.wakeup_request, 0);
945 
946 	trx_device_init(priv);
947 	hostif_init(priv);
948 	ks_wlan_net_start(netdev);
949 	ks7010_init_defaults(priv);
950 }
951 
ks7010_sdio_probe(struct sdio_func * func,const struct sdio_device_id * device)952 static int ks7010_sdio_probe(struct sdio_func *func,
953 			     const struct sdio_device_id *device)
954 {
955 	struct ks_wlan_private *priv = NULL;
956 	struct net_device *netdev = NULL;
957 	struct ks_sdio_card *card;
958 	int ret;
959 
960 	card = kzalloc(sizeof(*card), GFP_KERNEL);
961 	if (!card)
962 		return -ENOMEM;
963 
964 	card->func = func;
965 
966 	sdio_claim_host(func);
967 
968 	ret = sdio_set_block_size(func, KS7010_IO_BLOCK_SIZE);
969 	if (ret)
970 		goto err_free_card;
971 
972 	dev_dbg(&card->func->dev, "multi_block=%d sdio_set_block_size()=%d %d\n",
973 		func->card->cccr.multi_block, func->cur_blksize, ret);
974 
975 	ret = sdio_enable_func(func);
976 	if (ret)
977 		goto err_free_card;
978 
979 	ret = ks7010_sdio_setup_irqs(func);
980 	if (ret)
981 		goto err_disable_func;
982 
983 	sdio_release_host(func);
984 
985 	sdio_set_drvdata(func, card);
986 
987 	dev_dbg(&card->func->dev, "class = 0x%X, vendor = 0x%X, device = 0x%X\n",
988 		func->class, func->vendor, func->device);
989 
990 	/* private memory allocate */
991 	netdev = alloc_etherdev(sizeof(*priv));
992 	if (!netdev) {
993 		dev_err(&card->func->dev, "Unable to alloc new net device\n");
994 		goto err_release_irq;
995 	}
996 
997 	ret = dev_alloc_name(netdev, "wlan%d");
998 	if (ret < 0) {
999 		dev_err(&card->func->dev, "Couldn't get name!\n");
1000 		goto err_free_netdev;
1001 	}
1002 
1003 	priv = netdev_priv(netdev);
1004 
1005 	card->priv = priv;
1006 	SET_NETDEV_DEV(netdev, &card->func->dev);
1007 
1008 	ks7010_private_init(priv, card, netdev);
1009 
1010 	ret = ks7010_upload_firmware(card);
1011 	if (ret) {
1012 		netdev_err(priv->net_dev,
1013 			   "firmware load failed !! ret = %d\n", ret);
1014 		goto err_free_netdev;
1015 	}
1016 
1017 	ks7010_sdio_init_irqs(func, priv);
1018 
1019 	priv->dev_state = DEVICE_STATE_BOOT;
1020 
1021 	priv->wq = alloc_workqueue("wq", WQ_MEM_RECLAIM, 1);
1022 	if (!priv->wq) {
1023 		netdev_err(priv->net_dev, "create_workqueue failed !!\n");
1024 		goto err_free_netdev;
1025 	}
1026 
1027 	INIT_DELAYED_WORK(&priv->rw_dwork, ks7010_rw_function);
1028 	ks7010_card_init(priv);
1029 
1030 	ret = register_netdev(priv->net_dev);
1031 	if (ret)
1032 		goto err_destroy_wq;
1033 
1034 	return 0;
1035 
1036  err_destroy_wq:
1037 	destroy_workqueue(priv->wq);
1038  err_free_netdev:
1039 	free_netdev(netdev);
1040  err_release_irq:
1041 	sdio_claim_host(func);
1042 	sdio_release_irq(func);
1043  err_disable_func:
1044 	sdio_disable_func(func);
1045  err_free_card:
1046 	sdio_release_host(func);
1047 	sdio_set_drvdata(func, NULL);
1048 	kfree(card);
1049 
1050 	return -ENODEV;
1051 }
1052 
1053 /* send stop request to MAC */
send_stop_request(struct sdio_func * func)1054 static int send_stop_request(struct sdio_func *func)
1055 {
1056 	struct hostif_stop_request *pp;
1057 	struct ks_sdio_card *card;
1058 	size_t size;
1059 
1060 	card = sdio_get_drvdata(func);
1061 
1062 	pp = kzalloc(hif_align_size(sizeof(*pp)), GFP_KERNEL);
1063 	if (!pp)
1064 		return -ENOMEM;
1065 
1066 	size = sizeof(*pp) - sizeof(pp->header.size);
1067 	pp->header.size = cpu_to_le16(size);
1068 	pp->header.event = cpu_to_le16(HIF_STOP_REQ);
1069 
1070 	sdio_claim_host(func);
1071 	write_to_device(card->priv, (u8 *)pp, hif_align_size(sizeof(*pp)));
1072 	sdio_release_host(func);
1073 
1074 	kfree(pp);
1075 	return 0;
1076 }
1077 
ks7010_sdio_remove(struct sdio_func * func)1078 static void ks7010_sdio_remove(struct sdio_func *func)
1079 {
1080 	int ret;
1081 	struct ks_sdio_card *card;
1082 	struct ks_wlan_private *priv;
1083 
1084 	card = sdio_get_drvdata(func);
1085 
1086 	if (!card)
1087 		return;
1088 
1089 	priv = card->priv;
1090 	if (!priv)
1091 		goto err_free_card;
1092 
1093 	ks_wlan_net_stop(priv->net_dev);
1094 
1095 	/* interrupt disable */
1096 	sdio_claim_host(func);
1097 	sdio_writeb(func, 0, INT_ENABLE_REG, &ret);
1098 	sdio_writeb(func, 0xff, INT_PENDING_REG, &ret);
1099 	sdio_release_host(func);
1100 
1101 	ret = send_stop_request(func);
1102 	if (ret)	/* memory allocation failure */
1103 		goto err_free_card;
1104 
1105 	if (priv->wq)
1106 		destroy_workqueue(priv->wq);
1107 
1108 	hostif_exit(priv);
1109 
1110 	unregister_netdev(priv->net_dev);
1111 
1112 	trx_device_exit(priv);
1113 	free_netdev(priv->net_dev);
1114 	card->priv = NULL;
1115 
1116 	sdio_claim_host(func);
1117 	sdio_release_irq(func);
1118 	sdio_disable_func(func);
1119 	sdio_release_host(func);
1120 err_free_card:
1121 	sdio_set_drvdata(func, NULL);
1122 	kfree(card);
1123 }
1124 
1125 static const struct sdio_device_id ks7010_sdio_ids[] = {
1126 	{SDIO_DEVICE(SDIO_VENDOR_ID_KS_CODE_A, SDIO_DEVICE_ID_KS_7010)},
1127 	{SDIO_DEVICE(SDIO_VENDOR_ID_KS_CODE_B, SDIO_DEVICE_ID_KS_7010)},
1128 	{ /* all zero */ }
1129 };
1130 MODULE_DEVICE_TABLE(sdio, ks7010_sdio_ids);
1131 
1132 static struct sdio_driver ks7010_sdio_driver = {
1133 	.name = "ks7010_sdio",
1134 	.id_table = ks7010_sdio_ids,
1135 	.probe = ks7010_sdio_probe,
1136 	.remove = ks7010_sdio_remove,
1137 };
1138 
1139 module_driver(ks7010_sdio_driver, sdio_register_driver, sdio_unregister_driver);
1140 MODULE_AUTHOR("Sang Engineering, Qi-Hardware, KeyStream");
1141 MODULE_DESCRIPTION("Driver for KeyStream KS7010 based SDIO cards");
1142 MODULE_LICENSE("GPL v2");
1143 MODULE_FIRMWARE(ROM_FILE);
1144