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