greybus: core: add module abstraction
[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_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
257                         u32 *value)
258 {
259         struct gb_svc_dme_peer_get_request request;
260         struct gb_svc_dme_peer_get_response response;
261         u16 result;
262         int ret;
263
264         request.intf_id = intf_id;
265         request.attr = cpu_to_le16(attr);
266         request.selector = cpu_to_le16(selector);
267
268         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
269                                 &request, sizeof(request),
270                                 &response, sizeof(response));
271         if (ret) {
272                 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
273                                 intf_id, attr, selector, ret);
274                 return ret;
275         }
276
277         result = le16_to_cpu(response.result_code);
278         if (result) {
279                 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
280                                 intf_id, attr, selector, result);
281                 return -EIO;
282         }
283
284         if (value)
285                 *value = le32_to_cpu(response.attr_value);
286
287         return 0;
288 }
289 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
290
291 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
292                         u32 value)
293 {
294         struct gb_svc_dme_peer_set_request request;
295         struct gb_svc_dme_peer_set_response response;
296         u16 result;
297         int ret;
298
299         request.intf_id = intf_id;
300         request.attr = cpu_to_le16(attr);
301         request.selector = cpu_to_le16(selector);
302         request.value = cpu_to_le32(value);
303
304         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
305                                 &request, sizeof(request),
306                                 &response, sizeof(response));
307         if (ret) {
308                 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
309                                 intf_id, attr, selector, value, ret);
310                 return ret;
311         }
312
313         result = le16_to_cpu(response.result_code);
314         if (result) {
315                 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
316                                 intf_id, attr, selector, value, result);
317                 return -EIO;
318         }
319
320         return 0;
321 }
322 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
323
324 int gb_svc_connection_create(struct gb_svc *svc,
325                                 u8 intf1_id, u16 cport1_id,
326                                 u8 intf2_id, u16 cport2_id,
327                                 u8 cport_flags)
328 {
329         struct gb_svc_conn_create_request request;
330
331         request.intf1_id = intf1_id;
332         request.cport1_id = cpu_to_le16(cport1_id);
333         request.intf2_id = intf2_id;
334         request.cport2_id = cpu_to_le16(cport2_id);
335         request.tc = 0;         /* TC0 */
336         request.flags = cport_flags;
337
338         return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
339                                  &request, sizeof(request), NULL, 0);
340 }
341 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
342
343 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
344                                u8 intf2_id, u16 cport2_id)
345 {
346         struct gb_svc_conn_destroy_request request;
347         struct gb_connection *connection = svc->connection;
348         int ret;
349
350         request.intf1_id = intf1_id;
351         request.cport1_id = cpu_to_le16(cport1_id);
352         request.intf2_id = intf2_id;
353         request.cport2_id = cpu_to_le16(cport2_id);
354
355         ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
356                                 &request, sizeof(request), NULL, 0);
357         if (ret) {
358                 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
359                                 intf1_id, cport1_id, intf2_id, cport2_id, ret);
360         }
361 }
362 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
363
364 /* Creates bi-directional routes between the devices */
365 int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
366                                u8 intf2_id, u8 dev2_id)
367 {
368         struct gb_svc_route_create_request request;
369
370         request.intf1_id = intf1_id;
371         request.dev1_id = dev1_id;
372         request.intf2_id = intf2_id;
373         request.dev2_id = dev2_id;
374
375         return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
376                                  &request, sizeof(request), NULL, 0);
377 }
378
379 /* Destroys bi-directional routes between the devices */
380 void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
381 {
382         struct gb_svc_route_destroy_request request;
383         int ret;
384
385         request.intf1_id = intf1_id;
386         request.intf2_id = intf2_id;
387
388         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
389                                 &request, sizeof(request), NULL, 0);
390         if (ret) {
391                 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
392                                 intf1_id, intf2_id, ret);
393         }
394 }
395
396 int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
397                                u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
398                                u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
399                                u8 flags, u32 quirks)
400 {
401         struct gb_svc_intf_set_pwrm_request request;
402         struct gb_svc_intf_set_pwrm_response response;
403         int ret;
404
405         request.intf_id = intf_id;
406         request.hs_series = hs_series;
407         request.tx_mode = tx_mode;
408         request.tx_gear = tx_gear;
409         request.tx_nlanes = tx_nlanes;
410         request.rx_mode = rx_mode;
411         request.rx_gear = rx_gear;
412         request.rx_nlanes = rx_nlanes;
413         request.flags = flags;
414         request.quirks = cpu_to_le32(quirks);
415
416         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
417                                 &request, sizeof(request),
418                                 &response, sizeof(response));
419         if (ret < 0)
420                 return ret;
421
422         return le16_to_cpu(response.result_code);
423 }
424 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
425
426 int gb_svc_ping(struct gb_svc *svc)
427 {
428         return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
429                                          NULL, 0, NULL, 0,
430                                          GB_OPERATION_TIMEOUT_DEFAULT * 2);
431 }
432 EXPORT_SYMBOL_GPL(gb_svc_ping);
433
434 static int gb_svc_version_request(struct gb_operation *op)
435 {
436         struct gb_connection *connection = op->connection;
437         struct gb_svc *svc = gb_connection_get_data(connection);
438         struct gb_protocol_version_request *request;
439         struct gb_protocol_version_response *response;
440
441         if (op->request->payload_size < sizeof(*request)) {
442                 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
443                                 op->request->payload_size,
444                                 sizeof(*request));
445                 return -EINVAL;
446         }
447
448         request = op->request->payload;
449
450         if (request->major > GB_SVC_VERSION_MAJOR) {
451                 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
452                                 request->major, GB_SVC_VERSION_MAJOR);
453                 return -ENOTSUPP;
454         }
455
456         svc->protocol_major = request->major;
457         svc->protocol_minor = request->minor;
458
459         if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
460                 return -ENOMEM;
461
462         response = op->response->payload;
463         response->major = svc->protocol_major;
464         response->minor = svc->protocol_minor;
465
466         return 0;
467 }
468
469 static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
470                                         size_t len, loff_t *offset)
471 {
472         struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
473         struct gb_svc *svc = pwrmon_rails->svc;
474         int ret, desc;
475         u32 value;
476         char buff[16];
477
478         ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
479                                        GB_SVC_PWRMON_TYPE_VOL, &value);
480         if (ret) {
481                 dev_err(&svc->dev,
482                         "failed to get voltage sample %u: %d\n",
483                         pwrmon_rails->id, ret);
484                 return ret;
485         }
486
487         desc = scnprintf(buff, sizeof(buff), "%u\n", value);
488
489         return simple_read_from_buffer(buf, len, offset, buff, desc);
490 }
491
492 static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
493                                         size_t len, loff_t *offset)
494 {
495         struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
496         struct gb_svc *svc = pwrmon_rails->svc;
497         int ret, desc;
498         u32 value;
499         char buff[16];
500
501         ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
502                                        GB_SVC_PWRMON_TYPE_CURR, &value);
503         if (ret) {
504                 dev_err(&svc->dev,
505                         "failed to get current sample %u: %d\n",
506                         pwrmon_rails->id, ret);
507                 return ret;
508         }
509
510         desc = scnprintf(buff, sizeof(buff), "%u\n", value);
511
512         return simple_read_from_buffer(buf, len, offset, buff, desc);
513 }
514
515 static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
516                                       size_t len, loff_t *offset)
517 {
518         struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
519         struct gb_svc *svc = pwrmon_rails->svc;
520         int ret, desc;
521         u32 value;
522         char buff[16];
523
524         ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
525                                        GB_SVC_PWRMON_TYPE_PWR, &value);
526         if (ret) {
527                 dev_err(&svc->dev, "failed to get power sample %u: %d\n",
528                         pwrmon_rails->id, ret);
529                 return ret;
530         }
531
532         desc = scnprintf(buff, sizeof(buff), "%u\n", value);
533
534         return simple_read_from_buffer(buf, len, offset, buff, desc);
535 }
536
537 static const struct file_operations pwrmon_debugfs_voltage_fops = {
538         .read           = pwr_debugfs_voltage_read,
539 };
540
541 static const struct file_operations pwrmon_debugfs_current_fops = {
542         .read           = pwr_debugfs_current_read,
543 };
544
545 static const struct file_operations pwrmon_debugfs_power_fops = {
546         .read           = pwr_debugfs_power_read,
547 };
548
549 static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
550 {
551         int i;
552         size_t bufsize;
553         struct dentry *dent;
554
555         dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
556         if (IS_ERR_OR_NULL(dent))
557                 return;
558
559         if (gb_svc_pwrmon_rail_count_get(svc, &svc->rail_count))
560                 goto err_pwrmon_debugfs;
561
562         if (!svc->rail_count || svc->rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
563                 goto err_pwrmon_debugfs;
564
565         bufsize = GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * svc->rail_count;
566
567         svc->rail_names = kzalloc(bufsize, GFP_KERNEL);
568         if (!svc->rail_names)
569                 goto err_pwrmon_debugfs;
570
571         svc->pwrmon_rails = kcalloc(svc->rail_count, sizeof(*svc->pwrmon_rails),
572                                     GFP_KERNEL);
573         if (!svc->pwrmon_rails)
574                 goto err_pwrmon_debugfs_free;
575
576         if (gb_svc_pwrmon_rail_names_get(svc, svc->rail_names, bufsize))
577                 goto err_pwrmon_debugfs_free;
578
579         for (i = 0; i < svc->rail_count; i++) {
580                 struct dentry *dir;
581                 struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
582                 char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
583
584                 snprintf(fname, sizeof(fname), "%s",
585                          (char *)&svc->rail_names->name[i]);
586
587                 rail->id = i;
588                 rail->svc = svc;
589
590                 dir = debugfs_create_dir(fname, dent);
591                 debugfs_create_file("voltage_now", S_IRUGO, dir, rail,
592                                     &pwrmon_debugfs_voltage_fops);
593                 debugfs_create_file("current_now", S_IRUGO, dir, rail,
594                                     &pwrmon_debugfs_current_fops);
595                 debugfs_create_file("power_now", S_IRUGO, dir, rail,
596                                     &pwrmon_debugfs_power_fops);
597         };
598         return;
599
600 err_pwrmon_debugfs_free:
601         kfree(svc->rail_names);
602         svc->rail_names = NULL;
603
604         kfree(svc->pwrmon_rails);
605         svc->pwrmon_rails = NULL;
606
607 err_pwrmon_debugfs:
608         debugfs_remove(dent);
609 }
610
611 static void gb_svc_debugfs_init(struct gb_svc *svc)
612 {
613         svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
614                                                  gb_debugfs_get());
615         gb_svc_pwrmon_debugfs_init(svc);
616 }
617
618 static void gb_svc_debugfs_exit(struct gb_svc *svc)
619 {
620         debugfs_remove_recursive(svc->debugfs_dentry);
621         kfree(svc->rail_names);
622 }
623
624 static int gb_svc_hello(struct gb_operation *op)
625 {
626         struct gb_connection *connection = op->connection;
627         struct gb_svc *svc = gb_connection_get_data(connection);
628         struct gb_svc_hello_request *hello_request;
629         int ret;
630
631         if (op->request->payload_size < sizeof(*hello_request)) {
632                 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
633                                 op->request->payload_size,
634                                 sizeof(*hello_request));
635                 return -EINVAL;
636         }
637
638         hello_request = op->request->payload;
639         svc->endo_id = le16_to_cpu(hello_request->endo_id);
640         svc->ap_intf_id = hello_request->interface_id;
641
642         ret = device_add(&svc->dev);
643         if (ret) {
644                 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
645                 return ret;
646         }
647
648         ret = input_register_device(svc->input);
649         if (ret) {
650                 dev_err(&svc->dev, "failed to register input: %d\n", ret);
651                 device_del(&svc->dev);
652                 return ret;
653         }
654
655         ret = gb_svc_watchdog_create(svc);
656         if (ret) {
657                 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
658                 input_unregister_device(svc->input);
659                 device_del(&svc->dev);
660                 return ret;
661         }
662
663         gb_svc_debugfs_init(svc);
664
665         return 0;
666 }
667
668 static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
669 {
670         struct gb_host_device *hd = svc->hd;
671         struct gb_module *module;
672
673         list_for_each_entry(module, &hd->modules, hd_node) {
674                 if (module->module_id == module_id)
675                         return module;
676         }
677
678         return NULL;
679 }
680
681 static void gb_svc_intf_reenable(struct gb_svc *svc, struct gb_interface *intf)
682 {
683         int ret;
684
685         /* Mark as disconnected to prevent I/O during disable. */
686         intf->disconnected = true;
687         gb_interface_disable(intf);
688         intf->disconnected = false;
689
690         ret = gb_interface_enable(intf);
691         if (ret) {
692                 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
693                                 intf->interface_id, ret);
694
695                 gb_interface_deactivate(intf);
696         }
697 }
698
699 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
700 {
701         struct gb_svc_intf_hotplug_request *request;
702         struct gb_connection *connection = operation->connection;
703         struct gb_svc *svc = gb_connection_get_data(connection);
704         struct gb_host_device *hd = connection->hd;
705         struct gb_module *module;
706         u8 intf_id;
707         int ret;
708
709         /* The request message size has already been verified. */
710         request = operation->request->payload;
711         intf_id = request->intf_id;
712
713         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
714
715         /* All modules are considered 1x2 for now */
716         module = gb_svc_module_lookup(svc, intf_id);
717         if (module) {
718                 dev_info(&svc->dev, "mode switch detected on interface %u\n",
719                                 intf_id);
720
721                 return gb_svc_intf_reenable(svc, module->interfaces[0]);
722         }
723
724         module = gb_module_create(hd, intf_id, 1);
725         if (!module) {
726                 dev_err(&svc->dev, "failed to create module\n");
727                 return;
728         }
729
730         ret = gb_module_add(module);
731         if (ret) {
732                 gb_module_put(module);
733                 return;
734         }
735
736         list_add(&module->hd_node, &hd->modules);
737 }
738
739 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
740 {
741         struct gb_svc *svc = gb_connection_get_data(operation->connection);
742         struct gb_svc_intf_hot_unplug_request *request;
743         struct gb_module *module;
744         u8 intf_id;
745
746         /* The request message size has already been verified. */
747         request = operation->request->payload;
748         intf_id = request->intf_id;
749
750         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
751
752         /* All modules are considered 1x2 for now */
753         module = gb_svc_module_lookup(svc, intf_id);
754         if (!module) {
755                 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
756                                 intf_id);
757                 return;
758         }
759
760         module->disconnected = true;
761
762         gb_module_del(module);
763         list_del(&module->hd_node);
764         gb_module_put(module);
765 }
766
767 static void gb_svc_process_deferred_request(struct work_struct *work)
768 {
769         struct gb_svc_deferred_request *dr;
770         struct gb_operation *operation;
771         struct gb_svc *svc;
772         u8 type;
773
774         dr = container_of(work, struct gb_svc_deferred_request, work);
775         operation = dr->operation;
776         svc = gb_connection_get_data(operation->connection);
777         type = operation->request->header->type;
778
779         switch (type) {
780         case GB_SVC_TYPE_INTF_HOTPLUG:
781                 gb_svc_process_intf_hotplug(operation);
782                 break;
783         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
784                 gb_svc_process_intf_hot_unplug(operation);
785                 break;
786         default:
787                 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
788         }
789
790         gb_operation_put(operation);
791         kfree(dr);
792 }
793
794 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
795 {
796         struct gb_svc *svc = gb_connection_get_data(operation->connection);
797         struct gb_svc_deferred_request *dr;
798
799         dr = kmalloc(sizeof(*dr), GFP_KERNEL);
800         if (!dr)
801                 return -ENOMEM;
802
803         gb_operation_get(operation);
804
805         dr->operation = operation;
806         INIT_WORK(&dr->work, gb_svc_process_deferred_request);
807
808         queue_work(svc->wq, &dr->work);
809
810         return 0;
811 }
812
813 /*
814  * Bringing up a module can be time consuming, as that may require lots of
815  * initialization on the module side. Over that, we may also need to download
816  * the firmware first and flash that on the module.
817  *
818  * In order not to make other svc events wait for all this to finish,
819  * handle most of module hotplug stuff outside of the hotplug callback, with
820  * help of a workqueue.
821  */
822 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
823 {
824         struct gb_svc *svc = gb_connection_get_data(op->connection);
825         struct gb_svc_intf_hotplug_request *request;
826
827         if (op->request->payload_size < sizeof(*request)) {
828                 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
829                                 op->request->payload_size, sizeof(*request));
830                 return -EINVAL;
831         }
832
833         request = op->request->payload;
834
835         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
836
837         return gb_svc_queue_deferred_request(op);
838 }
839
840 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
841 {
842         struct gb_svc *svc = gb_connection_get_data(op->connection);
843         struct gb_svc_intf_hot_unplug_request *request;
844
845         if (op->request->payload_size < sizeof(*request)) {
846                 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
847                                 op->request->payload_size, sizeof(*request));
848                 return -EINVAL;
849         }
850
851         request = op->request->payload;
852
853         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
854
855         return gb_svc_queue_deferred_request(op);
856 }
857
858 static int gb_svc_intf_reset_recv(struct gb_operation *op)
859 {
860         struct gb_svc *svc = gb_connection_get_data(op->connection);
861         struct gb_message *request = op->request;
862         struct gb_svc_intf_reset_request *reset;
863         u8 intf_id;
864
865         if (request->payload_size < sizeof(*reset)) {
866                 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
867                                 request->payload_size, sizeof(*reset));
868                 return -EINVAL;
869         }
870         reset = request->payload;
871
872         intf_id = reset->intf_id;
873
874         /* FIXME Reset the interface here */
875
876         return 0;
877 }
878
879 static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
880 {
881         switch (key_code) {
882         case GB_KEYCODE_ARA:
883                 *code = SVC_KEY_ARA_BUTTON;
884                 break;
885         default:
886                 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
887                 return -EINVAL;
888         }
889
890         return 0;
891 }
892
893 static int gb_svc_key_event_recv(struct gb_operation *op)
894 {
895         struct gb_svc *svc = gb_connection_get_data(op->connection);
896         struct gb_message *request = op->request;
897         struct gb_svc_key_event_request *key;
898         u16 code;
899         u8 event;
900         int ret;
901
902         if (request->payload_size < sizeof(*key)) {
903                 dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
904                          request->payload_size, sizeof(*key));
905                 return -EINVAL;
906         }
907
908         key = request->payload;
909
910         ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
911         if (ret < 0)
912                 return ret;
913
914         event = key->key_event;
915         if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
916                 dev_warn(&svc->dev, "unknown key event received: %u\n", event);
917                 return -EINVAL;
918         }
919
920         input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
921         input_sync(svc->input);
922
923         return 0;
924 }
925
926 static int gb_svc_request_handler(struct gb_operation *op)
927 {
928         struct gb_connection *connection = op->connection;
929         struct gb_svc *svc = gb_connection_get_data(connection);
930         u8 type = op->type;
931         int ret = 0;
932
933         /*
934          * SVC requests need to follow a specific order (at least initially) and
935          * below code takes care of enforcing that. The expected order is:
936          * - PROTOCOL_VERSION
937          * - SVC_HELLO
938          * - Any other request, but the earlier two.
939          *
940          * Incoming requests are guaranteed to be serialized and so we don't
941          * need to protect 'state' for any races.
942          */
943         switch (type) {
944         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
945                 if (svc->state != GB_SVC_STATE_RESET)
946                         ret = -EINVAL;
947                 break;
948         case GB_SVC_TYPE_SVC_HELLO:
949                 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
950                         ret = -EINVAL;
951                 break;
952         default:
953                 if (svc->state != GB_SVC_STATE_SVC_HELLO)
954                         ret = -EINVAL;
955                 break;
956         }
957
958         if (ret) {
959                 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
960                                 type, svc->state);
961                 return ret;
962         }
963
964         switch (type) {
965         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
966                 ret = gb_svc_version_request(op);
967                 if (!ret)
968                         svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
969                 return ret;
970         case GB_SVC_TYPE_SVC_HELLO:
971                 ret = gb_svc_hello(op);
972                 if (!ret)
973                         svc->state = GB_SVC_STATE_SVC_HELLO;
974                 return ret;
975         case GB_SVC_TYPE_INTF_HOTPLUG:
976                 return gb_svc_intf_hotplug_recv(op);
977         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
978                 return gb_svc_intf_hot_unplug_recv(op);
979         case GB_SVC_TYPE_INTF_RESET:
980                 return gb_svc_intf_reset_recv(op);
981         case GB_SVC_TYPE_KEY_EVENT:
982                 return gb_svc_key_event_recv(op);
983         default:
984                 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
985                 return -EINVAL;
986         }
987 }
988
989 static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
990 {
991         struct input_dev *input_dev;
992
993         input_dev = input_allocate_device();
994         if (!input_dev)
995                 return ERR_PTR(-ENOMEM);
996
997         input_dev->name = dev_name(&svc->dev);
998         svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
999                                     input_dev->name);
1000         if (!svc->input_phys)
1001                 goto err_free_input;
1002
1003         input_dev->phys = svc->input_phys;
1004         input_dev->dev.parent = &svc->dev;
1005
1006         input_set_drvdata(input_dev, svc);
1007
1008         input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
1009
1010         return input_dev;
1011
1012 err_free_input:
1013         input_free_device(svc->input);
1014         return ERR_PTR(-ENOMEM);
1015 }
1016
1017 static void gb_svc_release(struct device *dev)
1018 {
1019         struct gb_svc *svc = to_gb_svc(dev);
1020
1021         if (svc->connection)
1022                 gb_connection_destroy(svc->connection);
1023         ida_destroy(&svc->device_id_map);
1024         destroy_workqueue(svc->wq);
1025         kfree(svc->input_phys);
1026         kfree(svc);
1027 }
1028
1029 struct device_type greybus_svc_type = {
1030         .name           = "greybus_svc",
1031         .release        = gb_svc_release,
1032 };
1033
1034 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
1035 {
1036         struct gb_svc *svc;
1037
1038         svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1039         if (!svc)
1040                 return NULL;
1041
1042         svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
1043         if (!svc->wq) {
1044                 kfree(svc);
1045                 return NULL;
1046         }
1047
1048         svc->dev.parent = &hd->dev;
1049         svc->dev.bus = &greybus_bus_type;
1050         svc->dev.type = &greybus_svc_type;
1051         svc->dev.groups = svc_groups;
1052         svc->dev.dma_mask = svc->dev.parent->dma_mask;
1053         device_initialize(&svc->dev);
1054
1055         dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
1056
1057         ida_init(&svc->device_id_map);
1058         svc->state = GB_SVC_STATE_RESET;
1059         svc->hd = hd;
1060
1061         svc->input = gb_svc_input_create(svc);
1062         if (IS_ERR(svc->input)) {
1063                 dev_err(&svc->dev, "failed to create input device: %ld\n",
1064                         PTR_ERR(svc->input));
1065                 goto err_put_device;
1066         }
1067
1068         svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
1069                                                 gb_svc_request_handler);
1070         if (IS_ERR(svc->connection)) {
1071                 dev_err(&svc->dev, "failed to create connection: %ld\n",
1072                                 PTR_ERR(svc->connection));
1073                 goto err_free_input;
1074         }
1075
1076         gb_connection_set_data(svc->connection, svc);
1077
1078         return svc;
1079
1080 err_free_input:
1081         input_free_device(svc->input);
1082 err_put_device:
1083         put_device(&svc->dev);
1084         return NULL;
1085 }
1086
1087 int gb_svc_add(struct gb_svc *svc)
1088 {
1089         int ret;
1090
1091         /*
1092          * The SVC protocol is currently driven by the SVC, so the SVC device
1093          * is added from the connection request handler when enough
1094          * information has been received.
1095          */
1096         ret = gb_connection_enable(svc->connection);
1097         if (ret)
1098                 return ret;
1099
1100         return 0;
1101 }
1102
1103 static void gb_svc_remove_modules(struct gb_svc *svc)
1104 {
1105         struct gb_host_device *hd = svc->hd;
1106         struct gb_module *module, *tmp;
1107
1108         list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) {
1109                 gb_module_del(module);
1110                 list_del(&module->hd_node);
1111                 gb_module_put(module);
1112         }
1113 }
1114
1115 void gb_svc_del(struct gb_svc *svc)
1116 {
1117         gb_connection_disable(svc->connection);
1118
1119         /*
1120          * The SVC device and input device may have been registered
1121          * from the request handler.
1122          */
1123         if (device_is_registered(&svc->dev)) {
1124                 gb_svc_debugfs_exit(svc);
1125                 gb_svc_watchdog_destroy(svc);
1126                 input_unregister_device(svc->input);
1127                 device_del(&svc->dev);
1128         }
1129
1130         flush_workqueue(svc->wq);
1131
1132         gb_svc_remove_modules(svc);
1133 }
1134
1135 void gb_svc_put(struct gb_svc *svc)
1136 {
1137         put_device(&svc->dev);
1138 }