tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / misc / mei / client.c
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2012, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  */
16
17 #include <linux/pci.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/delay.h>
21
22 #include <linux/mei.h>
23
24 #include "mei_dev.h"
25 #include "hbm.h"
26 #include "client.h"
27
28 /**
29  * mei_me_cl_by_uuid - locate index of me client
30  *
31  * @dev: mei device
32  * returns me client index or -ENOENT if not found
33  */
34 int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
35 {
36         int i, res = -ENOENT;
37
38         for (i = 0; i < dev->me_clients_num; ++i)
39                 if (uuid_le_cmp(*uuid,
40                                 dev->me_clients[i].props.protocol_name) == 0) {
41                         res = i;
42                         break;
43                 }
44
45         return res;
46 }
47
48
49 /**
50  * mei_me_cl_by_id return index to me_clients for client_id
51  *
52  * @dev: the device structure
53  * @client_id: me client id
54  *
55  * Locking: called under "dev->device_lock" lock
56  *
57  * returns index on success, -ENOENT on failure.
58  */
59
60 int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
61 {
62         int i;
63         for (i = 0; i < dev->me_clients_num; i++)
64                 if (dev->me_clients[i].client_id == client_id)
65                         break;
66         if (WARN_ON(dev->me_clients[i].client_id != client_id))
67                 return -ENOENT;
68
69         if (i == dev->me_clients_num)
70                 return -ENOENT;
71
72         return i;
73 }
74
75
76 /**
77  * mei_io_list_flush - removes list entry belonging to cl.
78  *
79  * @list:  An instance of our list structure
80  * @cl: host client
81  */
82 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
83 {
84         struct mei_cl_cb *cb;
85         struct mei_cl_cb *next;
86
87         list_for_each_entry_safe(cb, next, &list->list, list) {
88                 if (cb->cl && mei_cl_cmp_id(cl, cb->cl))
89                         list_del(&cb->list);
90         }
91 }
92
93 /**
94  * mei_io_cb_free - free mei_cb_private related memory
95  *
96  * @cb: mei callback struct
97  */
98 void mei_io_cb_free(struct mei_cl_cb *cb)
99 {
100         if (cb == NULL)
101                 return;
102
103         kfree(cb->request_buffer.data);
104         kfree(cb->response_buffer.data);
105         kfree(cb);
106 }
107
108 /**
109  * mei_io_cb_init - allocate and initialize io callback
110  *
111  * @cl - mei client
112  * @fp: pointer to file structure
113  *
114  * returns mei_cl_cb pointer or NULL;
115  */
116 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
117 {
118         struct mei_cl_cb *cb;
119
120         cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
121         if (!cb)
122                 return NULL;
123
124         mei_io_list_init(cb);
125
126         cb->file_object = fp;
127         cb->cl = cl;
128         cb->buf_idx = 0;
129         return cb;
130 }
131
132 /**
133  * mei_io_cb_alloc_req_buf - allocate request buffer
134  *
135  * @cb: io callback structure
136  * @length: size of the buffer
137  *
138  * returns 0 on success
139  *         -EINVAL if cb is NULL
140  *         -ENOMEM if allocation failed
141  */
142 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
143 {
144         if (!cb)
145                 return -EINVAL;
146
147         if (length == 0)
148                 return 0;
149
150         cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
151         if (!cb->request_buffer.data)
152                 return -ENOMEM;
153         cb->request_buffer.size = length;
154         return 0;
155 }
156 /**
157  * mei_io_cb_alloc_resp_buf - allocate respose buffer
158  *
159  * @cb: io callback structure
160  * @length: size of the buffer
161  *
162  * returns 0 on success
163  *         -EINVAL if cb is NULL
164  *         -ENOMEM if allocation failed
165  */
166 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
167 {
168         if (!cb)
169                 return -EINVAL;
170
171         if (length == 0)
172                 return 0;
173
174         cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
175         if (!cb->response_buffer.data)
176                 return -ENOMEM;
177         cb->response_buffer.size = length;
178         return 0;
179 }
180
181
182
183 /**
184  * mei_cl_flush_queues - flushes queue lists belonging to cl.
185  *
186  * @cl: host client
187  */
188 int mei_cl_flush_queues(struct mei_cl *cl)
189 {
190         if (WARN_ON(!cl || !cl->dev))
191                 return -EINVAL;
192
193         dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
194         mei_io_list_flush(&cl->dev->read_list, cl);
195         mei_io_list_flush(&cl->dev->write_list, cl);
196         mei_io_list_flush(&cl->dev->write_waiting_list, cl);
197         mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
198         mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
199         mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
200         mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
201         return 0;
202 }
203
204
205 /**
206  * mei_cl_init - initializes intialize cl.
207  *
208  * @cl: host client to be initialized
209  * @dev: mei device
210  */
211 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
212 {
213         memset(cl, 0, sizeof(struct mei_cl));
214         init_waitqueue_head(&cl->wait);
215         init_waitqueue_head(&cl->rx_wait);
216         init_waitqueue_head(&cl->tx_wait);
217         INIT_LIST_HEAD(&cl->link);
218         INIT_LIST_HEAD(&cl->device_link);
219         cl->reading_state = MEI_IDLE;
220         cl->writing_state = MEI_IDLE;
221         cl->dev = dev;
222 }
223
224 /**
225  * mei_cl_allocate - allocates cl  structure and sets it up.
226  *
227  * @dev: mei device
228  * returns  The allocated file or NULL on failure
229  */
230 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
231 {
232         struct mei_cl *cl;
233
234         cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
235         if (!cl)
236                 return NULL;
237
238         mei_cl_init(cl, dev);
239
240         return cl;
241 }
242
243 /**
244  * mei_cl_find_read_cb - find this cl's callback in the read list
245  *
246  * @cl: host client
247  *
248  * returns cb on success, NULL on error
249  */
250 struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
251 {
252         struct mei_device *dev = cl->dev;
253         struct mei_cl_cb *cb = NULL;
254         struct mei_cl_cb *next = NULL;
255
256         list_for_each_entry_safe(cb, next, &dev->read_list.list, list)
257                 if (mei_cl_cmp_id(cl, cb->cl))
258                         return cb;
259         return NULL;
260 }
261
262 /** mei_cl_link: allocte host id in the host map
263  *
264  * @cl - host client
265  * @id - fixed host id or -1 for genereting one
266  *
267  * returns 0 on success
268  *      -EINVAL on incorrect values
269  *      -ENONET if client not found
270  */
271 int mei_cl_link(struct mei_cl *cl, int id)
272 {
273         struct mei_device *dev;
274
275         if (WARN_ON(!cl || !cl->dev))
276                 return -EINVAL;
277
278         dev = cl->dev;
279
280         /* If Id is not asigned get one*/
281         if (id == MEI_HOST_CLIENT_ID_ANY)
282                 id = find_first_zero_bit(dev->host_clients_map,
283                                         MEI_CLIENTS_MAX);
284
285         if (id >= MEI_CLIENTS_MAX) {
286                 dev_err(&dev->pdev->dev, "id exceded %d", MEI_CLIENTS_MAX) ;
287                 return -ENOENT;
288         }
289
290         dev->open_handle_count++;
291
292         cl->host_client_id = id;
293         list_add_tail(&cl->link, &dev->file_list);
294
295         set_bit(id, dev->host_clients_map);
296
297         cl->state = MEI_FILE_INITIALIZING;
298
299         dev_dbg(&dev->pdev->dev, "link cl host id = %d\n", cl->host_client_id);
300         return 0;
301 }
302
303 /**
304  * mei_cl_unlink - remove me_cl from the list
305  *
306  * @cl: host client
307  */
308 int mei_cl_unlink(struct mei_cl *cl)
309 {
310         struct mei_device *dev;
311         struct mei_cl *pos, *next;
312
313         /* don't shout on error exit path */
314         if (!cl)
315                 return 0;
316
317         /* wd and amthif might not be initialized */
318         if (!cl->dev)
319                 return 0;
320
321         dev = cl->dev;
322
323         list_for_each_entry_safe(pos, next, &dev->file_list, link) {
324                 if (cl->host_client_id == pos->host_client_id) {
325                         dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
326                                 pos->host_client_id, pos->me_client_id);
327                         list_del_init(&pos->link);
328                         break;
329                 }
330         }
331         return 0;
332 }
333
334
335 void mei_host_client_init(struct work_struct *work)
336 {
337         struct mei_device *dev = container_of(work,
338                                               struct mei_device, init_work);
339         struct mei_client_properties *client_props;
340         int i;
341
342         mutex_lock(&dev->device_lock);
343
344         bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
345         dev->open_handle_count = 0;
346
347         /*
348          * Reserving the first three client IDs
349          * 0: Reserved for MEI Bus Message communications
350          * 1: Reserved for Watchdog
351          * 2: Reserved for AMTHI
352          */
353         bitmap_set(dev->host_clients_map, 0, 3);
354
355         for (i = 0; i < dev->me_clients_num; i++) {
356                 client_props = &dev->me_clients[i].props;
357
358                 if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
359                         mei_amthif_host_init(dev);
360                 else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
361                         mei_wd_host_init(dev);
362                 else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
363                         mei_nfc_host_init(dev);
364
365         }
366
367         dev->dev_state = MEI_DEV_ENABLED;
368
369         mutex_unlock(&dev->device_lock);
370 }
371
372
373 /**
374  * mei_cl_disconnect - disconnect host clinet form the me one
375  *
376  * @cl: host client
377  *
378  * Locking: called under "dev->device_lock" lock
379  *
380  * returns 0 on success, <0 on failure.
381  */
382 int mei_cl_disconnect(struct mei_cl *cl)
383 {
384         struct mei_device *dev;
385         struct mei_cl_cb *cb;
386         int rets, err;
387
388         if (WARN_ON(!cl || !cl->dev))
389                 return -ENODEV;
390
391         dev = cl->dev;
392
393         if (cl->state != MEI_FILE_DISCONNECTING)
394                 return 0;
395
396         cb = mei_io_cb_init(cl, NULL);
397         if (!cb)
398                 return -ENOMEM;
399
400         cb->fop_type = MEI_FOP_CLOSE;
401         if (dev->hbuf_is_ready) {
402                 dev->hbuf_is_ready = false;
403                 if (mei_hbm_cl_disconnect_req(dev, cl)) {
404                         rets = -ENODEV;
405                         dev_err(&dev->pdev->dev, "failed to disconnect.\n");
406                         goto free;
407                 }
408                 cl->timer_count = MEI_CONNECT_TIMEOUT;
409                 mdelay(10); /* Wait for hardware disconnection ready */
410                 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
411         } else {
412                 dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
413                 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
414
415         }
416         mutex_unlock(&dev->device_lock);
417
418         err = wait_event_timeout(dev->wait_recvd_msg,
419                         MEI_FILE_DISCONNECTED == cl->state,
420                         mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
421
422         mutex_lock(&dev->device_lock);
423         if (MEI_FILE_DISCONNECTED == cl->state) {
424                 rets = 0;
425                 dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
426         } else {
427                 rets = -ENODEV;
428                 if (MEI_FILE_DISCONNECTED != cl->state)
429                         dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
430
431                 if (err)
432                         dev_dbg(&dev->pdev->dev,
433                                         "wait failed disconnect err=%08x\n",
434                                         err);
435
436                 dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
437         }
438
439         mei_io_list_flush(&dev->ctrl_rd_list, cl);
440         mei_io_list_flush(&dev->ctrl_wr_list, cl);
441 free:
442         mei_io_cb_free(cb);
443         return rets;
444 }
445
446
447 /**
448  * mei_cl_is_other_connecting - checks if other
449  *    client with the same me client id is connecting
450  *
451  * @cl: private data of the file object
452  *
453  * returns ture if other client is connected, 0 - otherwise.
454  */
455 bool mei_cl_is_other_connecting(struct mei_cl *cl)
456 {
457         struct mei_device *dev;
458         struct mei_cl *pos;
459         struct mei_cl *next;
460
461         if (WARN_ON(!cl || !cl->dev))
462                 return false;
463
464         dev = cl->dev;
465
466         list_for_each_entry_safe(pos, next, &dev->file_list, link) {
467                 if ((pos->state == MEI_FILE_CONNECTING) &&
468                     (pos != cl) && cl->me_client_id == pos->me_client_id)
469                         return true;
470
471         }
472
473         return false;
474 }
475
476 /**
477  * mei_cl_connect - connect host clinet to the me one
478  *
479  * @cl: host client
480  *
481  * Locking: called under "dev->device_lock" lock
482  *
483  * returns 0 on success, <0 on failure.
484  */
485 int mei_cl_connect(struct mei_cl *cl, struct file *file)
486 {
487         struct mei_device *dev;
488         struct mei_cl_cb *cb;
489         long timeout = mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT);
490         int rets;
491
492         if (WARN_ON(!cl || !cl->dev))
493                 return -ENODEV;
494
495         dev = cl->dev;
496
497         cb = mei_io_cb_init(cl, file);
498         if (!cb) {
499                 rets = -ENOMEM;
500                 goto out;
501         }
502
503         cb->fop_type = MEI_FOP_IOCTL;
504
505         if (dev->hbuf_is_ready && !mei_cl_is_other_connecting(cl)) {
506                 dev->hbuf_is_ready = false;
507
508                 if (mei_hbm_cl_connect_req(dev, cl)) {
509                         rets = -ENODEV;
510                         goto out;
511                 }
512                 cl->timer_count = MEI_CONNECT_TIMEOUT;
513                 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
514         } else {
515                 cl->state = MEI_FILE_INITIALIZING;
516                 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
517         }
518
519         mutex_unlock(&dev->device_lock);
520         rets = wait_event_timeout(dev->wait_recvd_msg,
521                                  (cl->state == MEI_FILE_CONNECTED ||
522                                   cl->state == MEI_FILE_DISCONNECTED),
523                                  timeout * HZ);
524         mutex_lock(&dev->device_lock);
525
526         if (cl->state != MEI_FILE_CONNECTED) {
527                 rets = -EFAULT;
528
529                 mei_io_list_flush(&dev->ctrl_rd_list, cl);
530                 mei_io_list_flush(&dev->ctrl_wr_list, cl);
531                 goto out;
532         }
533
534         rets = cl->status;
535
536 out:
537         mei_io_cb_free(cb);
538         return rets;
539 }
540
541 /**
542  * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
543  *
544  * @cl: private data of the file object
545  *
546  * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
547  *      -ENOENT if mei_cl is not present
548  *      -EINVAL if single_recv_buf == 0
549  */
550 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
551 {
552         struct mei_device *dev;
553         int i;
554
555         if (WARN_ON(!cl || !cl->dev))
556                 return -EINVAL;
557
558         dev = cl->dev;
559
560         if (!dev->me_clients_num)
561                 return 0;
562
563         if (cl->mei_flow_ctrl_creds > 0)
564                 return 1;
565
566         for (i = 0; i < dev->me_clients_num; i++) {
567                 struct mei_me_client  *me_cl = &dev->me_clients[i];
568                 if (me_cl->client_id == cl->me_client_id) {
569                         if (me_cl->mei_flow_ctrl_creds) {
570                                 if (WARN_ON(me_cl->props.single_recv_buf == 0))
571                                         return -EINVAL;
572                                 return 1;
573                         } else {
574                                 return 0;
575                         }
576                 }
577         }
578         return -ENOENT;
579 }
580
581 /**
582  * mei_cl_flow_ctrl_reduce - reduces flow_control.
583  *
584  * @cl: private data of the file object
585  *
586  * @returns
587  *      0 on success
588  *      -ENOENT when me client is not found
589  *      -EINVAL when ctrl credits are <= 0
590  */
591 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
592 {
593         struct mei_device *dev;
594         int i;
595
596         if (WARN_ON(!cl || !cl->dev))
597                 return -EINVAL;
598
599         dev = cl->dev;
600
601         if (!dev->me_clients_num)
602                 return -ENOENT;
603
604         for (i = 0; i < dev->me_clients_num; i++) {
605                 struct mei_me_client  *me_cl = &dev->me_clients[i];
606                 if (me_cl->client_id == cl->me_client_id) {
607                         if (me_cl->props.single_recv_buf != 0) {
608                                 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
609                                         return -EINVAL;
610                                 dev->me_clients[i].mei_flow_ctrl_creds--;
611                         } else {
612                                 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
613                                         return -EINVAL;
614                                 cl->mei_flow_ctrl_creds--;
615                         }
616                         return 0;
617                 }
618         }
619         return -ENOENT;
620 }
621
622 /**
623  * mei_cl_read_start - the start read client message function.
624  *
625  * @cl: host client
626  *
627  * returns 0 on success, <0 on failure.
628  */
629 int mei_cl_read_start(struct mei_cl *cl, size_t length)
630 {
631         struct mei_device *dev;
632         struct mei_cl_cb *cb;
633         int rets;
634         int i;
635
636         if (WARN_ON(!cl || !cl->dev))
637                 return -ENODEV;
638
639         dev = cl->dev;
640
641         if (cl->state != MEI_FILE_CONNECTED)
642                 return -ENODEV;
643
644         if (dev->dev_state != MEI_DEV_ENABLED)
645                 return -ENODEV;
646
647         if (cl->read_cb) {
648                 dev_dbg(&dev->pdev->dev, "read is pending.\n");
649                 return -EBUSY;
650         }
651         i = mei_me_cl_by_id(dev, cl->me_client_id);
652         if (i < 0) {
653                 dev_err(&dev->pdev->dev, "no such me client %d\n",
654                         cl->me_client_id);
655                 return  -ENODEV;
656         }
657
658         cb = mei_io_cb_init(cl, NULL);
659         if (!cb)
660                 return -ENOMEM;
661
662         /* always allocate at least client max message */
663         length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
664         rets = mei_io_cb_alloc_resp_buf(cb, length);
665         if (rets)
666                 goto err;
667
668         cb->fop_type = MEI_FOP_READ;
669         if (dev->hbuf_is_ready) {
670                 dev->hbuf_is_ready = false;
671                 if (mei_hbm_cl_flow_control_req(dev, cl)) {
672                         rets = -ENODEV;
673                         goto err;
674                 }
675                 list_add_tail(&cb->list, &dev->read_list.list);
676         } else {
677                 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
678         }
679
680         cl->read_cb = cb;
681
682         return rets;
683 err:
684         mei_io_cb_free(cb);
685         return rets;
686 }
687
688 /**
689  * mei_cl_write - submit a write cb to mei device
690         assumes device_lock is locked
691  *
692  * @cl: host client
693  * @cl: write callback with filled data
694  *
695  * returns numbe of bytes sent on success, <0 on failure.
696  */
697 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
698 {
699         struct mei_device *dev;
700         struct mei_msg_data *buf;
701         struct mei_msg_hdr mei_hdr;
702         int rets;
703
704
705         if (WARN_ON(!cl || !cl->dev))
706                 return -ENODEV;
707
708         if (WARN_ON(!cb))
709                 return -EINVAL;
710
711         dev = cl->dev;
712
713
714         buf = &cb->request_buffer;
715
716         dev_dbg(&dev->pdev->dev, "mei_cl_write %d\n", buf->size);
717
718
719         cb->fop_type = MEI_FOP_WRITE;
720
721         rets = mei_cl_flow_ctrl_creds(cl);
722         if (rets < 0)
723                 goto err;
724
725         /* Host buffer is not ready, we queue the request */
726         if (rets == 0 || !dev->hbuf_is_ready) {
727                 cb->buf_idx = 0;
728                 /* unseting complete will enqueue the cb for write */
729                 mei_hdr.msg_complete = 0;
730                 cl->writing_state = MEI_WRITING;
731                 rets = buf->size;
732                 goto out;
733         }
734
735         dev->hbuf_is_ready = false;
736
737         /* Check for a maximum length */
738         if (buf->size > mei_hbuf_max_len(dev)) {
739                 mei_hdr.length = mei_hbuf_max_len(dev);
740                 mei_hdr.msg_complete = 0;
741         } else {
742                 mei_hdr.length = buf->size;
743                 mei_hdr.msg_complete = 1;
744         }
745
746         mei_hdr.host_addr = cl->host_client_id;
747         mei_hdr.me_addr = cl->me_client_id;
748         mei_hdr.reserved = 0;
749
750         dev_dbg(&dev->pdev->dev, "write " MEI_HDR_FMT "\n",
751                 MEI_HDR_PRM(&mei_hdr));
752
753
754         if (mei_write_message(dev, &mei_hdr, buf->data)) {
755                 rets = -EIO;
756                 goto err;
757         }
758
759         cl->writing_state = MEI_WRITING;
760         cb->buf_idx = mei_hdr.length;
761
762         rets = buf->size;
763 out:
764         if (mei_hdr.msg_complete) {
765                 if (mei_cl_flow_ctrl_reduce(cl)) {
766                         rets = -ENODEV;
767                         goto err;
768                 }
769                 list_add_tail(&cb->list, &dev->write_waiting_list.list);
770         } else {
771                 list_add_tail(&cb->list, &dev->write_list.list);
772         }
773
774
775         if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
776
777                 mutex_unlock(&dev->device_lock);
778                 if (wait_event_interruptible(cl->tx_wait,
779                         cl->writing_state == MEI_WRITE_COMPLETE)) {
780                                 if (signal_pending(current))
781                                         rets = -EINTR;
782                                 else
783                                         rets = -ERESTARTSYS;
784                 }
785                 mutex_lock(&dev->device_lock);
786         }
787 err:
788         return rets;
789 }
790
791
792
793 /**
794  * mei_cl_all_disconnect - disconnect forcefully all connected clients
795  *
796  * @dev - mei device
797  */
798
799 void mei_cl_all_disconnect(struct mei_device *dev)
800 {
801         struct mei_cl *cl, *next;
802
803         list_for_each_entry_safe(cl, next, &dev->file_list, link) {
804                 cl->state = MEI_FILE_DISCONNECTED;
805                 cl->mei_flow_ctrl_creds = 0;
806                 cl->timer_count = 0;
807         }
808 }
809
810
811 /**
812  * mei_cl_all_read_wakeup  - wake up all readings so they can be interrupted
813  *
814  * @dev  - mei device
815  */
816 void mei_cl_all_read_wakeup(struct mei_device *dev)
817 {
818         struct mei_cl *cl, *next;
819         list_for_each_entry_safe(cl, next, &dev->file_list, link) {
820                 if (waitqueue_active(&cl->rx_wait)) {
821                         dev_dbg(&dev->pdev->dev, "Waking up client!\n");
822                         wake_up_interruptible(&cl->rx_wait);
823                 }
824         }
825 }
826
827 /**
828  * mei_cl_all_write_clear - clear all pending writes
829
830  * @dev - mei device
831  */
832 void mei_cl_all_write_clear(struct mei_device *dev)
833 {
834         struct mei_cl_cb *cb, *next;
835         struct list_head *list;
836
837         list = &dev->write_list.list;
838         list_for_each_entry_safe(cb, next, list, list) {
839                 list_del(&cb->list);
840                 mei_io_cb_free(cb);
841         }
842
843         list = &dev->write_waiting_list.list;
844         list_for_each_entry_safe(cb, next, list, list) {
845                 list_del(&cb->list);
846                 mei_io_cb_free(cb);
847         }
848 }
849
850