greybus: hd: make host device a device
[cascardo/linux.git] / drivers / staging / greybus / endo.c
1 /*
2  * Greybus endo code
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include "greybus.h"
11
12 /* Endo ID (16 bits long) Masks */
13 #define ENDO_ID_MASK                            0xFFFF
14 #define ENDO_LARGE_MASK                         0x1000
15 #define ENDO_MEDIUM_MASK                        0x0400
16 #define ENDO_MINI_MASK                          0x0100
17
18 #define ENDO_FRONT_MASK(id)                     ((id) >> 13)
19 #define ENDO_BACK_SIDE_RIBS_MASK(ribs)          ((1 << (ribs)) - 1)
20
21 /*
22  * endo_is_medium() should be used only if endo isn't large. And endo_is_mini()
23  * should be used only if endo isn't large or medium.
24  */
25 #define endo_is_large(id)                       ((id) & ENDO_LARGE_MASK)
26 #define endo_is_medium(id)                      ((id) & ENDO_MEDIUM_MASK)
27 #define endo_is_mini(id)                        ((id) & ENDO_MINI_MASK)
28
29 #define endo_back_left_ribs(id, ribs)           (((id) >> (ribs)) & ENDO_BACK_SIDE_RIBS_MASK(ribs))
30 #define endo_back_right_ribs(id, ribs)          ((id) & ENDO_BACK_SIDE_RIBS_MASK(ribs))
31
32 /*
33  * An Endo has interface block positions on the front and back.
34  * Each has numeric ID, starting with 1 (interface 0 represents
35  * the SVC within the Endo itself).  The maximum interface ID is the
36  * also the number of non-SVC interfaces possible on the endo.
37  *
38  * Total number of interfaces:
39  * - Front: 4
40  * - Back left: max_ribs + 1
41  * - Back right: max_ribs + 1
42  */
43 #define max_endo_interface_id(endo_layout) \
44                 (4 + ((endo_layout)->max_ribs + 1) * 2)
45
46 static struct ida greybus_endo_id_map;
47
48 /* endo sysfs attributes */
49 static ssize_t serial_number_show(struct device *dev,
50                                 struct device_attribute *attr, char *buf)
51 {
52         struct gb_endo *endo = to_gb_endo(dev);
53
54         return sprintf(buf, "%s\n", &endo->svc_info.serial_number[0]);
55 }
56 static DEVICE_ATTR_RO(serial_number);
57
58 static ssize_t version_show(struct device *dev,
59                                 struct device_attribute *attr, char *buf)
60 {
61         struct gb_endo *endo = to_gb_endo(dev);
62
63         return sprintf(buf, "%s\n", &endo->svc_info.version[0]);
64 }
65 static DEVICE_ATTR_RO(version);
66
67 static struct attribute *svc_attrs[] = {
68         &dev_attr_serial_number.attr,
69         &dev_attr_version.attr,
70         NULL,
71 };
72
73 static const struct attribute_group svc_group = {
74         .attrs = svc_attrs,
75         .name = "svc",
76 };
77
78 static ssize_t id_show(struct device *dev,
79                         struct device_attribute *attr, char *buf)
80 {
81         struct gb_endo *endo = to_gb_endo(dev);
82
83         return sprintf(buf, "0x%04x\n", endo->id);
84 }
85 static DEVICE_ATTR_RO(id);
86
87 static ssize_t ap_intf_id_show(struct device *dev,
88                         struct device_attribute *attr, char *buf)
89 {
90         struct gb_endo *endo = to_gb_endo(dev);
91
92         return sprintf(buf, "%u\n", endo->ap_intf_id);
93 }
94 static DEVICE_ATTR_RO(ap_intf_id);
95
96 static struct attribute *endo_attrs[] = {
97         &dev_attr_id.attr,
98         &dev_attr_ap_intf_id.attr,
99         NULL,
100 };
101
102 static const struct attribute_group endo_group = {
103         .attrs = endo_attrs,
104 };
105
106 static const struct attribute_group *endo_groups[] = {
107         &endo_group,
108         &svc_group,
109         NULL,
110 };
111
112 static void gb_endo_release(struct device *dev)
113 {
114         struct gb_endo *endo = to_gb_endo(dev);
115
116         ida_simple_remove(&greybus_endo_id_map, endo->dev_id);
117         kfree(endo);
118 }
119
120 struct device_type greybus_endo_type = {
121         .name =         "greybus_endo",
122         .release =      gb_endo_release,
123 };
124
125
126 /* Validate Endo ID */
127
128 /*
129  * The maximum module height is 2 units.  This means any adjacent pair of bits
130  * in the left or right mask must have at least one bit set.
131  */
132 static inline bool modules_oversized(unsigned int count, unsigned int mask)
133 {
134         int i;
135
136         for (i = 0; i < count - 1; i++)
137                 if (!(mask & (0x3 << i)))
138                         return true;
139
140         return false;
141 }
142
143 /* Reverse a number of least significant bits in a value */
144 static u8 reverse_bits(unsigned int value, unsigned int bits)
145 {
146         u8 result = 0;
147         u8 result_mask = 1 << (bits - 1);
148         u8 value_mask = 1;
149
150         while (value && result_mask) {
151                 if (value & value_mask) {
152                         result |= result_mask;
153                         value ^= value_mask;
154                 }
155                 value_mask <<= 1;
156                 result_mask >>= 1;
157         }
158
159         return result;
160 }
161
162 /*
163  * An Endo can have at most one instance of a single rib spanning its whole
164  * width.  That is, the left and right bit masks representing the rib positions
165  * must have at most one bit set in both masks.
166  */
167 static bool single_cross_rib(u8 left_ribs, u8 right_ribs)
168 {
169         u8 span_ribs = left_ribs & right_ribs;
170
171         /* Power of 2 ? */
172         if (span_ribs & (span_ribs - 1))
173                 return false;
174         return true;
175 }
176
177 /*
178  * Each Endo size has its own set of front module configurations.  For most, the
179  * resulting rib mask is the same regardless of the Endo size.  The mini Endo
180  * has a few differences though.
181  *
182  * Endo front has 4 interface blocks and 3 rib positions. A maximum of 2 ribs
183  * are allowed to be present for any endo type.
184  *
185  * This routine validates front mask and sets 'front_ribs', its 3 least
186  * significant bits represent front ribs mask, other are 0.  The front values
187  * should be within range (1..6).
188  *
189  * front_ribs bitmask:
190  * - Bit 0: 1st rib location from top, i.e. between interface 1 and 2.
191  * - Bit 1: 2nd rib location from top, i.e. between interface 2 and 3.
192  * - Bit 2: 3rd rib location from top, i.e. between interface 3 and 4.
193  */
194 static bool validate_front_ribs(struct gb_host_device *hd,
195                                 struct endo_layout *layout, bool mini,
196                                 u16 endo_id)
197 {
198         u8 front_mask = ENDO_FRONT_MASK(endo_id);
199
200         /* Verify front endo mask is in valid range, i.e. 1-6 */
201
202         switch (front_mask) {
203         case 1:
204                 layout->front_ribs = 0x0;
205                 break;
206         case 2:
207                 layout->front_ribs = 0x1;
208                 break;
209         case 3:
210                 layout->front_ribs = 0x4;
211                 break;
212         case 4:
213                 layout->front_ribs = mini ? 0x2 : 0x3;
214                 break;
215         case 5:
216                 layout->front_ribs = mini ? 0x2 : 0x6;
217                 break;
218         case 6:
219                 layout->front_ribs = 0x5;
220                 break;
221         default:
222                 dev_err(&hd->dev,
223                         "%s: Invalid endo front mask 0x%02x, id 0x%04x\n",
224                         __func__, front_mask, endo_id);
225                 return false;
226         }
227
228         return true;
229 }
230
231 /*
232  * The rear of an endo has a single vertical "spine", and the modules placed on
233  * the left and right of that spine are separated by ribs.  Only one "cross"
234  * (i.e. rib that spans the entire width) is allowed of the back of the endo;
235  * all other ribs reach from the spine to the left or right edge.
236  *
237  * The width of the module positions on the left and right of the spine are
238  * determined by the width of the endo (either 1 or 2 "units").  The height of
239  * the modules is determined by the placement of the ribs (a module can be
240  * either 1 or 2 units high).
241  *
242  * The lower 13 bits of the 16-bit endo id are used to encode back ribs
243  * information.  The large form factor endo uses all of these bits; the medium
244  * and mini form factors leave some bits unused (such bits shall be ignored, and
245  * are 0 for the purposes of this endo id definition).
246  *
247  * Each defined bit represents a rib position on one or the other side
248  * of the spine on the back of an endo.  If that bit is set (1), it
249  * means a rib is present in the corresponding location; otherwise
250  * there is no rib there.
251  *
252  * Rotating an endo 180 degrees does not produce a new rib configuration. A
253  * single endo id represents a specific configuration of ribs without regard to
254  * its rotational orientation.  We define one canonical id to represent a
255  * particular endo configuration.
256  */
257 static bool validate_back_ribs(struct gb_host_device *hd,
258                                struct endo_layout *layout, u16 endo_id)
259 {
260         u8 max_ribs = layout->max_ribs;
261         u8 left_ribs;
262         u8 right_ribs;
263
264         /* Extract the left and right rib masks */
265         left_ribs = endo_back_left_ribs(endo_id, max_ribs);
266         right_ribs = endo_back_right_ribs(endo_id, max_ribs);
267
268         if (!single_cross_rib(left_ribs, right_ribs)) {
269                 dev_err(&hd->dev,
270                         "%s: More than one spanning rib (left 0x%02x right 0x%02x), id 0x%04x\n",
271                         __func__, left_ribs, right_ribs, endo_id);
272                 return false;
273         }
274
275         if (modules_oversized(max_ribs, left_ribs)) {
276                         dev_err(&hd->dev,
277                                 "%s: Oversized module (left) 0x%02x, id 0x%04x\n",
278                                 __func__, left_ribs, endo_id);
279                         return false;
280         }
281
282         if (modules_oversized(max_ribs, right_ribs)) {
283                         dev_err(&hd->dev,
284                                 "%s: Oversized module (Right) 0x%02x, id 0x%04x\n",
285                                 __func__, right_ribs, endo_id);
286                         return false;
287         }
288
289         /*
290          * The Endo numbering scheme represents the left and right rib
291          * configuration in a way that's convenient for looking for multiple
292          * spanning ribs.  But it doesn't match the normal Endo interface
293          * numbering scheme (increasing counter-clockwise around the back).
294          * Reverse the right bit positions so they do match.
295          */
296         right_ribs = reverse_bits(right_ribs, max_ribs);
297
298         /*
299          * A mini or large Endo rotated 180 degrees is still the same Endo.  In
300          * most cases that allows two distinct values to represent the same
301          * Endo; we choose one of them to be the canonical one (and the other is
302          * invalid).  The canonical one is identified by higher value of left
303          * ribs mask.
304          *
305          * This doesn't apply to medium Endos, because the left and right sides
306          * are of different widths.
307          */
308         if (max_ribs != ENDO_BACK_RIBS_MEDIUM && left_ribs < right_ribs) {
309                 dev_err(&hd->dev, "%s: Non-canonical endo id 0x%04x\n", __func__,
310                         endo_id);
311                 return false;
312         }
313
314         layout->left_ribs = left_ribs;
315         layout->right_ribs = right_ribs;
316         return true;
317 }
318
319 /*
320  * Validate the endo-id passed from SVC. Error out if its not a valid Endo,
321  * else return structure representing ribs positions on front and back of Endo.
322  */
323 static int gb_endo_validate_id(struct gb_host_device *hd,
324                                struct endo_layout *layout, u16 endo_id)
325 {
326         /* Validate Endo Size */
327         if (endo_is_large(endo_id)) {
328                 /* Large Endo type */
329                 layout->max_ribs = ENDO_BACK_RIBS_LARGE;
330         } else if (endo_is_medium(endo_id)) {
331                 /* Medium Endo type */
332                 layout->max_ribs = ENDO_BACK_RIBS_MEDIUM;
333         } else if (endo_is_mini(endo_id)) {
334                 /* Mini Endo type */
335                 layout->max_ribs = ENDO_BACK_RIBS_MINI;
336         } else {
337                 dev_err(&hd->dev, "%s: Invalid endo type, id 0x%04x\n",
338                         __func__, endo_id);
339                 return -EINVAL;
340         }
341
342         if (!validate_back_ribs(hd, layout, endo_id))
343                 return -EINVAL;
344
345         if (!validate_front_ribs(hd, layout,
346                                  layout->max_ribs == ENDO_BACK_RIBS_MINI,
347                                  endo_id))
348                 return -EINVAL;
349
350         return 0;
351 }
352
353 /*
354  * Look up which module contains the given interface.
355  *
356  * A module's ID is the same as its lowest-numbered interface ID. So the module
357  * ID for a 1x1 module is always the same as its interface ID.
358  *
359  * For Endo Back:
360  * The module ID for an interface on a 1x2 or 2x2 module (which use two
361  * interface blocks) can be either the interface ID, or one less than the
362  * interface ID if there is no rib "above" the interface.
363  *
364  * For Endo Front:
365  * There are three rib locations in front and all of them might be unused, i.e.
366  * a single module is used for all 4 interfaces. We need to check all ribs in
367  * that case to find module ID.
368  */
369 u8 endo_get_module_id(struct gb_endo *endo, u8 interface_id)
370 {
371         struct endo_layout *layout = &endo->layout;
372         unsigned int height = layout->max_ribs + 1;
373         unsigned int iid = interface_id - 1;
374         unsigned int mask, rib_mask;
375
376         if (!interface_id)
377                 return 0;
378
379         if (iid < height) {                     /* back left */
380                 mask = layout->left_ribs;
381         } else if (iid < 2 * height) {  /* back right */
382                 mask = layout->right_ribs;
383                 iid -= height;
384         } else {                                        /* front */
385                 mask = layout->front_ribs;
386                 iid -= 2 * height;
387         }
388
389         /*
390          * Find the next rib *above* this interface to determine the lowest
391          * interface ID in the module.
392          */
393         rib_mask = 1 << iid;
394         while ((rib_mask >>= 1) != 0 && !(mask & rib_mask))
395                 --interface_id;
396
397         return interface_id;
398 }
399
400 /*
401  * Creates all possible modules for the Endo.
402  *
403  * We try to create modules for all possible interface IDs. If a module is
404  * already created, we skip creating it again with the help of prev_module_id.
405  */
406 static int create_modules(struct gb_endo *endo)
407 {
408         struct gb_module *module;
409         int prev_module_id = 0;
410         int interface_id;
411         int module_id;
412         int max_id;
413
414         max_id = max_endo_interface_id(&endo->layout);
415
416         /* Find module corresponding to each interface */
417         for (interface_id = 1; interface_id <= max_id; interface_id++) {
418                 module_id = endo_get_module_id(endo, interface_id);
419
420                 if (WARN_ON(!module_id))
421                         continue;
422
423                 /* Skip already created modules */
424                 if (module_id == prev_module_id)
425                         continue;
426
427                 prev_module_id = module_id;
428
429                 /* New module, create it */
430                 module = gb_module_create(&endo->dev, module_id);
431                 if (!module)
432                         return -EINVAL;
433         }
434
435         return 0;
436 }
437
438 static int gb_endo_register(struct gb_host_device *hd,
439                             struct gb_endo *endo)
440 {
441         int dev_id;
442         int retval;
443
444         dev_id = ida_simple_get(&greybus_endo_id_map, 0, 0, GFP_KERNEL);
445         if (dev_id < 0)
446                 return dev_id;
447
448         endo->dev_id = dev_id;
449
450         endo->dev.parent = &hd->dev;
451         endo->dev.bus = &greybus_bus_type;
452         endo->dev.type = &greybus_endo_type;
453         endo->dev.groups = endo_groups;
454         endo->dev.dma_mask = hd->dev.dma_mask;
455         device_initialize(&endo->dev);
456         dev_set_name(&endo->dev, "endo%hu", endo->dev_id);
457
458         // FIXME
459         // Get the version and serial number from the SVC, right now we are
460         // using "fake" numbers.
461         strcpy(&endo->svc_info.serial_number[0], "042");
462         strcpy(&endo->svc_info.version[0], "0.0");
463
464         retval = device_add(&endo->dev);
465         if (retval) {
466                 dev_err(&hd->dev, "failed to add endo device of id 0x%04x\n",
467                         endo->id);
468                 put_device(&endo->dev);
469         }
470
471         return retval;
472 }
473
474 struct gb_endo *gb_endo_create(struct gb_host_device *hd, u16 endo_id,
475                                 u8 ap_intf_id)
476 {
477         struct gb_endo *endo;
478         int retval;
479
480         endo = kzalloc(sizeof(*endo), GFP_KERNEL);
481         if (!endo)
482                 return ERR_PTR(-ENOMEM);
483
484         /* First check if the value supplied is a valid endo id */
485         if (gb_endo_validate_id(hd, &endo->layout, endo_id)) {
486                 retval = -EINVAL;
487                 goto free_endo;
488         }
489         if (ap_intf_id > max_endo_interface_id(&endo->layout)) {
490                 retval = -EINVAL;
491                 goto free_endo;
492         }
493         endo->id = endo_id;
494         endo->ap_intf_id = ap_intf_id;
495
496         /* Register Endo device */
497         retval = gb_endo_register(hd, endo);
498         if (retval)
499                 goto free_endo;
500
501         /* Create modules/interfaces */
502         retval = create_modules(endo);
503         if (retval) {
504                 gb_endo_remove(endo);
505                 return NULL;
506         }
507
508         return endo;
509
510 free_endo:
511         kfree(endo);
512
513         return ERR_PTR(retval);
514 }
515
516 void gb_endo_remove(struct gb_endo *endo)
517 {
518         if (!endo)
519                 return;
520
521         /* remove all modules for this endo */
522         gb_module_remove_all(endo);
523
524         device_unregister(&endo->dev);
525 }
526
527 int greybus_endo_setup(struct gb_host_device *hd, u16 endo_id,
528                        u8 ap_intf_id)
529 {
530         struct gb_endo *endo;
531
532         endo = gb_endo_create(hd, endo_id, ap_intf_id);
533         if (IS_ERR(endo))
534                 return PTR_ERR(endo);
535         hd->endo = endo;
536
537         return 0;
538 }
539 EXPORT_SYMBOL_GPL(greybus_endo_setup);
540
541 int __init gb_endo_init(void)
542 {
543         ida_init(&greybus_endo_id_map);
544
545         return 0;
546 }
547
548 void gb_endo_exit(void)
549 {
550         ida_destroy(&greybus_endo_id_map);
551 }