-
Notifications
You must be signed in to change notification settings - Fork 1
/
cw1200_spi.c
975 lines (790 loc) · 24.2 KB
/
cw1200_spi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/*
* Mac80211 SPI driver for ST-Ericsson CW1200 device
*
* Copyright (c) 2011, Sagrad Inc.
* Author: Solomon Peachy <speachy@sagrad.com>
*
* Based on cw1200_sdio.c
* Copyright (c) 2010, ST-Ericsson
* Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/suspend.h>
#include <net/mac80211.h>
#include <linux/spi/spi.h>
#include <linux/device.h>
#include "cw1200.h"
#include "hwbus.h"
#include <linux/cw1200_platform.h>
#include "hwio.h"
MODULE_AUTHOR("Solomon Peachy <speachy@sagrad.com>");
MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SPI driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("spi:cw1200_wlan_spi");
static struct workqueue_struct *cw1200_fwio_workqueue = NULL;
static int cw1200_fw_reset_cnt = 0;
/* #define SPI_DEBUG */
static struct spi_device *cw1200_spi_dev;
struct hwbus_priv {
struct spi_device *func;
struct cw1200_common *core;
const struct cw1200_platform_data_spi *pdata;
spinlock_t lock; /* Serialize all bus operations */
wait_queue_head_t wq;
int claimed;
// VLAD:
int manually_suspended;
atomic_t suspended;
struct work_struct cw1200_fw_failure_work;
struct notifier_block pm_notify;
struct mutex spi_pm_mutex;
};
static struct platform_device *cw1200_fwio_dev;
static void cw1200_fw_failure_job(struct work_struct *work);
int cw1200_pm_notify(struct notifier_block *notify_block,
unsigned long mode, void *unused)
{
struct hwbus_priv *self = container_of(notify_block, struct hwbus_priv, pm_notify);
switch (mode) {
case PM_SUSPEND_PREPARE:
{ int ret;
mutex_lock(&self->spi_pm_mutex);
if(self->manually_suspended || atomic_read(&self->suspended) ) {
dev_dbg(&cw1200_spi_dev->dev,"%s() already suspended \n",__func__);
mutex_unlock(&self->spi_pm_mutex);
return 0;
}
self->core->cw1200_fw_error_status = CW1200_FW_ERR_DOTERMINATE;
wake_up_interruptible(&self->core->cw1200_fw_wq);
ret = wait_event_interruptible(self->core->cw1200_fw_wq,CW1200_FW_ERR_TERMINATED == self->core->cw1200_fw_error_status);
self->core->cw1200_fw_error_status = CW1200_FW_ERR_IDLE;
mutex_unlock(&self->spi_pm_mutex);
} break;
case PM_POST_SUSPEND:
{
if(self->manually_suspended) {
dev_dbg(&cw1200_spi_dev->dev,"%s() already suspended \n",__func__);
return 0;
}
if(NULL == self->core) {
dev_err(&cw1200_spi_dev->dev,"%s(): self->core is NULL.\n",__func__);
return 0;
}
self->core->cw1200_fw_error_status = CW1200_FW_ERR_IDLE;
init_waitqueue_head(&self->core->cw1200_fw_wq);
INIT_WORK(&self->cw1200_fw_failure_work,cw1200_fw_failure_job);
queue_work(cw1200_fwio_workqueue,&self->cw1200_fw_failure_work);
} break;
}
return 0;
}
static int cw1200_fwio_prepare(struct device *dev)
{
struct hwbus_priv *self = spi_get_drvdata(cw1200_spi_dev);
if(self->manually_suspended || atomic_read(&self->suspended) ) {
dev_dbg(&cw1200_spi_dev->dev,"%s() already suspended \n",__func__);
return 0;
}
dev_dbg(&cw1200_spi_dev->dev,"%s() \n",__func__);
self->core->cw1200_fw_error_status = CW1200_FW_ERR_DOTERMINATE;
wake_up_interruptible(&self->core->cw1200_fw_wq);
wait_event_interruptible(self->core->cw1200_fw_wq,CW1200_FW_ERR_TERMINATED == self->core->cw1200_fw_error_status);
self->core->cw1200_fw_error_status = CW1200_FW_ERR_IDLE;
return 0;
}
static void cw1200_fwio_complete(struct device *dev)
{
struct hwbus_priv *self = spi_get_drvdata(cw1200_spi_dev);
if(self->manually_suspended) {
dev_dbg(&cw1200_spi_dev->dev,"%s() already suspended \n",__func__);
return;
}
dev_dbg(&cw1200_spi_dev->dev,"%s() \n",__func__);
if(NULL == self->core) {
dev_err(&cw1200_spi_dev->dev,"%s(): self->core is NULL.\n",__func__);
return;
}
self->core->cw1200_fw_error_status = CW1200_FW_ERR_IDLE;
init_waitqueue_head(&self->core->cw1200_fw_wq);
queue_work(cw1200_fwio_workqueue,&self->cw1200_fw_failure_work);
}
static struct class *cw1200_class;
static int cw1200_fwio_probe(struct platform_device *pdev)
{
cw1200_class = class_create(THIS_MODULE, "cw1200_fw_io");
if (IS_ERR(cw1200_class)) {
printk(KERN_ERR"========[ failed to create cw1200_fw_io class ]=========\n");
}
device_create(cw1200_class,&pdev->dev,0,NULL,"cw1200_fw_io");
return 0;
}
static int cw1200_fwio_remove(struct platform_device *pdev)
{
device_destroy(cw1200_class,0);
class_destroy(cw1200_class);
return 0;
}
static struct platform_driver cw1200_fwio_driver = {
.probe = cw1200_fwio_probe,
.remove = cw1200_fwio_remove,
.driver = {
.name = "cw1200_fw_io",
},
};
#define SDIO_TO_SPI_ADDR(addr) ((addr & 0x1f)>>2)
#define SET_WRITE 0x7FFF /* usage: and operation */
#define SET_READ 0x8000 /* usage: or operation */
/* Notes on byte ordering:
LE: B0 B1 B2 B3
BE: B3 B2 B1 B0
Hardware expects 32-bit data to be written as 16-bit BE words:
B1 B0 B3 B2
*/
static int cw1200_spi_memcpy_fromio(struct hwbus_priv *self,
unsigned int addr,
void *dst, int count)
{
int ret, i;
u16 regaddr;
struct spi_message m;
struct spi_transfer t_addr = {
.tx_buf = ®addr,
.len = sizeof(regaddr),
};
struct spi_transfer t_msg = {
.rx_buf = dst,
.len = count,
};
regaddr = (SDIO_TO_SPI_ADDR(addr))<<12;
regaddr |= SET_READ;
regaddr |= (count>>1);
#ifdef SPI_DEBUG
pr_info("READ : %04d from 0x%02x (%04x)\n", count, addr, regaddr);
#endif
/* Header is LE16 */
regaddr = cpu_to_le16(regaddr);
/* We have to byteswap if the SPI bus is limited to 8b operation
or we are running on a Big Endian system
*/
#if defined(__LITTLE_ENDIAN)
if (self->func->bits_per_word == 8)
#endif
regaddr = swab16(regaddr);
spi_message_init(&m);
spi_message_add_tail(&t_addr, &m);
spi_message_add_tail(&t_msg, &m);
ret = spi_sync(self->func, &m);
#ifdef SPI_DEBUG
pr_info("READ : ");
for (i = 0; i < t_addr.len; i++)
printk("%02x ", ((u8 *)t_addr.tx_buf)[i]);
printk(" : ");
for (i = 0; i < t_msg.len; i++)
printk("%02x ", ((u8 *)t_msg.rx_buf)[i]);
printk("\n");
#endif
/* We have to byteswap if the SPI bus is limited to 8b operation
or we are running on a Big Endian system
*/
#if defined(__LITTLE_ENDIAN)
if (self->func->bits_per_word == 8)
#endif
{
uint16_t *buf = (uint16_t *)dst;
for (i = 0; i < ((count + 1) >> 1); i++)
buf[i] = swab16(buf[i]);
}
return ret;
}
static int cw1200_spi_memcpy_toio(struct hwbus_priv *self,
unsigned int addr,
const void *src, int count)
{
int rval, i;
u16 regaddr;
struct spi_transfer t_addr = {
.tx_buf = ®addr,
.len = sizeof(regaddr),
};
struct spi_transfer t_msg = {
.tx_buf = src,
.len = count,
};
struct spi_message m;
regaddr = (SDIO_TO_SPI_ADDR(addr))<<12;
regaddr &= SET_WRITE;
regaddr |= (count>>1);
#ifdef SPI_DEBUG
pr_info("WRITE: %04d to 0x%02x (%04x)\n", count, addr, regaddr);
#endif
/* Header is LE16 */
regaddr = cpu_to_le16(regaddr);
/* We have to byteswap if the SPI bus is limited to 8b operation
or we are running on a Big Endian system
*/
#if defined(__LITTLE_ENDIAN)
if (self->func->bits_per_word == 8)
#endif
{
uint16_t *buf = (uint16_t *)src;
regaddr = swab16(regaddr);
for (i = 0; i < ((count + 1) >> 1); i++)
buf[i] = swab16(buf[i]);
}
#ifdef SPI_DEBUG
pr_info("WRITE: ");
for (i = 0; i < t_addr.len; i++)
printk("%02x ", ((u8 *)t_addr.tx_buf)[i]);
printk(" : ");
for (i = 0; i < t_msg.len; i++)
printk("%02x ", ((u8 *)t_msg.tx_buf)[i]);
printk("\n");
#endif
spi_message_init(&m);
spi_message_add_tail(&t_addr, &m);
spi_message_add_tail(&t_msg, &m);
rval = spi_sync(self->func, &m);
#ifdef SPI_DEBUG
pr_info("WROTE: %d\n", m.actual_length);
#endif
#if defined(__LITTLE_ENDIAN)
/* We have to byteswap if the SPI bus is limited to 8b operation */
if (self->func->bits_per_word == 8)
#endif
{
uint16_t *buf = (uint16_t *)src;
for (i = 0; i < ((count + 1) >> 1); i++)
buf[i] = swab16(buf[i]);
}
return rval;
}
static void cw1200_spi_lock(struct hwbus_priv *self)
{
unsigned long flags;
DECLARE_WAITQUEUE(wait, current);
might_sleep();
add_wait_queue(&self->wq, &wait);
spin_lock_irqsave(&self->lock, flags);
while (1) {
set_current_state(TASK_UNINTERRUPTIBLE);
if (!self->claimed)
break;
spin_unlock_irqrestore(&self->lock, flags);
schedule();
spin_lock_irqsave(&self->lock, flags);
}
set_current_state(TASK_RUNNING);
self->claimed = 1;
spin_unlock_irqrestore(&self->lock, flags);
remove_wait_queue(&self->wq, &wait);
return;
}
static void cw1200_spi_unlock(struct hwbus_priv *self)
{
unsigned long flags;
spin_lock_irqsave(&self->lock, flags);
self->claimed = 0;
spin_unlock_irqrestore(&self->lock, flags);
wake_up(&self->wq);
return;
}
static irqreturn_t cw1200_spi_irq_handler(int irq, void *dev_id)
{
struct hwbus_priv *self = dev_id;
if (self->core) {
cw1200_spi_lock(self);
cw1200_irq_handler(self->core);
cw1200_spi_unlock(self);
return IRQ_HANDLED;
} else {
return IRQ_NONE;
}
}
static int cw1200_spi_irq_subscribe(struct hwbus_priv *self)
{
int ret;
pr_debug("SW IRQ subscribe\n");
ret = request_threaded_irq(self->func->irq, NULL,
cw1200_spi_irq_handler,
IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
"cw1200_wlan_irq", self);
if (WARN_ON(ret < 0))
goto exit;
ret = enable_irq_wake(self->func->irq);
if (WARN_ON(ret))
goto free_irq;
return 0;
free_irq:
free_irq(self->func->irq, self);
exit:
return ret;
}
static int cw1200_spi_irq_unsubscribe(struct hwbus_priv *self)
{
int ret = 0;
pr_debug("SW IRQ unsubscribe\n");
disable_irq_wake(self->func->irq);
free_irq(self->func->irq, self);
return ret;
}
static int cw1200_spi_off(const struct cw1200_platform_data_spi *pdata)
{
const struct resource *reset = pdata->reset;
const struct resource *powerup = pdata->powerup;
if (reset) {
gpio_set_value(reset->start, 0);
msleep(30); /* Min is 2 * CLK32K cycles */
gpio_free(reset->start);
}
if (powerup) {
gpio_direction_output(powerup->start, 0);
gpio_free(powerup->start);
}
if (pdata->power_ctrl)
pdata->power_ctrl(pdata, false);
if (pdata->clk_ctrl)
pdata->clk_ctrl(pdata, false);
return 0;
}
static int cw1200_spi_on(const struct cw1200_platform_data_spi *pdata)
{
const struct resource *reset = pdata->reset;
const struct resource *powerup = pdata->powerup;
/* Ensure I/Os are pulled low */
if (reset) {
int res;
if( (res = gpio_request(reset->start, reset->name) < 0) )
printk(KERN_CRIT"VLAD: %s request gpio %s failed: %d \n",__func__,reset->name,res);
gpio_direction_output(reset->start, 0);
}
if (powerup) {
int res;
if( ( res = gpio_request(powerup->start, powerup->name)) < 0)
printk(KERN_CRIT"VLAD: %s request gpio %s failed: %d\n",__func__,powerup->name,res);
gpio_direction_output(powerup->start, 0);
}
if (reset || powerup)
msleep(10); /* Settle time? */
/* Enable 3v3 and 1v8 to hardware */
if (pdata->power_ctrl) {
if (pdata->power_ctrl(pdata, true)) {
pr_err("power_ctrl() failed!\n");
return -1;
}
}
/* Enable CLK32K */
if (pdata->clk_ctrl) {
if (pdata->clk_ctrl(pdata, true)) {
pr_err("clk_ctrl() failed!\n");
return -1;
}
msleep(10); /* Delay until clock is stable for 2 cycles */
}
/* Enable POWERUP signal */
if (powerup) {
gpio_set_value(powerup->start, 1);
msleep(250); /* or more..? */
}
/* Enable RSTn signal */
if (reset) {
gpio_set_value(reset->start, 1);
msleep(50); /* Or more..? */
}
return 0;
}
static size_t cw1200_spi_align_size(struct hwbus_priv *self, size_t size)
{
return size & 1 ? size + 1 : size;
}
static int cw1200_spi_pm(struct hwbus_priv *self, bool suspend)
{
return irq_set_irq_wake(self->func->irq, suspend);
}
static struct hwbus_ops cw1200_spi_hwbus_ops = {
.hwbus_memcpy_fromio = cw1200_spi_memcpy_fromio,
.hwbus_memcpy_toio = cw1200_spi_memcpy_toio,
.lock = cw1200_spi_lock,
.unlock = cw1200_spi_unlock,
.align_size = cw1200_spi_align_size,
.power_mgmt = cw1200_spi_pm,
};
static int cw1200_spi_suspend(struct spi_device *spi_dev, pm_message_t state)
{
struct hwbus_priv *self = spi_get_drvdata(spi_dev);
const struct cw1200_platform_data_spi *pdata = cw1200_get_platform_data();
const struct resource *reset = pdata->reset;
const struct resource *powerup = pdata->powerup;
if(self->manually_suspended || atomic_cmpxchg(&self->suspended,0,1)) {
dev_dbg(&cw1200_spi_dev->dev,"%s() already suspended \n",__func__);
return 0;
}
dev_dbg(&cw1200_spi_dev->dev,"%s(%.4X) \n",__func__,state.event);
if(self) {
if(self->core) {
cw1200_spi_irq_unsubscribe(self);
cw1200_core_release(self->core);
self->core = NULL;
}
}
if (reset) {
gpio_set_value(reset->start, 0);
}
if (powerup) {
gpio_set_value(powerup->start, 0);
}
return 0;
}
static int cw1200_spi_resume(struct spi_device *spi_dev)
{
struct hwbus_priv *self = spi_get_drvdata(spi_dev);
const struct cw1200_platform_data_spi *pdata = cw1200_get_platform_data();
const struct resource *reset = pdata->reset;
const struct resource *powerup = pdata->powerup;
if(self->manually_suspended || !atomic_cmpxchg(&self->suspended,1,0)) {
dev_dbg(&cw1200_spi_dev->dev,"%s() skipped resume \n",__func__);
return 0;
}
if (reset || powerup)
msleep(10); /* Settle time? */
/* Enable POWERUP signal */
if (powerup) {
gpio_set_value(powerup->start, 1);
msleep(250); /* or more..? */
}
/* Enable RSTn signal */
if (reset) {
gpio_set_value(reset->start, 1);
msleep(50); /* Or more..? */
}
dev_dbg(&cw1200_spi_dev->dev,"%s() \n",__func__);
cw1200_spi_irq_subscribe(self);
cw1200_core_probe(&cw1200_spi_hwbus_ops,
self, &self->func->dev, &self->core,
self->pdata->ref_clk,
self->pdata->macaddr,
self->pdata->sdd_file,
self->pdata->have_5ghz);
return 0;
}
static void cw1200_fw_failure_job(struct work_struct *work)
{
int status;
pm_message_t evt;
struct hwbus_priv *self = container_of(work, struct hwbus_priv, cw1200_fw_failure_work);
struct cw1200_common *priv;
priv = self->core;
evt.event = 0;
status = wait_event_interruptible(priv->cw1200_fw_wq,CW1200_FW_ERR_IDLE != priv->cw1200_fw_error_status);
if(status < 0 ) {
dev_err(&cw1200_spi_dev->dev,"%s failed to wait for fw failure %d",__func__,status);
} else if (CW1200_FW_ERR_DOALARM == priv->cw1200_fw_error_status) {
if(cw1200_fwio_dev) { /* sending mdev event to initiate user-space driven wifi reset sequence */
dev_info(&cw1200_spi_dev->dev,"cw1200 firmware exception detected, preparing to reset\n");
platform_driver_unregister(&cw1200_fwio_driver);
cw1200_fwio_dev->dev.platform_data = NULL;
platform_device_unregister(cw1200_fwio_dev);
cw1200_fwio_dev = NULL;
status = wait_event_interruptible_timeout(priv->cw1200_fw_wq,priv->cw1200_fw_error_status > CW1200_FW_ERR_DOALARM,HZ*60*5);
if(status < 0 ) {
dev_err(&cw1200_spi_dev->dev,"%s failed to wait for fw reset command %d",__func__,status);
goto terminate;
} else if(0 == status) { /* timeout*/
dev_err(&cw1200_spi_dev->dev,"cw1200 reset fw command timeout\n");
goto terminate;
} else if( CW1200_FW_ERR_DORESET == priv->cw1200_fw_error_status) {
dev_info(&cw1200_spi_dev->dev,"executing cw1200 firmware reset\n");
cw1200_fw_reset_cnt++;
mutex_lock(&self->spi_pm_mutex);
cw1200_spi_suspend(cw1200_spi_dev,evt);
msleep_interruptible(200);
cw1200_spi_resume(cw1200_spi_dev);
self->core->cw1200_fw_error_status = CW1200_FW_ERR_IDLE;
init_waitqueue_head(&self->core->cw1200_fw_wq);
mutex_unlock(&self->spi_pm_mutex);
} else if (CW1200_FW_ERR_DOTERMINATE == priv->cw1200_fw_error_status) {
goto terminate;
} else goto oops;
}
} else if (CW1200_FW_ERR_DORESET == priv->cw1200_fw_error_status) {
dev_err(&cw1200_spi_dev->dev,"RESET not allowed\n");
self->core->cw1200_fw_error_status = CW1200_FW_ERR_IDLE;
} else if (CW1200_FW_ERR_DOTERMINATE == priv->cw1200_fw_error_status) {
goto terminate;
} else goto oops;
if(!cw1200_fwio_dev) {
int ret;
ret = platform_driver_register(&cw1200_fwio_driver);
cw1200_fwio_dev = platform_device_alloc("cw1200_fw_io", 0);
ret = platform_device_add(cw1200_fwio_dev);
if (ret) {
kfree(cw1200_fwio_dev);
}
}
queue_work(cw1200_fwio_workqueue,&self->cw1200_fw_failure_work);
return;
oops:
dev_err(&cw1200_spi_dev->dev,"%s() unexpected event: %d\n",__func__,priv->cw1200_fw_error_status);
terminate:
dev_info(&cw1200_spi_dev->dev,"%s() termination \n",__func__);
priv->cw1200_fw_error_status = CW1200_FW_ERR_TERMINATED;
wake_up_interruptible(&priv->cw1200_fw_wq);
}
static ssize_t dev_type_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
return sprintf(buf,"%d\n",cw1200_fw_reset_cnt);
}
static ssize_t cw1200_do_reset(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
pm_message_t evt;
struct hwbus_priv *self = NULL;
self = spi_get_drvdata(cw1200_spi_dev);
evt.event = 0;
if(!strcmp(buf,"RESET\n")) {
dev_info(&cw1200_spi_dev->dev,"%s() RESET received \n",__func__);
if(self && self->core) {
if(CW1200_FW_ERR_DOALARM == self->core->cw1200_fw_error_status ) {
dev_info(&cw1200_spi_dev->dev,"%s() waking up the terminator \n",__func__);
self->core->cw1200_fw_error_status = CW1200_FW_ERR_DORESET;
wake_up_interruptible(&self->core->cw1200_fw_wq);
} else {
dev_info(&cw1200_spi_dev->dev,"%s() RESET not permitted at state: %d \n",__func__,self->core->cw1200_fw_error_status );
}
} else {
dev_err(&cw1200_spi_dev->dev,"%s self->core == NULL\n",__func__);
}
} else if(!strcmp(buf,"SUSPEND\n")) {
dev_dbg(&cw1200_spi_dev->dev,"%s() SUSPEND received \n",__func__);
if( 0 == self->manually_suspended ) {
cw1200_fwio_prepare(NULL);
cw1200_spi_suspend(cw1200_spi_dev,evt);
self->manually_suspended = 1;
}
} else if(!strcmp(buf,"RESUME\n")) {
dev_dbg(&cw1200_spi_dev->dev,"%s() RESUME received \n",__func__);
if(self->manually_suspended) {
self->manually_suspended = 0;
cw1200_spi_resume(cw1200_spi_dev);
cw1200_fwio_complete(NULL);
}
} else {
return -EACCES;
}
return size;
}
DEVICE_ATTR(cw1200_fw_ok, S_IRUGO, dev_type_show, NULL);
DEVICE_ATTR(cw1200_fw_reset, S_IWUGO, NULL, cw1200_do_reset);
/* Probe Function to be called by SPI stack when device is discovered */
static int cw1200_spi_probe(struct spi_device *func)
{
const struct cw1200_platform_data_spi *plat_data = cw1200_get_platform_data();
struct hwbus_priv *self;
int status;
cw1200_fwio_workqueue = alloc_workqueue("cw1200_fwio_q",WQ_RESCUER , 1);
if( NULL == cw1200_fwio_workqueue) {
dev_err(&func->dev,"cw1200_fwio_workqueue == NULL\n");
return -EFAULT;
}
/* Sanity check speed */
if (func->max_speed_hz > 52000000)
func->max_speed_hz = 52000000;
if (func->max_speed_hz < 1000000)
func->max_speed_hz = 1000000;
/* Fix up transfer size */
if (plat_data->spi_bits_per_word)
func->bits_per_word = plat_data->spi_bits_per_word;
if (!func->bits_per_word)
func->bits_per_word = 16;
/* And finally.. */
func->mode = SPI_MODE_0;
pr_info("cw1200_wlan_spi: Probe called (CS %d M %d BPW %d CLK %d)\n",
func->chip_select, func->mode, func->bits_per_word,
func->max_speed_hz);
if (cw1200_spi_on(plat_data)) {
pr_err("spi_on() failed!\n");
return -1;
}
if (spi_setup(func)) {
pr_err("spi_setup() failed!\n");
return -1;
}
self = kzalloc(sizeof(*self), GFP_KERNEL);
if (!self) {
pr_err("Can't allocate SPI hwbus_priv.");
return -ENOMEM;
}
self->manually_suspended = 0;
atomic_set(&self->suspended,0);
self->pdata = plat_data;
self->func = func;
spin_lock_init(&self->lock);
spi_set_drvdata(func, self);
init_waitqueue_head(&self->wq);
mutex_init(&self->spi_pm_mutex);
status = cw1200_spi_irq_subscribe(self);
status = cw1200_core_probe(&cw1200_spi_hwbus_ops,
self, &func->dev, &self->core,
self->pdata->ref_clk,
self->pdata->macaddr,
self->pdata->sdd_file,
self->pdata->have_5ghz);
if( 0 == status) {
self->core->cw1200_fw_error_status = CW1200_FW_ERR_IDLE;
init_waitqueue_head(&self->core->cw1200_fw_wq);
INIT_WORK(&self->cw1200_fw_failure_work, cw1200_fw_failure_job);
queue_work(cw1200_fwio_workqueue,&self->cw1200_fw_failure_work);
}
if (status) {
cw1200_spi_irq_unsubscribe(self);
cw1200_spi_off(plat_data);
kfree(self);
}
cw1200_spi_dev = func;
self->pm_notify.notifier_call = cw1200_pm_notify;
register_pm_notifier(&self->pm_notify);
status = device_create_file(&func->dev,&dev_attr_cw1200_fw_ok);
if(status) dev_err(&func->dev, "dev_attr_dev_type %d", status);
status = device_create_file(&func->dev,&dev_attr_cw1200_fw_reset);
if(status) dev_err(&func->dev, "dev_attr_dev_type %d", status);
#if defined(CONFIG_DEBUG_FS)
{
void cw1200_create_debugfs(void);
cw1200_create_debugfs();
}
#endif
return status;
}
/* Disconnect Function to be called by SPI stack when device is disconnected */
static int cw1200_spi_disconnect(struct spi_device *func)
{
struct hwbus_priv *self = spi_get_drvdata(func);
unregister_pm_notifier(&self->pm_notify);
if (self) {
cw1200_spi_irq_unsubscribe(self);
if (self->core) {
cw1200_core_release(self->core);
self->core = NULL;
}
kfree(self);
}
cw1200_spi_off(func->dev.platform_data);
destroy_workqueue(cw1200_fwio_workqueue);
mutex_destroy(&self->spi_pm_mutex);
return 0;
}
static struct spi_driver spi_driver = {
.probe = cw1200_spi_probe,
.remove = cw1200_spi_disconnect,
.suspend = cw1200_spi_suspend,
.resume = cw1200_spi_resume,
.driver = {
.name = "cw1200_wlan_spi",
.bus = &spi_bus_type,
.owner = THIS_MODULE,
},
};
/* Init Module function -> Called by insmod */
static int __init cw1200_spi_init(void)
{
int ret;
ret = spi_register_driver(&spi_driver);
if (!ret) {
ret = platform_driver_register(&cw1200_fwio_driver);
if (ret)
return ret;
cw1200_fwio_dev = platform_device_alloc("cw1200_fw_io", 0);
if (!cw1200_fwio_dev) {
platform_driver_unregister(&cw1200_fwio_driver);
return -ENOMEM;
}
ret = platform_device_add(cw1200_fwio_dev);
if (ret) {
kfree(cw1200_fwio_dev);
}
}
return ret;
}
/* Called at Driver Unloading */
static void __exit cw1200_spi_exit(void)
{
spi_unregister_driver(&spi_driver);
}
#ifdef CONFIG_DEBUG_FS
static int debugfs_cmd;
static int cw1200_debugfs_set_cmd(void *data, u64 val)
{
debugfs_cmd = (int)val;
printk(KERN_CRIT"VLAD: %s(%d)\n",__func__,debugfs_cmd);
switch(debugfs_cmd) {
case 1: {
pm_message_t evt;
evt.event = 0;
cw1200_spi_suspend(cw1200_spi_dev,evt);
msleep_interruptible(100);
cw1200_spi_resume(cw1200_spi_dev);
} break;
case 2: {
if(cw1200_fwio_dev) {
platform_driver_unregister(&cw1200_fwio_driver);
cw1200_fwio_dev->dev.platform_data = NULL;
platform_device_unregister(cw1200_fwio_dev);
cw1200_fwio_dev = NULL;
}
} break;
case 3: {
if(!cw1200_fwio_dev) {
int ret;
ret = platform_driver_register(&cw1200_fwio_driver);
cw1200_fwio_dev = platform_device_alloc("cw1200_fw_io", 0);
ret = platform_device_add(cw1200_fwio_dev);
if (ret) {
kfree(cw1200_fwio_dev);
}
}
} break;
case 4: {
struct hwbus_priv *self = NULL;
self = spi_get_drvdata(cw1200_spi_dev);
self->core->cw1200_fw_error_status = CW1200_FW_ERR_DOALARM;
wake_up_interruptible(&self->core->cw1200_fw_wq);
} break;
case 5: {
struct hwbus_priv *self = NULL;
self = spi_get_drvdata(cw1200_spi_dev);
self->core->bh_error = 1;
barrier();
wake_up(&self->core->bh_wq);
}
}
return 0;
}
static int cw1200_debugfs_get_cmd(void *data, u64* val)
{
*val = (u64)debugfs_cmd;
return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(fops_cw1200_debugfs, cw1200_debugfs_get_cmd,cw1200_debugfs_set_cmd, "%08lld\n");
void cw1200_create_debugfs(void)
{
struct dentry *regs;
static const mode_t dm = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
regs = debugfs_create_dir("cw1200_spi", NULL);
if(regs) {
debugfs_create_file("debug_cmd", dm, regs,(void*)&debugfs_cmd, &fops_cw1200_debugfs);
}
}
#endif
module_init(cw1200_spi_init);
module_exit(cw1200_spi_exit);