]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/svc.c
d285361cc5218ce05967e9da4d4e16a52b5b545e
[karo-tx-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 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 svc_debugfs_init(struct gb_svc *svc)
612 {
613         svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
614                                                  gb_debugfs_get());
615         svc_pwrmon_debugfs_init(svc);
616 }
617
618 static void 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         svc_debugfs_init(svc);
664
665         return 0;
666 }
667
668 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
669 {
670         struct gb_svc_intf_hotplug_request *request;
671         struct gb_connection *connection = operation->connection;
672         struct gb_svc *svc = gb_connection_get_data(connection);
673         struct gb_host_device *hd = connection->hd;
674         struct gb_interface *intf;
675         u8 intf_id;
676         int ret;
677
678         /* The request message size has already been verified. */
679         request = operation->request->payload;
680         intf_id = request->intf_id;
681
682         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
683
684         intf = gb_interface_find(hd, intf_id);
685         if (intf) {
686                 dev_info(&svc->dev, "mode switch detected on interface %u\n",
687                                 intf_id);
688
689                 /* Mark as disconnected to prevent I/O during disable. */
690                 intf->disconnected = true;
691                 gb_interface_disable(intf);
692                 intf->disconnected = false;
693
694                 goto enable_interface;
695         }
696
697         intf = gb_interface_create(hd, intf_id);
698         if (!intf) {
699                 dev_err(&svc->dev, "failed to create interface %u\n",
700                                 intf_id);
701                 return;
702         }
703
704         ret = gb_interface_activate(intf);
705         if (ret) {
706                 dev_err(&svc->dev, "failed to activate interface %u: %d\n",
707                                 intf_id, ret);
708                 gb_interface_add(intf);
709                 return;
710         }
711
712         ret = gb_interface_add(intf);
713         if (ret)
714                 goto err_interface_deactivate;
715
716 enable_interface:
717         ret = gb_interface_enable(intf);
718         if (ret) {
719                 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
720                                 intf_id, ret);
721                 goto err_interface_deactivate;
722         }
723
724         return;
725
726 err_interface_deactivate:
727         gb_interface_deactivate(intf);
728 }
729
730 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
731 {
732         struct gb_svc *svc = gb_connection_get_data(operation->connection);
733         struct gb_svc_intf_hot_unplug_request *request;
734         struct gb_host_device *hd = operation->connection->hd;
735         struct gb_interface *intf;
736         u8 intf_id;
737
738         /* The request message size has already been verified. */
739         request = operation->request->payload;
740         intf_id = request->intf_id;
741
742         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
743
744         intf = gb_interface_find(hd, intf_id);
745         if (!intf) {
746                 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
747                                 intf_id);
748                 return;
749         }
750
751         /* Mark as disconnected to prevent I/O during disable. */
752         intf->disconnected = true;
753
754         gb_interface_disable(intf);
755         gb_interface_deactivate(intf);
756         gb_interface_remove(intf);
757 }
758
759 static void gb_svc_process_deferred_request(struct work_struct *work)
760 {
761         struct gb_svc_deferred_request *dr;
762         struct gb_operation *operation;
763         struct gb_svc *svc;
764         u8 type;
765
766         dr = container_of(work, struct gb_svc_deferred_request, work);
767         operation = dr->operation;
768         svc = gb_connection_get_data(operation->connection);
769         type = operation->request->header->type;
770
771         switch (type) {
772         case GB_SVC_TYPE_INTF_HOTPLUG:
773                 gb_svc_process_intf_hotplug(operation);
774                 break;
775         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
776                 gb_svc_process_intf_hot_unplug(operation);
777                 break;
778         default:
779                 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
780         }
781
782         gb_operation_put(operation);
783         kfree(dr);
784 }
785
786 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
787 {
788         struct gb_svc *svc = gb_connection_get_data(operation->connection);
789         struct gb_svc_deferred_request *dr;
790
791         dr = kmalloc(sizeof(*dr), GFP_KERNEL);
792         if (!dr)
793                 return -ENOMEM;
794
795         gb_operation_get(operation);
796
797         dr->operation = operation;
798         INIT_WORK(&dr->work, gb_svc_process_deferred_request);
799
800         queue_work(svc->wq, &dr->work);
801
802         return 0;
803 }
804
805 /*
806  * Bringing up a module can be time consuming, as that may require lots of
807  * initialization on the module side. Over that, we may also need to download
808  * the firmware first and flash that on the module.
809  *
810  * In order not to make other svc events wait for all this to finish,
811  * handle most of module hotplug stuff outside of the hotplug callback, with
812  * help of a workqueue.
813  */
814 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
815 {
816         struct gb_svc *svc = gb_connection_get_data(op->connection);
817         struct gb_svc_intf_hotplug_request *request;
818
819         if (op->request->payload_size < sizeof(*request)) {
820                 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
821                                 op->request->payload_size, sizeof(*request));
822                 return -EINVAL;
823         }
824
825         request = op->request->payload;
826
827         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
828
829         return gb_svc_queue_deferred_request(op);
830 }
831
832 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
833 {
834         struct gb_svc *svc = gb_connection_get_data(op->connection);
835         struct gb_svc_intf_hot_unplug_request *request;
836
837         if (op->request->payload_size < sizeof(*request)) {
838                 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
839                                 op->request->payload_size, sizeof(*request));
840                 return -EINVAL;
841         }
842
843         request = op->request->payload;
844
845         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
846
847         return gb_svc_queue_deferred_request(op);
848 }
849
850 static int gb_svc_intf_reset_recv(struct gb_operation *op)
851 {
852         struct gb_svc *svc = gb_connection_get_data(op->connection);
853         struct gb_message *request = op->request;
854         struct gb_svc_intf_reset_request *reset;
855         u8 intf_id;
856
857         if (request->payload_size < sizeof(*reset)) {
858                 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
859                                 request->payload_size, sizeof(*reset));
860                 return -EINVAL;
861         }
862         reset = request->payload;
863
864         intf_id = reset->intf_id;
865
866         /* FIXME Reset the interface here */
867
868         return 0;
869 }
870
871 static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
872 {
873         switch (key_code) {
874         case GB_KEYCODE_ARA:
875                 *code = SVC_KEY_ARA_BUTTON;
876                 break;
877         default:
878                 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
879                 return -EINVAL;
880         }
881
882         return 0;
883 }
884
885 static int gb_svc_key_event_recv(struct gb_operation *op)
886 {
887         struct gb_svc *svc = gb_connection_get_data(op->connection);
888         struct gb_message *request = op->request;
889         struct gb_svc_key_event_request *key;
890         u16 code;
891         u8 event;
892         int ret;
893
894         if (request->payload_size < sizeof(*key)) {
895                 dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
896                          request->payload_size, sizeof(*key));
897                 return -EINVAL;
898         }
899
900         key = request->payload;
901
902         ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
903         if (ret < 0)
904                 return ret;
905
906         event = key->key_event;
907         if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
908                 dev_warn(&svc->dev, "unknown key event received: %u\n", event);
909                 return -EINVAL;
910         }
911
912         input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
913         input_sync(svc->input);
914
915         return 0;
916 }
917
918 static int gb_svc_request_handler(struct gb_operation *op)
919 {
920         struct gb_connection *connection = op->connection;
921         struct gb_svc *svc = gb_connection_get_data(connection);
922         u8 type = op->type;
923         int ret = 0;
924
925         /*
926          * SVC requests need to follow a specific order (at least initially) and
927          * below code takes care of enforcing that. The expected order is:
928          * - PROTOCOL_VERSION
929          * - SVC_HELLO
930          * - Any other request, but the earlier two.
931          *
932          * Incoming requests are guaranteed to be serialized and so we don't
933          * need to protect 'state' for any races.
934          */
935         switch (type) {
936         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
937                 if (svc->state != GB_SVC_STATE_RESET)
938                         ret = -EINVAL;
939                 break;
940         case GB_SVC_TYPE_SVC_HELLO:
941                 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
942                         ret = -EINVAL;
943                 break;
944         default:
945                 if (svc->state != GB_SVC_STATE_SVC_HELLO)
946                         ret = -EINVAL;
947                 break;
948         }
949
950         if (ret) {
951                 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
952                                 type, svc->state);
953                 return ret;
954         }
955
956         switch (type) {
957         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
958                 ret = gb_svc_version_request(op);
959                 if (!ret)
960                         svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
961                 return ret;
962         case GB_SVC_TYPE_SVC_HELLO:
963                 ret = gb_svc_hello(op);
964                 if (!ret)
965                         svc->state = GB_SVC_STATE_SVC_HELLO;
966                 return ret;
967         case GB_SVC_TYPE_INTF_HOTPLUG:
968                 return gb_svc_intf_hotplug_recv(op);
969         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
970                 return gb_svc_intf_hot_unplug_recv(op);
971         case GB_SVC_TYPE_INTF_RESET:
972                 return gb_svc_intf_reset_recv(op);
973         case GB_SVC_TYPE_KEY_EVENT:
974                 return gb_svc_key_event_recv(op);
975         default:
976                 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
977                 return -EINVAL;
978         }
979 }
980
981 static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
982 {
983         struct input_dev *input_dev;
984
985         input_dev = input_allocate_device();
986         if (!input_dev)
987                 return ERR_PTR(-ENOMEM);
988
989         input_dev->name = dev_name(&svc->dev);
990         svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
991                                     input_dev->name);
992         if (!svc->input_phys)
993                 goto err_free_input;
994
995         input_dev->phys = svc->input_phys;
996         input_dev->dev.parent = &svc->dev;
997
998         input_set_drvdata(input_dev, svc);
999
1000         input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
1001
1002         return input_dev;
1003
1004 err_free_input:
1005         input_free_device(svc->input);
1006         return ERR_PTR(-ENOMEM);
1007 }
1008
1009 static void gb_svc_release(struct device *dev)
1010 {
1011         struct gb_svc *svc = to_gb_svc(dev);
1012
1013         if (svc->connection)
1014                 gb_connection_destroy(svc->connection);
1015         ida_destroy(&svc->device_id_map);
1016         destroy_workqueue(svc->wq);
1017         kfree(svc->input_phys);
1018         kfree(svc);
1019 }
1020
1021 struct device_type greybus_svc_type = {
1022         .name           = "greybus_svc",
1023         .release        = gb_svc_release,
1024 };
1025
1026 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
1027 {
1028         struct gb_svc *svc;
1029
1030         svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1031         if (!svc)
1032                 return NULL;
1033
1034         svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
1035         if (!svc->wq) {
1036                 kfree(svc);
1037                 return NULL;
1038         }
1039
1040         svc->dev.parent = &hd->dev;
1041         svc->dev.bus = &greybus_bus_type;
1042         svc->dev.type = &greybus_svc_type;
1043         svc->dev.groups = svc_groups;
1044         svc->dev.dma_mask = svc->dev.parent->dma_mask;
1045         device_initialize(&svc->dev);
1046
1047         dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
1048
1049         ida_init(&svc->device_id_map);
1050         svc->state = GB_SVC_STATE_RESET;
1051         svc->hd = hd;
1052
1053         svc->input = gb_svc_input_create(svc);
1054         if (IS_ERR(svc->input)) {
1055                 dev_err(&svc->dev, "failed to create input device: %ld\n",
1056                         PTR_ERR(svc->input));
1057                 goto err_put_device;
1058         }
1059
1060         svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
1061                                                 gb_svc_request_handler);
1062         if (IS_ERR(svc->connection)) {
1063                 dev_err(&svc->dev, "failed to create connection: %ld\n",
1064                                 PTR_ERR(svc->connection));
1065                 goto err_free_input;
1066         }
1067
1068         gb_connection_set_data(svc->connection, svc);
1069
1070         return svc;
1071
1072 err_free_input:
1073         input_free_device(svc->input);
1074 err_put_device:
1075         put_device(&svc->dev);
1076         return NULL;
1077 }
1078
1079 int gb_svc_add(struct gb_svc *svc)
1080 {
1081         int ret;
1082
1083         /*
1084          * The SVC protocol is currently driven by the SVC, so the SVC device
1085          * is added from the connection request handler when enough
1086          * information has been received.
1087          */
1088         ret = gb_connection_enable(svc->connection);
1089         if (ret)
1090                 return ret;
1091
1092         return 0;
1093 }
1094
1095 static void gb_svc_remove_interfaces(struct gb_svc *svc)
1096 {
1097         struct gb_interface *intf, *tmp;
1098
1099         list_for_each_entry_safe(intf, tmp, &svc->hd->interfaces, links) {
1100                 gb_interface_disable(intf);
1101                 gb_interface_deactivate(intf);
1102                 gb_interface_remove(intf);
1103         }
1104 }
1105
1106 void gb_svc_del(struct gb_svc *svc)
1107 {
1108         gb_connection_disable(svc->connection);
1109
1110         /*
1111          * The SVC device and input device may have been registered
1112          * from the request handler.
1113          */
1114         if (device_is_registered(&svc->dev)) {
1115                 svc_debugfs_exit(svc);
1116                 gb_svc_watchdog_destroy(svc);
1117                 input_unregister_device(svc->input);
1118                 device_del(&svc->dev);
1119         }
1120
1121         flush_workqueue(svc->wq);
1122
1123         gb_svc_remove_interfaces(svc);
1124 }
1125
1126 void gb_svc_put(struct gb_svc *svc)
1127 {
1128         put_device(&svc->dev);
1129 }