greybus: svc: add stub functions for v_sys, refclk and unipro
[cascardo/linux.git] / drivers / staging / greybus / svc.c
1 /*
2  * SVC Greybus driver.
3  *
4  * Copyright 2015 Google Inc.
5  * Copyright 2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/debugfs.h>
11 #include <linux/input.h>
12 #include <linux/workqueue.h>
13
14 #include "greybus.h"
15
16 #define SVC_KEY_ARA_BUTTON      KEY_A
17
18 #define SVC_INTF_EJECT_TIMEOUT  9000
19
20 struct gb_svc_deferred_request {
21         struct work_struct work;
22         struct gb_operation *operation;
23 };
24
25
26 static ssize_t endo_id_show(struct device *dev,
27                         struct device_attribute *attr, char *buf)
28 {
29         struct gb_svc *svc = to_gb_svc(dev);
30
31         return sprintf(buf, "0x%04x\n", svc->endo_id);
32 }
33 static DEVICE_ATTR_RO(endo_id);
34
35 static ssize_t ap_intf_id_show(struct device *dev,
36                         struct device_attribute *attr, char *buf)
37 {
38         struct gb_svc *svc = to_gb_svc(dev);
39
40         return sprintf(buf, "%u\n", svc->ap_intf_id);
41 }
42 static DEVICE_ATTR_RO(ap_intf_id);
43
44
45 // FIXME
46 // This is a hack, we need to do this "right" and clean the interface up
47 // properly, not just forcibly yank the thing out of the system and hope for the
48 // best.  But for now, people want their modules to come out without having to
49 // throw the thing to the ground or get out a screwdriver.
50 static ssize_t intf_eject_store(struct device *dev,
51                                 struct device_attribute *attr, const char *buf,
52                                 size_t len)
53 {
54         struct gb_svc *svc = to_gb_svc(dev);
55         unsigned short intf_id;
56         int ret;
57
58         ret = kstrtou16(buf, 10, &intf_id);
59         if (ret < 0)
60                 return ret;
61
62         dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
63
64         ret = gb_svc_intf_eject(svc, intf_id);
65         if (ret < 0)
66                 return ret;
67
68         return len;
69 }
70 static DEVICE_ATTR_WO(intf_eject);
71
72 static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
73                              char *buf)
74 {
75         struct gb_svc *svc = to_gb_svc(dev);
76
77         return sprintf(buf, "%s\n",
78                        gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
79 }
80
81 static ssize_t watchdog_store(struct device *dev,
82                               struct device_attribute *attr, const char *buf,
83                               size_t len)
84 {
85         struct gb_svc *svc = to_gb_svc(dev);
86         int retval;
87         bool user_request;
88
89         retval = strtobool(buf, &user_request);
90         if (retval)
91                 return retval;
92
93         if (user_request)
94                 retval = gb_svc_watchdog_enable(svc);
95         else
96                 retval = gb_svc_watchdog_disable(svc);
97         if (retval)
98                 return retval;
99         return len;
100 }
101 static DEVICE_ATTR_RW(watchdog);
102
103 static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value)
104 {
105         struct gb_svc_pwrmon_rail_count_get_response response;
106         int ret;
107
108         ret = gb_operation_sync(svc->connection,
109                                 GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0,
110                                 &response, sizeof(response));
111         if (ret) {
112                 dev_err(&svc->dev, "failed to get rail count: %d\n", ret);
113                 return ret;
114         }
115
116         *value = response.rail_count;
117
118         return 0;
119 }
120
121 static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc,
122                 struct gb_svc_pwrmon_rail_names_get_response *response,
123                 size_t bufsize)
124 {
125         int ret;
126
127         ret = gb_operation_sync(svc->connection,
128                                 GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0,
129                                 response, bufsize);
130         if (ret) {
131                 dev_err(&svc->dev, "failed to get rail names: %d\n", ret);
132                 return ret;
133         }
134
135         return 0;
136 }
137
138 static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id,
139                                     u8 measurement_type, u32 *value)
140 {
141         struct gb_svc_pwrmon_sample_get_request request;
142         struct gb_svc_pwrmon_sample_get_response response;
143         int ret;
144
145         request.rail_id = rail_id;
146         request.measurement_type = measurement_type;
147
148         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET,
149                                 &request, sizeof(request),
150                                 &response, sizeof(response));
151         if (ret) {
152                 dev_err(&svc->dev, "failed to get rail sample: %d\n", ret);
153                 return ret;
154         }
155
156         if (response.result) {
157                 dev_err(&svc->dev,
158                         "UniPro error while getting rail power sample (%d %d): %d\n",
159                         rail_id, measurement_type, response.result);
160                 switch (response.result) {
161                 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
162                         return -EINVAL;
163                 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
164                         return -ENOMSG;
165                 default:
166                         return -EIO;
167                 }
168         }
169
170         *value = le32_to_cpu(response.measurement);
171
172         return 0;
173 }
174
175 int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
176                                   u8 measurement_type, u32 *value)
177 {
178         struct gb_svc_pwrmon_intf_sample_get_request request;
179         struct gb_svc_pwrmon_intf_sample_get_response response;
180         int ret;
181
182         request.intf_id = intf_id;
183         request.measurement_type = measurement_type;
184
185         ret = gb_operation_sync(svc->connection,
186                                 GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
187                                 &request, sizeof(request),
188                                 &response, sizeof(response));
189         if (ret) {
190                 dev_err(&svc->dev, "failed to get intf sample: %d\n", ret);
191                 return ret;
192         }
193
194         if (response.result) {
195                 dev_err(&svc->dev,
196                         "UniPro error while getting intf power sample (%d %d): %d\n",
197                         intf_id, measurement_type, response.result);
198                 switch (response.result) {
199                 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
200                         return -EINVAL;
201                 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
202                         return -ENOSYS;
203                 default:
204                         return -EIO;
205                 }
206         }
207
208         *value = le32_to_cpu(response.measurement);
209
210         return 0;
211 }
212
213 static struct attribute *svc_attrs[] = {
214         &dev_attr_endo_id.attr,
215         &dev_attr_ap_intf_id.attr,
216         &dev_attr_intf_eject.attr,
217         &dev_attr_watchdog.attr,
218         NULL,
219 };
220 ATTRIBUTE_GROUPS(svc);
221
222 int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
223 {
224         struct gb_svc_intf_device_id_request request;
225
226         request.intf_id = intf_id;
227         request.device_id = device_id;
228
229         return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
230                                  &request, sizeof(request), NULL, 0);
231 }
232
233 int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
234 {
235         struct gb_svc_intf_eject_request request;
236         int ret;
237
238         request.intf_id = intf_id;
239
240         /*
241          * The pulse width for module release in svc is long so we need to
242          * increase the timeout so the operation will not return to soon.
243          */
244         ret = gb_operation_sync_timeout(svc->connection,
245                                         GB_SVC_TYPE_INTF_EJECT, &request,
246                                         sizeof(request), NULL, 0,
247                                         SVC_INTF_EJECT_TIMEOUT);
248         if (ret) {
249                 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
250                 return ret;
251         }
252
253         return 0;
254 }
255
256 int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable)
257 {
258         /* FIXME: implement */
259
260         return 0;
261 }
262
263 int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable)
264 {
265         /* FIXME: implement */
266
267         return 0;
268 }
269
270 int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable)
271 {
272         /* FIXME: implement */
273
274         return 0;
275 }
276
277 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
278                         u32 *value)
279 {
280         struct gb_svc_dme_peer_get_request request;
281         struct gb_svc_dme_peer_get_response response;
282         u16 result;
283         int ret;
284
285         request.intf_id = intf_id;
286         request.attr = cpu_to_le16(attr);
287         request.selector = cpu_to_le16(selector);
288
289         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
290                                 &request, sizeof(request),
291                                 &response, sizeof(response));
292         if (ret) {
293                 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
294                                 intf_id, attr, selector, ret);
295                 return ret;
296         }
297
298         result = le16_to_cpu(response.result_code);
299         if (result) {
300                 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
301                                 intf_id, attr, selector, result);
302                 return -EIO;
303         }
304
305         if (value)
306                 *value = le32_to_cpu(response.attr_value);
307
308         return 0;
309 }
310 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
311
312 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
313                         u32 value)
314 {
315         struct gb_svc_dme_peer_set_request request;
316         struct gb_svc_dme_peer_set_response response;
317         u16 result;
318         int ret;
319
320         request.intf_id = intf_id;
321         request.attr = cpu_to_le16(attr);
322         request.selector = cpu_to_le16(selector);
323         request.value = cpu_to_le32(value);
324
325         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
326                                 &request, sizeof(request),
327                                 &response, sizeof(response));
328         if (ret) {
329                 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
330                                 intf_id, attr, selector, value, ret);
331                 return ret;
332         }
333
334         result = le16_to_cpu(response.result_code);
335         if (result) {
336                 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
337                                 intf_id, attr, selector, value, result);
338                 return -EIO;
339         }
340
341         return 0;
342 }
343 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
344
345 int gb_svc_connection_create(struct gb_svc *svc,
346                                 u8 intf1_id, u16 cport1_id,
347                                 u8 intf2_id, u16 cport2_id,
348                                 u8 cport_flags)
349 {
350         struct gb_svc_conn_create_request request;
351
352         request.intf1_id = intf1_id;
353         request.cport1_id = cpu_to_le16(cport1_id);
354         request.intf2_id = intf2_id;
355         request.cport2_id = cpu_to_le16(cport2_id);
356         request.tc = 0;         /* TC0 */
357         request.flags = cport_flags;
358
359         return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
360                                  &request, sizeof(request), NULL, 0);
361 }
362 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
363
364 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
365                                u8 intf2_id, u16 cport2_id)
366 {
367         struct gb_svc_conn_destroy_request request;
368         struct gb_connection *connection = svc->connection;
369         int ret;
370
371         request.intf1_id = intf1_id;
372         request.cport1_id = cpu_to_le16(cport1_id);
373         request.intf2_id = intf2_id;
374         request.cport2_id = cpu_to_le16(cport2_id);
375
376         ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
377                                 &request, sizeof(request), NULL, 0);
378         if (ret) {
379                 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
380                                 intf1_id, cport1_id, intf2_id, cport2_id, ret);
381         }
382 }
383 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
384
385 /* Creates bi-directional routes between the devices */
386 int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
387                                u8 intf2_id, u8 dev2_id)
388 {
389         struct gb_svc_route_create_request request;
390
391         request.intf1_id = intf1_id;
392         request.dev1_id = dev1_id;
393         request.intf2_id = intf2_id;
394         request.dev2_id = dev2_id;
395
396         return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
397                                  &request, sizeof(request), NULL, 0);
398 }
399
400 /* Destroys bi-directional routes between the devices */
401 void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
402 {
403         struct gb_svc_route_destroy_request request;
404         int ret;
405
406         request.intf1_id = intf1_id;
407         request.intf2_id = intf2_id;
408
409         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
410                                 &request, sizeof(request), NULL, 0);
411         if (ret) {
412                 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
413                                 intf1_id, intf2_id, ret);
414         }
415 }
416
417 int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
418                                u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
419                                u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
420                                u8 flags, u32 quirks)
421 {
422         struct gb_svc_intf_set_pwrm_request request;
423         struct gb_svc_intf_set_pwrm_response response;
424         int ret;
425
426         request.intf_id = intf_id;
427         request.hs_series = hs_series;
428         request.tx_mode = tx_mode;
429         request.tx_gear = tx_gear;
430         request.tx_nlanes = tx_nlanes;
431         request.rx_mode = rx_mode;
432         request.rx_gear = rx_gear;
433         request.rx_nlanes = rx_nlanes;
434         request.flags = flags;
435         request.quirks = cpu_to_le32(quirks);
436
437         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
438                                 &request, sizeof(request),
439                                 &response, sizeof(response));
440         if (ret < 0)
441                 return ret;
442
443         return le16_to_cpu(response.result_code);
444 }
445 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
446
447 int gb_svc_ping(struct gb_svc *svc)
448 {
449         return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
450                                          NULL, 0, NULL, 0,
451                                          GB_OPERATION_TIMEOUT_DEFAULT * 2);
452 }
453 EXPORT_SYMBOL_GPL(gb_svc_ping);
454
455 static int gb_svc_version_request(struct gb_operation *op)
456 {
457         struct gb_connection *connection = op->connection;
458         struct gb_svc *svc = gb_connection_get_data(connection);
459         struct gb_protocol_version_request *request;
460         struct gb_protocol_version_response *response;
461
462         if (op->request->payload_size < sizeof(*request)) {
463                 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
464                                 op->request->payload_size,
465                                 sizeof(*request));
466                 return -EINVAL;
467         }
468
469         request = op->request->payload;
470
471         if (request->major > GB_SVC_VERSION_MAJOR) {
472                 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
473                                 request->major, GB_SVC_VERSION_MAJOR);
474                 return -ENOTSUPP;
475         }
476
477         svc->protocol_major = request->major;
478         svc->protocol_minor = request->minor;
479
480         if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
481                 return -ENOMEM;
482
483         response = op->response->payload;
484         response->major = svc->protocol_major;
485         response->minor = svc->protocol_minor;
486
487         return 0;
488 }
489
490 static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
491                                         size_t len, loff_t *offset)
492 {
493         struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
494         struct gb_svc *svc = pwrmon_rails->svc;
495         int ret, desc;
496         u32 value;
497         char buff[16];
498
499         ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
500                                        GB_SVC_PWRMON_TYPE_VOL, &value);
501         if (ret) {
502                 dev_err(&svc->dev,
503                         "failed to get voltage sample %u: %d\n",
504                         pwrmon_rails->id, ret);
505                 return ret;
506         }
507
508         desc = scnprintf(buff, sizeof(buff), "%u\n", value);
509
510         return simple_read_from_buffer(buf, len, offset, buff, desc);
511 }
512
513 static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
514                                         size_t len, loff_t *offset)
515 {
516         struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
517         struct gb_svc *svc = pwrmon_rails->svc;
518         int ret, desc;
519         u32 value;
520         char buff[16];
521
522         ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
523                                        GB_SVC_PWRMON_TYPE_CURR, &value);
524         if (ret) {
525                 dev_err(&svc->dev,
526                         "failed to get current sample %u: %d\n",
527                         pwrmon_rails->id, ret);
528                 return ret;
529         }
530
531         desc = scnprintf(buff, sizeof(buff), "%u\n", value);
532
533         return simple_read_from_buffer(buf, len, offset, buff, desc);
534 }
535
536 static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
537                                       size_t len, loff_t *offset)
538 {
539         struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
540         struct gb_svc *svc = pwrmon_rails->svc;
541         int ret, desc;
542         u32 value;
543         char buff[16];
544
545         ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
546                                        GB_SVC_PWRMON_TYPE_PWR, &value);
547         if (ret) {
548                 dev_err(&svc->dev, "failed to get power sample %u: %d\n",
549                         pwrmon_rails->id, ret);
550                 return ret;
551         }
552
553         desc = scnprintf(buff, sizeof(buff), "%u\n", value);
554
555         return simple_read_from_buffer(buf, len, offset, buff, desc);
556 }
557
558 static const struct file_operations pwrmon_debugfs_voltage_fops = {
559         .read           = pwr_debugfs_voltage_read,
560 };
561
562 static const struct file_operations pwrmon_debugfs_current_fops = {
563         .read           = pwr_debugfs_current_read,
564 };
565
566 static const struct file_operations pwrmon_debugfs_power_fops = {
567         .read           = pwr_debugfs_power_read,
568 };
569
570 static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
571 {
572         int i;
573         size_t bufsize;
574         struct dentry *dent;
575
576         dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
577         if (IS_ERR_OR_NULL(dent))
578                 return;
579
580         if (gb_svc_pwrmon_rail_count_get(svc, &svc->rail_count))
581                 goto err_pwrmon_debugfs;
582
583         if (!svc->rail_count || svc->rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
584                 goto err_pwrmon_debugfs;
585
586         bufsize = GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * svc->rail_count;
587
588         svc->rail_names = kzalloc(bufsize, GFP_KERNEL);
589         if (!svc->rail_names)
590                 goto err_pwrmon_debugfs;
591
592         svc->pwrmon_rails = kcalloc(svc->rail_count, sizeof(*svc->pwrmon_rails),
593                                     GFP_KERNEL);
594         if (!svc->pwrmon_rails)
595                 goto err_pwrmon_debugfs_free;
596
597         if (gb_svc_pwrmon_rail_names_get(svc, svc->rail_names, bufsize))
598                 goto err_pwrmon_debugfs_free;
599
600         for (i = 0; i < svc->rail_count; i++) {
601                 struct dentry *dir;
602                 struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
603                 char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
604
605                 snprintf(fname, sizeof(fname), "%s",
606                          (char *)&svc->rail_names->name[i]);
607
608                 rail->id = i;
609                 rail->svc = svc;
610
611                 dir = debugfs_create_dir(fname, dent);
612                 debugfs_create_file("voltage_now", S_IRUGO, dir, rail,
613                                     &pwrmon_debugfs_voltage_fops);
614                 debugfs_create_file("current_now", S_IRUGO, dir, rail,
615                                     &pwrmon_debugfs_current_fops);
616                 debugfs_create_file("power_now", S_IRUGO, dir, rail,
617                                     &pwrmon_debugfs_power_fops);
618         };
619         return;
620
621 err_pwrmon_debugfs_free:
622         kfree(svc->rail_names);
623         svc->rail_names = NULL;
624
625         kfree(svc->pwrmon_rails);
626         svc->pwrmon_rails = NULL;
627
628 err_pwrmon_debugfs:
629         debugfs_remove(dent);
630 }
631
632 static void gb_svc_debugfs_init(struct gb_svc *svc)
633 {
634         svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
635                                                  gb_debugfs_get());
636         gb_svc_pwrmon_debugfs_init(svc);
637 }
638
639 static void gb_svc_debugfs_exit(struct gb_svc *svc)
640 {
641         debugfs_remove_recursive(svc->debugfs_dentry);
642         kfree(svc->rail_names);
643 }
644
645 static int gb_svc_hello(struct gb_operation *op)
646 {
647         struct gb_connection *connection = op->connection;
648         struct gb_svc *svc = gb_connection_get_data(connection);
649         struct gb_svc_hello_request *hello_request;
650         int ret;
651
652         if (op->request->payload_size < sizeof(*hello_request)) {
653                 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
654                                 op->request->payload_size,
655                                 sizeof(*hello_request));
656                 return -EINVAL;
657         }
658
659         hello_request = op->request->payload;
660         svc->endo_id = le16_to_cpu(hello_request->endo_id);
661         svc->ap_intf_id = hello_request->interface_id;
662
663         ret = device_add(&svc->dev);
664         if (ret) {
665                 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
666                 return ret;
667         }
668
669         ret = input_register_device(svc->input);
670         if (ret) {
671                 dev_err(&svc->dev, "failed to register input: %d\n", ret);
672                 device_del(&svc->dev);
673                 return ret;
674         }
675
676         ret = gb_svc_watchdog_create(svc);
677         if (ret) {
678                 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
679                 input_unregister_device(svc->input);
680                 device_del(&svc->dev);
681                 return ret;
682         }
683
684         gb_svc_debugfs_init(svc);
685
686         return 0;
687 }
688
689 static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
690 {
691         struct gb_host_device *hd = svc->hd;
692         struct gb_module *module;
693
694         list_for_each_entry(module, &hd->modules, hd_node) {
695                 if (module->module_id == module_id)
696                         return module;
697         }
698
699         return NULL;
700 }
701
702 static void gb_svc_intf_reenable(struct gb_svc *svc, struct gb_interface *intf)
703 {
704         int ret;
705
706         mutex_lock(&intf->mutex);
707
708         /* Mark as disconnected to prevent I/O during disable. */
709         intf->disconnected = true;
710         gb_interface_disable(intf);
711         intf->disconnected = false;
712
713         ret = gb_interface_enable(intf);
714         if (ret) {
715                 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
716                                 intf->interface_id, ret);
717
718                 gb_interface_deactivate(intf);
719         }
720
721         mutex_unlock(&intf->mutex);
722 }
723
724 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
725 {
726         struct gb_svc_intf_hotplug_request *request;
727         struct gb_connection *connection = operation->connection;
728         struct gb_svc *svc = gb_connection_get_data(connection);
729         struct gb_host_device *hd = connection->hd;
730         struct gb_module *module;
731         u8 intf_id;
732         int ret;
733
734         /* The request message size has already been verified. */
735         request = operation->request->payload;
736         intf_id = request->intf_id;
737
738         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
739
740         /* All modules are considered 1x2 for now */
741         module = gb_svc_module_lookup(svc, intf_id);
742         if (module) {
743                 dev_info(&svc->dev, "mode switch detected on interface %u\n",
744                                 intf_id);
745
746                 return gb_svc_intf_reenable(svc, module->interfaces[0]);
747         }
748
749         module = gb_module_create(hd, intf_id, 1);
750         if (!module) {
751                 dev_err(&svc->dev, "failed to create module\n");
752                 return;
753         }
754
755         ret = gb_module_add(module);
756         if (ret) {
757                 gb_module_put(module);
758                 return;
759         }
760
761         list_add(&module->hd_node, &hd->modules);
762 }
763
764 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
765 {
766         struct gb_svc *svc = gb_connection_get_data(operation->connection);
767         struct gb_svc_intf_hot_unplug_request *request;
768         struct gb_module *module;
769         u8 intf_id;
770
771         /* The request message size has already been verified. */
772         request = operation->request->payload;
773         intf_id = request->intf_id;
774
775         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
776
777         /* All modules are considered 1x2 for now */
778         module = gb_svc_module_lookup(svc, intf_id);
779         if (!module) {
780                 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
781                                 intf_id);
782                 return;
783         }
784
785         module->disconnected = true;
786
787         gb_module_del(module);
788         list_del(&module->hd_node);
789         gb_module_put(module);
790 }
791
792 static void gb_svc_process_deferred_request(struct work_struct *work)
793 {
794         struct gb_svc_deferred_request *dr;
795         struct gb_operation *operation;
796         struct gb_svc *svc;
797         u8 type;
798
799         dr = container_of(work, struct gb_svc_deferred_request, work);
800         operation = dr->operation;
801         svc = gb_connection_get_data(operation->connection);
802         type = operation->request->header->type;
803
804         switch (type) {
805         case GB_SVC_TYPE_INTF_HOTPLUG:
806                 gb_svc_process_intf_hotplug(operation);
807                 break;
808         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
809                 gb_svc_process_intf_hot_unplug(operation);
810                 break;
811         default:
812                 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
813         }
814
815         gb_operation_put(operation);
816         kfree(dr);
817 }
818
819 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
820 {
821         struct gb_svc *svc = gb_connection_get_data(operation->connection);
822         struct gb_svc_deferred_request *dr;
823
824         dr = kmalloc(sizeof(*dr), GFP_KERNEL);
825         if (!dr)
826                 return -ENOMEM;
827
828         gb_operation_get(operation);
829
830         dr->operation = operation;
831         INIT_WORK(&dr->work, gb_svc_process_deferred_request);
832
833         queue_work(svc->wq, &dr->work);
834
835         return 0;
836 }
837
838 /*
839  * Bringing up a module can be time consuming, as that may require lots of
840  * initialization on the module side. Over that, we may also need to download
841  * the firmware first and flash that on the module.
842  *
843  * In order not to make other svc events wait for all this to finish,
844  * handle most of module hotplug stuff outside of the hotplug callback, with
845  * help of a workqueue.
846  */
847 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
848 {
849         struct gb_svc *svc = gb_connection_get_data(op->connection);
850         struct gb_svc_intf_hotplug_request *request;
851
852         if (op->request->payload_size < sizeof(*request)) {
853                 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
854                                 op->request->payload_size, sizeof(*request));
855                 return -EINVAL;
856         }
857
858         request = op->request->payload;
859
860         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
861
862         return gb_svc_queue_deferred_request(op);
863 }
864
865 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
866 {
867         struct gb_svc *svc = gb_connection_get_data(op->connection);
868         struct gb_svc_intf_hot_unplug_request *request;
869
870         if (op->request->payload_size < sizeof(*request)) {
871                 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
872                                 op->request->payload_size, sizeof(*request));
873                 return -EINVAL;
874         }
875
876         request = op->request->payload;
877
878         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
879
880         return gb_svc_queue_deferred_request(op);
881 }
882
883 static int gb_svc_intf_reset_recv(struct gb_operation *op)
884 {
885         struct gb_svc *svc = gb_connection_get_data(op->connection);
886         struct gb_message *request = op->request;
887         struct gb_svc_intf_reset_request *reset;
888         u8 intf_id;
889
890         if (request->payload_size < sizeof(*reset)) {
891                 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
892                                 request->payload_size, sizeof(*reset));
893                 return -EINVAL;
894         }
895         reset = request->payload;
896
897         intf_id = reset->intf_id;
898
899         /* FIXME Reset the interface here */
900
901         return 0;
902 }
903
904 static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
905 {
906         switch (key_code) {
907         case GB_KEYCODE_ARA:
908                 *code = SVC_KEY_ARA_BUTTON;
909                 break;
910         default:
911                 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
912                 return -EINVAL;
913         }
914
915         return 0;
916 }
917
918 static int gb_svc_key_event_recv(struct gb_operation *op)
919 {
920         struct gb_svc *svc = gb_connection_get_data(op->connection);
921         struct gb_message *request = op->request;
922         struct gb_svc_key_event_request *key;
923         u16 code;
924         u8 event;
925         int ret;
926
927         if (request->payload_size < sizeof(*key)) {
928                 dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
929                          request->payload_size, sizeof(*key));
930                 return -EINVAL;
931         }
932
933         key = request->payload;
934
935         ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
936         if (ret < 0)
937                 return ret;
938
939         event = key->key_event;
940         if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
941                 dev_warn(&svc->dev, "unknown key event received: %u\n", event);
942                 return -EINVAL;
943         }
944
945         input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
946         input_sync(svc->input);
947
948         return 0;
949 }
950
951 static int gb_svc_request_handler(struct gb_operation *op)
952 {
953         struct gb_connection *connection = op->connection;
954         struct gb_svc *svc = gb_connection_get_data(connection);
955         u8 type = op->type;
956         int ret = 0;
957
958         /*
959          * SVC requests need to follow a specific order (at least initially) and
960          * below code takes care of enforcing that. The expected order is:
961          * - PROTOCOL_VERSION
962          * - SVC_HELLO
963          * - Any other request, but the earlier two.
964          *
965          * Incoming requests are guaranteed to be serialized and so we don't
966          * need to protect 'state' for any races.
967          */
968         switch (type) {
969         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
970                 if (svc->state != GB_SVC_STATE_RESET)
971                         ret = -EINVAL;
972                 break;
973         case GB_SVC_TYPE_SVC_HELLO:
974                 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
975                         ret = -EINVAL;
976                 break;
977         default:
978                 if (svc->state != GB_SVC_STATE_SVC_HELLO)
979                         ret = -EINVAL;
980                 break;
981         }
982
983         if (ret) {
984                 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
985                                 type, svc->state);
986                 return ret;
987         }
988
989         switch (type) {
990         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
991                 ret = gb_svc_version_request(op);
992                 if (!ret)
993                         svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
994                 return ret;
995         case GB_SVC_TYPE_SVC_HELLO:
996                 ret = gb_svc_hello(op);
997                 if (!ret)
998                         svc->state = GB_SVC_STATE_SVC_HELLO;
999                 return ret;
1000         case GB_SVC_TYPE_INTF_HOTPLUG:
1001                 return gb_svc_intf_hotplug_recv(op);
1002         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
1003                 return gb_svc_intf_hot_unplug_recv(op);
1004         case GB_SVC_TYPE_INTF_RESET:
1005                 return gb_svc_intf_reset_recv(op);
1006         case GB_SVC_TYPE_KEY_EVENT:
1007                 return gb_svc_key_event_recv(op);
1008         default:
1009                 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
1010                 return -EINVAL;
1011         }
1012 }
1013
1014 static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
1015 {
1016         struct input_dev *input_dev;
1017
1018         input_dev = input_allocate_device();
1019         if (!input_dev)
1020                 return ERR_PTR(-ENOMEM);
1021
1022         input_dev->name = dev_name(&svc->dev);
1023         svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
1024                                     input_dev->name);
1025         if (!svc->input_phys)
1026                 goto err_free_input;
1027
1028         input_dev->phys = svc->input_phys;
1029         input_dev->dev.parent = &svc->dev;
1030
1031         input_set_drvdata(input_dev, svc);
1032
1033         input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
1034
1035         return input_dev;
1036
1037 err_free_input:
1038         input_free_device(svc->input);
1039         return ERR_PTR(-ENOMEM);
1040 }
1041
1042 static void gb_svc_release(struct device *dev)
1043 {
1044         struct gb_svc *svc = to_gb_svc(dev);
1045
1046         if (svc->connection)
1047                 gb_connection_destroy(svc->connection);
1048         ida_destroy(&svc->device_id_map);
1049         destroy_workqueue(svc->wq);
1050         kfree(svc->input_phys);
1051         kfree(svc);
1052 }
1053
1054 struct device_type greybus_svc_type = {
1055         .name           = "greybus_svc",
1056         .release        = gb_svc_release,
1057 };
1058
1059 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
1060 {
1061         struct gb_svc *svc;
1062
1063         svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1064         if (!svc)
1065                 return NULL;
1066
1067         svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
1068         if (!svc->wq) {
1069                 kfree(svc);
1070                 return NULL;
1071         }
1072
1073         svc->dev.parent = &hd->dev;
1074         svc->dev.bus = &greybus_bus_type;
1075         svc->dev.type = &greybus_svc_type;
1076         svc->dev.groups = svc_groups;
1077         svc->dev.dma_mask = svc->dev.parent->dma_mask;
1078         device_initialize(&svc->dev);
1079
1080         dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
1081
1082         ida_init(&svc->device_id_map);
1083         svc->state = GB_SVC_STATE_RESET;
1084         svc->hd = hd;
1085
1086         svc->input = gb_svc_input_create(svc);
1087         if (IS_ERR(svc->input)) {
1088                 dev_err(&svc->dev, "failed to create input device: %ld\n",
1089                         PTR_ERR(svc->input));
1090                 goto err_put_device;
1091         }
1092
1093         svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
1094                                                 gb_svc_request_handler);
1095         if (IS_ERR(svc->connection)) {
1096                 dev_err(&svc->dev, "failed to create connection: %ld\n",
1097                                 PTR_ERR(svc->connection));
1098                 goto err_free_input;
1099         }
1100
1101         gb_connection_set_data(svc->connection, svc);
1102
1103         return svc;
1104
1105 err_free_input:
1106         input_free_device(svc->input);
1107 err_put_device:
1108         put_device(&svc->dev);
1109         return NULL;
1110 }
1111
1112 int gb_svc_add(struct gb_svc *svc)
1113 {
1114         int ret;
1115
1116         /*
1117          * The SVC protocol is currently driven by the SVC, so the SVC device
1118          * is added from the connection request handler when enough
1119          * information has been received.
1120          */
1121         ret = gb_connection_enable(svc->connection);
1122         if (ret)
1123                 return ret;
1124
1125         return 0;
1126 }
1127
1128 static void gb_svc_remove_modules(struct gb_svc *svc)
1129 {
1130         struct gb_host_device *hd = svc->hd;
1131         struct gb_module *module, *tmp;
1132
1133         list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) {
1134                 gb_module_del(module);
1135                 list_del(&module->hd_node);
1136                 gb_module_put(module);
1137         }
1138 }
1139
1140 void gb_svc_del(struct gb_svc *svc)
1141 {
1142         gb_connection_disable(svc->connection);
1143
1144         /*
1145          * The SVC device and input device may have been registered
1146          * from the request handler.
1147          */
1148         if (device_is_registered(&svc->dev)) {
1149                 gb_svc_debugfs_exit(svc);
1150                 gb_svc_watchdog_destroy(svc);
1151                 input_unregister_device(svc->input);
1152                 device_del(&svc->dev);
1153         }
1154
1155         flush_workqueue(svc->wq);
1156
1157         gb_svc_remove_modules(svc);
1158 }
1159
1160 void gb_svc_put(struct gb_svc *svc)
1161 {
1162         put_device(&svc->dev);
1163 }