This source file includes following definitions.
- ishtp_recv
- ishtp_send_msg
- ishtp_write_message
- ishtp_fw_cl_by_uuid
- ishtp_fw_cl_get_client
- ishtp_get_fw_client_id
- ishtp_fw_cl_by_id
- ishtp_cl_device_probe
- ishtp_cl_bus_match
- ishtp_cl_device_remove
- ishtp_cl_device_suspend
- ishtp_cl_device_resume
- ishtp_cl_device_reset
- modalias_show
- ishtp_cl_uevent
- ishtp_cl_dev_release
- ishtp_bus_add_device
- ishtp_bus_remove_device
- ishtp_cl_driver_register
- ishtp_cl_driver_unregister
- ishtp_bus_event_work
- ishtp_cl_bus_rx_event
- ishtp_register_event_cb
- ishtp_get_device
- ishtp_put_device
- ishtp_set_drvdata
- ishtp_get_drvdata
- ishtp_dev_to_cl_device
- ishtp_bus_new_client
- ishtp_cl_device_bind
- ishtp_bus_remove_all_clients
- ishtp_reset_handler
- ishtp_reset_compl_handler
- ishtp_use_dma_transfer
- ishtp_device
- ishtp_get_pci_device
- ishtp_trace_callback
- ish_hw_reset
- ishtp_bus_register
- ishtp_bus_unregister
1
2
3
4
5
6
7
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include "bus.h"
15 #include "ishtp-dev.h"
16 #include "client.h"
17 #include "hbm.h"
18
19 static int ishtp_use_dma;
20 module_param_named(ishtp_use_dma, ishtp_use_dma, int, 0600);
21 MODULE_PARM_DESC(ishtp_use_dma, "Use DMA to send messages");
22
23 #define to_ishtp_cl_driver(d) container_of(d, struct ishtp_cl_driver, driver)
24 #define to_ishtp_cl_device(d) container_of(d, struct ishtp_cl_device, dev)
25 static bool ishtp_device_ready;
26
27
28
29
30
31
32
33
34
35
36 void ishtp_recv(struct ishtp_device *dev)
37 {
38 uint32_t msg_hdr;
39 struct ishtp_msg_hdr *ishtp_hdr;
40
41
42 msg_hdr = dev->ops->ishtp_read_hdr(dev);
43 if (!msg_hdr)
44 return;
45
46 dev->ops->sync_fw_clock(dev);
47
48 ishtp_hdr = (struct ishtp_msg_hdr *)&msg_hdr;
49 dev->ishtp_msg_hdr = msg_hdr;
50
51
52 if (ishtp_hdr->length > dev->mtu) {
53 dev_err(dev->devc,
54 "ISHTP hdr - bad length: %u; dropped [%08X]\n",
55 (unsigned int)ishtp_hdr->length, msg_hdr);
56 return;
57 }
58
59
60 if (!ishtp_hdr->host_addr && !ishtp_hdr->fw_addr)
61 recv_hbm(dev, ishtp_hdr);
62
63 else if (!ishtp_hdr->host_addr)
64 recv_fixed_cl_msg(dev, ishtp_hdr);
65 else
66
67 recv_ishtp_cl_msg(dev, ishtp_hdr);
68 }
69 EXPORT_SYMBOL(ishtp_recv);
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 int ishtp_send_msg(struct ishtp_device *dev, struct ishtp_msg_hdr *hdr,
85 void *msg, void(*ipc_send_compl)(void *),
86 void *ipc_send_compl_prm)
87 {
88 unsigned char ipc_msg[IPC_FULL_MSG_SIZE];
89 uint32_t drbl_val;
90
91 drbl_val = dev->ops->ipc_get_header(dev, hdr->length +
92 sizeof(struct ishtp_msg_hdr),
93 1);
94
95 memcpy(ipc_msg, &drbl_val, sizeof(uint32_t));
96 memcpy(ipc_msg + sizeof(uint32_t), hdr, sizeof(uint32_t));
97 memcpy(ipc_msg + 2 * sizeof(uint32_t), msg, hdr->length);
98 return dev->ops->write(dev, ipc_send_compl, ipc_send_compl_prm,
99 ipc_msg, 2 * sizeof(uint32_t) + hdr->length);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113 int ishtp_write_message(struct ishtp_device *dev, struct ishtp_msg_hdr *hdr,
114 void *buf)
115 {
116 return ishtp_send_msg(dev, hdr, buf, NULL, NULL);
117 }
118
119
120
121
122
123
124
125
126
127
128 int ishtp_fw_cl_by_uuid(struct ishtp_device *dev, const guid_t *uuid)
129 {
130 unsigned int i;
131
132 for (i = 0; i < dev->fw_clients_num; ++i) {
133 if (guid_equal(uuid, &dev->fw_clients[i].props.protocol_name))
134 return i;
135 }
136 return -ENOENT;
137 }
138 EXPORT_SYMBOL(ishtp_fw_cl_by_uuid);
139
140
141
142
143
144
145
146
147
148
149 struct ishtp_fw_client *ishtp_fw_cl_get_client(struct ishtp_device *dev,
150 const guid_t *uuid)
151 {
152 int i;
153 unsigned long flags;
154
155 spin_lock_irqsave(&dev->fw_clients_lock, flags);
156 i = ishtp_fw_cl_by_uuid(dev, uuid);
157 spin_unlock_irqrestore(&dev->fw_clients_lock, flags);
158 if (i < 0 || dev->fw_clients[i].props.fixed_address)
159 return NULL;
160
161 return &dev->fw_clients[i];
162 }
163 EXPORT_SYMBOL(ishtp_fw_cl_get_client);
164
165
166
167
168
169
170
171
172 int ishtp_get_fw_client_id(struct ishtp_fw_client *fw_client)
173 {
174 return fw_client->client_id;
175 }
176 EXPORT_SYMBOL(ishtp_get_fw_client_id);
177
178
179
180
181
182
183
184
185
186
187 int ishtp_fw_cl_by_id(struct ishtp_device *dev, uint8_t client_id)
188 {
189 int i, res = -ENOENT;
190 unsigned long flags;
191
192 spin_lock_irqsave(&dev->fw_clients_lock, flags);
193 for (i = 0; i < dev->fw_clients_num; i++) {
194 if (dev->fw_clients[i].client_id == client_id) {
195 res = i;
196 break;
197 }
198 }
199 spin_unlock_irqrestore(&dev->fw_clients_lock, flags);
200
201 return res;
202 }
203
204
205
206
207
208
209
210
211
212 static int ishtp_cl_device_probe(struct device *dev)
213 {
214 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
215 struct ishtp_cl_driver *driver;
216
217 if (!device)
218 return 0;
219
220 driver = to_ishtp_cl_driver(dev->driver);
221 if (!driver || !driver->probe)
222 return -ENODEV;
223
224 return driver->probe(device);
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238 static int ishtp_cl_bus_match(struct device *dev, struct device_driver *drv)
239 {
240 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
241 struct ishtp_cl_driver *driver = to_ishtp_cl_driver(drv);
242
243 return guid_equal(driver->guid,
244 &device->fw_client->props.protocol_name);
245 }
246
247
248
249
250
251
252
253
254
255
256
257 static int ishtp_cl_device_remove(struct device *dev)
258 {
259 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
260 struct ishtp_cl_driver *driver;
261
262 if (!device || !dev->driver)
263 return 0;
264
265 if (device->event_cb) {
266 device->event_cb = NULL;
267 cancel_work_sync(&device->event_work);
268 }
269
270 driver = to_ishtp_cl_driver(dev->driver);
271 if (!driver->remove) {
272 dev->driver = NULL;
273
274 return 0;
275 }
276
277 return driver->remove(device);
278 }
279
280
281
282
283
284
285
286
287
288 static int ishtp_cl_device_suspend(struct device *dev)
289 {
290 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
291 struct ishtp_cl_driver *driver;
292 int ret = 0;
293
294 if (!device)
295 return 0;
296
297 driver = to_ishtp_cl_driver(dev->driver);
298 if (driver && driver->driver.pm) {
299 if (driver->driver.pm->suspend)
300 ret = driver->driver.pm->suspend(dev);
301 }
302
303 return ret;
304 }
305
306
307
308
309
310
311
312
313
314 static int ishtp_cl_device_resume(struct device *dev)
315 {
316 struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
317 struct ishtp_cl_driver *driver;
318 int ret = 0;
319
320 if (!device)
321 return 0;
322
323
324
325
326
327 if (device->ishtp_dev->resume_flag)
328 return 0;
329
330 driver = to_ishtp_cl_driver(dev->driver);
331 if (driver && driver->driver.pm) {
332 if (driver->driver.pm->resume)
333 ret = driver->driver.pm->resume(dev);
334 }
335
336 return ret;
337 }
338
339
340
341
342
343
344
345
346
347
348 static int ishtp_cl_device_reset(struct ishtp_cl_device *device)
349 {
350 struct ishtp_cl_driver *driver;
351 int ret = 0;
352
353 device->event_cb = NULL;
354 cancel_work_sync(&device->event_work);
355
356 driver = to_ishtp_cl_driver(device->dev.driver);
357 if (driver && driver->reset)
358 ret = driver->reset(device);
359
360 return ret;
361 }
362
363 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
364 char *buf)
365 {
366 int len;
367
368 len = snprintf(buf, PAGE_SIZE, "ishtp:%s\n", dev_name(dev));
369 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
370 }
371 static DEVICE_ATTR_RO(modalias);
372
373 static struct attribute *ishtp_cl_dev_attrs[] = {
374 &dev_attr_modalias.attr,
375 NULL,
376 };
377 ATTRIBUTE_GROUPS(ishtp_cl_dev);
378
379 static int ishtp_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
380 {
381 if (add_uevent_var(env, "MODALIAS=ishtp:%s", dev_name(dev)))
382 return -ENOMEM;
383 return 0;
384 }
385
386 static const struct dev_pm_ops ishtp_cl_bus_dev_pm_ops = {
387
388 .suspend = ishtp_cl_device_suspend,
389 .resume = ishtp_cl_device_resume,
390
391 .freeze = ishtp_cl_device_suspend,
392 .thaw = ishtp_cl_device_resume,
393 .restore = ishtp_cl_device_resume,
394 };
395
396 static struct bus_type ishtp_cl_bus_type = {
397 .name = "ishtp",
398 .dev_groups = ishtp_cl_dev_groups,
399 .probe = ishtp_cl_device_probe,
400 .match = ishtp_cl_bus_match,
401 .remove = ishtp_cl_device_remove,
402 .pm = &ishtp_cl_bus_dev_pm_ops,
403 .uevent = ishtp_cl_uevent,
404 };
405
406 static void ishtp_cl_dev_release(struct device *dev)
407 {
408 kfree(to_ishtp_cl_device(dev));
409 }
410
411 static const struct device_type ishtp_cl_device_type = {
412 .release = ishtp_cl_dev_release,
413 };
414
415
416
417
418
419
420
421
422
423
424
425
426 static struct ishtp_cl_device *ishtp_bus_add_device(struct ishtp_device *dev,
427 guid_t uuid, char *name)
428 {
429 struct ishtp_cl_device *device;
430 int status;
431 unsigned long flags;
432
433 spin_lock_irqsave(&dev->device_list_lock, flags);
434 list_for_each_entry(device, &dev->device_list, device_link) {
435 if (!strcmp(name, dev_name(&device->dev))) {
436 device->fw_client = &dev->fw_clients[
437 dev->fw_client_presentation_num - 1];
438 spin_unlock_irqrestore(&dev->device_list_lock, flags);
439 ishtp_cl_device_reset(device);
440 return device;
441 }
442 }
443 spin_unlock_irqrestore(&dev->device_list_lock, flags);
444
445 device = kzalloc(sizeof(struct ishtp_cl_device), GFP_KERNEL);
446 if (!device)
447 return NULL;
448
449 device->dev.parent = dev->devc;
450 device->dev.bus = &ishtp_cl_bus_type;
451 device->dev.type = &ishtp_cl_device_type;
452 device->ishtp_dev = dev;
453
454 device->fw_client =
455 &dev->fw_clients[dev->fw_client_presentation_num - 1];
456
457 dev_set_name(&device->dev, "%s", name);
458
459 spin_lock_irqsave(&dev->device_list_lock, flags);
460 list_add_tail(&device->device_link, &dev->device_list);
461 spin_unlock_irqrestore(&dev->device_list_lock, flags);
462
463 status = device_register(&device->dev);
464 if (status) {
465 spin_lock_irqsave(&dev->device_list_lock, flags);
466 list_del(&device->device_link);
467 spin_unlock_irqrestore(&dev->device_list_lock, flags);
468 dev_err(dev->devc, "Failed to register ISHTP client device\n");
469 put_device(&device->dev);
470 return NULL;
471 }
472
473 ishtp_device_ready = true;
474
475 return device;
476 }
477
478
479
480
481
482
483
484
485
486
487 static void ishtp_bus_remove_device(struct ishtp_cl_device *device)
488 {
489 device_unregister(&device->dev);
490 }
491
492
493
494
495
496
497
498
499
500
501
502 int ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
503 struct module *owner)
504 {
505 int err;
506
507 if (!ishtp_device_ready)
508 return -ENODEV;
509
510 driver->driver.name = driver->name;
511 driver->driver.owner = owner;
512 driver->driver.bus = &ishtp_cl_bus_type;
513
514 err = driver_register(&driver->driver);
515 if (err)
516 return err;
517
518 return 0;
519 }
520 EXPORT_SYMBOL(ishtp_cl_driver_register);
521
522
523
524
525
526
527
528 void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver)
529 {
530 driver_unregister(&driver->driver);
531 }
532 EXPORT_SYMBOL(ishtp_cl_driver_unregister);
533
534
535
536
537
538
539
540
541
542 static void ishtp_bus_event_work(struct work_struct *work)
543 {
544 struct ishtp_cl_device *device;
545
546 device = container_of(work, struct ishtp_cl_device, event_work);
547
548 if (device->event_cb)
549 device->event_cb(device);
550 }
551
552
553
554
555
556
557
558
559 void ishtp_cl_bus_rx_event(struct ishtp_cl_device *device)
560 {
561 if (!device || !device->event_cb)
562 return;
563
564 if (device->event_cb)
565 schedule_work(&device->event_work);
566 }
567
568
569
570
571
572
573
574
575
576
577 int ishtp_register_event_cb(struct ishtp_cl_device *device,
578 void (*event_cb)(struct ishtp_cl_device *))
579 {
580 if (device->event_cb)
581 return -EALREADY;
582
583 device->event_cb = event_cb;
584 INIT_WORK(&device->event_work, ishtp_bus_event_work);
585
586 return 0;
587 }
588 EXPORT_SYMBOL(ishtp_register_event_cb);
589
590
591
592
593
594
595
596 void ishtp_get_device(struct ishtp_cl_device *cl_device)
597 {
598 cl_device->reference_count++;
599 }
600 EXPORT_SYMBOL(ishtp_get_device);
601
602
603
604
605
606
607
608 void ishtp_put_device(struct ishtp_cl_device *cl_device)
609 {
610 cl_device->reference_count--;
611 }
612 EXPORT_SYMBOL(ishtp_put_device);
613
614
615
616
617
618
619
620
621 void ishtp_set_drvdata(struct ishtp_cl_device *cl_device, void *data)
622 {
623 cl_device->driver_data = data;
624 }
625 EXPORT_SYMBOL(ishtp_set_drvdata);
626
627
628
629
630
631
632
633
634
635 void *ishtp_get_drvdata(struct ishtp_cl_device *cl_device)
636 {
637 return cl_device->driver_data;
638 }
639 EXPORT_SYMBOL(ishtp_get_drvdata);
640
641
642
643
644
645
646
647
648
649 struct ishtp_cl_device *ishtp_dev_to_cl_device(struct device *device)
650 {
651 return to_ishtp_cl_device(device);
652 }
653 EXPORT_SYMBOL(ishtp_dev_to_cl_device);
654
655
656
657
658
659
660
661
662
663
664 int ishtp_bus_new_client(struct ishtp_device *dev)
665 {
666 int i;
667 char *dev_name;
668 struct ishtp_cl_device *cl_device;
669 guid_t device_uuid;
670
671
672
673
674
675
676
677 i = dev->fw_client_presentation_num - 1;
678 device_uuid = dev->fw_clients[i].props.protocol_name;
679 dev_name = kasprintf(GFP_KERNEL, "{%pUL}", &device_uuid);
680 if (!dev_name)
681 return -ENOMEM;
682
683 cl_device = ishtp_bus_add_device(dev, device_uuid, dev_name);
684 if (!cl_device) {
685 kfree(dev_name);
686 return -ENOENT;
687 }
688
689 kfree(dev_name);
690
691 return 0;
692 }
693
694
695
696
697
698
699
700
701
702 int ishtp_cl_device_bind(struct ishtp_cl *cl)
703 {
704 struct ishtp_cl_device *cl_device;
705 unsigned long flags;
706 int rv;
707
708 if (!cl->fw_client_id || cl->state != ISHTP_CL_CONNECTED)
709 return -EFAULT;
710
711 rv = -ENOENT;
712 spin_lock_irqsave(&cl->dev->device_list_lock, flags);
713 list_for_each_entry(cl_device, &cl->dev->device_list,
714 device_link) {
715 if (cl_device->fw_client &&
716 cl_device->fw_client->client_id == cl->fw_client_id) {
717 cl->device = cl_device;
718 rv = 0;
719 break;
720 }
721 }
722 spin_unlock_irqrestore(&cl->dev->device_list_lock, flags);
723 return rv;
724 }
725
726
727
728
729
730
731
732
733
734
735
736 void ishtp_bus_remove_all_clients(struct ishtp_device *ishtp_dev,
737 bool warm_reset)
738 {
739 struct ishtp_cl_device *cl_device, *n;
740 struct ishtp_cl *cl;
741 unsigned long flags;
742
743 spin_lock_irqsave(&ishtp_dev->cl_list_lock, flags);
744 list_for_each_entry(cl, &ishtp_dev->cl_list, link) {
745 cl->state = ISHTP_CL_DISCONNECTED;
746
747
748
749
750
751
752 wake_up_interruptible(&cl->wait_ctrl_res);
753
754
755 ishtp_cl_flush_queues(cl);
756
757
758 ishtp_cl_free_rx_ring(cl);
759 ishtp_cl_free_tx_ring(cl);
760
761
762
763
764
765
766 }
767 spin_unlock_irqrestore(&ishtp_dev->cl_list_lock, flags);
768
769
770 ishtp_cl_free_dma_buf(ishtp_dev);
771
772
773 spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
774 list_for_each_entry_safe(cl_device, n, &ishtp_dev->device_list,
775 device_link) {
776 cl_device->fw_client = NULL;
777 if (warm_reset && cl_device->reference_count)
778 continue;
779
780 list_del(&cl_device->device_link);
781 spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
782 ishtp_bus_remove_device(cl_device);
783 spin_lock_irqsave(&ishtp_dev->device_list_lock, flags);
784 }
785 spin_unlock_irqrestore(&ishtp_dev->device_list_lock, flags);
786
787
788 spin_lock_irqsave(&ishtp_dev->fw_clients_lock, flags);
789 kfree(ishtp_dev->fw_clients);
790 ishtp_dev->fw_clients = NULL;
791 ishtp_dev->fw_clients_num = 0;
792 ishtp_dev->fw_client_presentation_num = 0;
793 ishtp_dev->fw_client_index = 0;
794 bitmap_zero(ishtp_dev->fw_clients_map, ISHTP_CLIENTS_MAX);
795 spin_unlock_irqrestore(&ishtp_dev->fw_clients_lock, flags);
796 }
797 EXPORT_SYMBOL(ishtp_bus_remove_all_clients);
798
799
800
801
802
803
804
805 void ishtp_reset_handler(struct ishtp_device *dev)
806 {
807 unsigned long flags;
808
809
810 dev->dev_state = ISHTP_DEV_RESETTING;
811
812
813 spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
814 dev->rd_msg_fifo_head = dev->rd_msg_fifo_tail = 0;
815 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
816
817
818 ishtp_bus_remove_all_clients(dev, true);
819 }
820 EXPORT_SYMBOL(ishtp_reset_handler);
821
822
823
824
825
826
827
828
829 void ishtp_reset_compl_handler(struct ishtp_device *dev)
830 {
831 dev->dev_state = ISHTP_DEV_INIT_CLIENTS;
832 dev->hbm_state = ISHTP_HBM_START;
833 ishtp_hbm_start_req(dev);
834 }
835 EXPORT_SYMBOL(ishtp_reset_compl_handler);
836
837
838
839
840
841
842
843
844 int ishtp_use_dma_transfer(void)
845 {
846 return ishtp_use_dma;
847 }
848
849
850
851
852
853
854
855
856
857 struct device *ishtp_device(struct ishtp_cl_device *device)
858 {
859 return &device->dev;
860 }
861 EXPORT_SYMBOL(ishtp_device);
862
863
864
865
866
867
868
869
870 struct device *ishtp_get_pci_device(struct ishtp_cl_device *device)
871 {
872 return device->ishtp_dev->devc;
873 }
874 EXPORT_SYMBOL(ishtp_get_pci_device);
875
876
877
878
879
880
881
882
883 void *ishtp_trace_callback(struct ishtp_cl_device *cl_device)
884 {
885 return cl_device->ishtp_dev->print_log;
886 }
887 EXPORT_SYMBOL(ishtp_trace_callback);
888
889
890
891
892
893
894
895
896 int ish_hw_reset(struct ishtp_device *dev)
897 {
898 return dev->ops->hw_reset(dev);
899 }
900 EXPORT_SYMBOL(ish_hw_reset);
901
902
903
904
905
906
907
908
909 static int __init ishtp_bus_register(void)
910 {
911 return bus_register(&ishtp_cl_bus_type);
912 }
913
914
915
916
917
918
919 static void __exit ishtp_bus_unregister(void)
920 {
921 bus_unregister(&ishtp_cl_bus_type);
922 }
923
924 module_init(ishtp_bus_register);
925 module_exit(ishtp_bus_unregister);
926
927 MODULE_LICENSE("GPL");