1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <linux/module.h>
38#include <linux/usb.h>
39#include <linux/interrupt.h>
40#include <linux/slab.h>
41#include <linux/delay.h>
42#include <linux/uwb.h>
43#include <linux/usb/wusb.h>
44#include <linux/usb/wusb-wa.h>
45#include "i1480-dfu.h"
46
47struct i1480_usb {
48 struct i1480 i1480;
49 struct usb_device *usb_dev;
50 struct usb_interface *usb_iface;
51 struct urb *neep_urb;
52};
53
54
55static
56void i1480_usb_init(struct i1480_usb *i1480_usb)
57{
58 i1480_init(&i1480_usb->i1480);
59}
60
61
62static
63int i1480_usb_create(struct i1480_usb *i1480_usb, struct usb_interface *iface)
64{
65 struct usb_device *usb_dev = interface_to_usbdev(iface);
66 int result = -ENOMEM;
67
68 i1480_usb->usb_dev = usb_get_dev(usb_dev);
69 i1480_usb->usb_iface = usb_get_intf(iface);
70 usb_set_intfdata(iface, i1480_usb);
71 i1480_usb->neep_urb = usb_alloc_urb(0, GFP_KERNEL);
72 if (i1480_usb->neep_urb == NULL)
73 goto error;
74 return 0;
75
76error:
77 usb_set_intfdata(iface, NULL);
78 usb_put_intf(iface);
79 usb_put_dev(usb_dev);
80 return result;
81}
82
83
84static
85void i1480_usb_destroy(struct i1480_usb *i1480_usb)
86{
87 usb_kill_urb(i1480_usb->neep_urb);
88 usb_free_urb(i1480_usb->neep_urb);
89 usb_set_intfdata(i1480_usb->usb_iface, NULL);
90 usb_put_intf(i1480_usb->usb_iface);
91 usb_put_dev(i1480_usb->usb_dev);
92}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109static
110int i1480_usb_write(struct i1480 *i1480, u32 memory_address,
111 const void *buffer, size_t size)
112{
113 int result = 0;
114 struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
115 size_t buffer_size, itr = 0;
116
117 BUG_ON(size & 0x3);
118 while (size > 0) {
119 buffer_size = size < i1480->buf_size ? size : i1480->buf_size;
120 memcpy(i1480->cmd_buf, buffer + itr, buffer_size);
121 result = usb_control_msg(
122 i1480_usb->usb_dev, usb_sndctrlpipe(i1480_usb->usb_dev, 0),
123 0xf0, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
124 memory_address, (memory_address >> 16),
125 i1480->cmd_buf, buffer_size, 100 );
126 if (result < 0)
127 break;
128 itr += result;
129 memory_address += result;
130 size -= result;
131 }
132 return result;
133}
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149static
150int i1480_usb_read(struct i1480 *i1480, u32 addr, size_t size)
151{
152 ssize_t result = 0, bytes = 0;
153 size_t itr, read_size = i1480->buf_size;
154 struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
155
156 BUG_ON(size > i1480->buf_size);
157 BUG_ON(size & 0x3);
158 BUG_ON(read_size > 512);
159
160 if (addr >= 0x8000d200 && addr < 0x8000d400)
161 read_size = 4;
162
163 for (itr = 0; itr < size; itr += read_size) {
164 size_t itr_addr = addr + itr;
165 size_t itr_size = min(read_size, size - itr);
166 result = usb_control_msg(
167 i1480_usb->usb_dev, usb_rcvctrlpipe(i1480_usb->usb_dev, 0),
168 0xf0, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
169 itr_addr, (itr_addr >> 16),
170 i1480->cmd_buf + itr, itr_size,
171 100 );
172 if (result < 0) {
173 dev_err(i1480->dev, "%s: USB read error: %zd\n",
174 __func__, result);
175 goto out;
176 }
177 if (result != itr_size) {
178 result = -EIO;
179 dev_err(i1480->dev,
180 "%s: partial read got only %zu bytes vs %zu expected\n",
181 __func__, result, itr_size);
182 goto out;
183 }
184 bytes += result;
185 }
186 result = bytes;
187out:
188 return result;
189}
190
191
192
193
194
195
196
197static
198void i1480_usb_neep_cb(struct urb *urb)
199{
200 struct i1480 *i1480 = urb->context;
201 struct device *dev = i1480->dev;
202
203 switch (urb->status) {
204 case 0:
205 break;
206 case -ECONNRESET:
207 case -ENOENT:
208 dev_dbg(dev, "NEEP: reset/noent %d\n", urb->status);
209 break;
210 case -ESHUTDOWN:
211 dev_dbg(dev, "NEEP: down %d\n", urb->status);
212 break;
213 default:
214 dev_err(dev, "NEEP: unknown status %d\n", urb->status);
215 break;
216 }
217 i1480->evt_result = urb->actual_length;
218 complete(&i1480->evt_complete);
219 return;
220}
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235static
236int i1480_usb_wait_init_done(struct i1480 *i1480)
237{
238 int result;
239 struct device *dev = i1480->dev;
240 struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
241 struct usb_endpoint_descriptor *epd;
242
243 init_completion(&i1480->evt_complete);
244 i1480->evt_result = -EINPROGRESS;
245 epd = &i1480_usb->usb_iface->cur_altsetting->endpoint[0].desc;
246 usb_fill_int_urb(i1480_usb->neep_urb, i1480_usb->usb_dev,
247 usb_rcvintpipe(i1480_usb->usb_dev, epd->bEndpointAddress),
248 i1480->evt_buf, i1480->buf_size,
249 i1480_usb_neep_cb, i1480, epd->bInterval);
250 result = usb_submit_urb(i1480_usb->neep_urb, GFP_KERNEL);
251 if (result < 0) {
252 dev_err(dev, "init done: cannot submit NEEP read: %d\n",
253 result);
254 goto error_submit;
255 }
256
257 result = wait_for_completion_interruptible_timeout(
258 &i1480->evt_complete, HZ);
259 if (result <= 0) {
260 result = result == 0 ? -ETIMEDOUT : result;
261 goto error_wait;
262 }
263 usb_kill_urb(i1480_usb->neep_urb);
264 return 0;
265
266error_wait:
267 usb_kill_urb(i1480_usb->neep_urb);
268error_submit:
269 i1480->evt_result = result;
270 return result;
271}
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290static
291int i1480_usb_cmd(struct i1480 *i1480, const char *cmd_name, size_t cmd_size)
292{
293 int result;
294 struct device *dev = i1480->dev;
295 struct i1480_usb *i1480_usb = container_of(i1480, struct i1480_usb, i1480);
296 struct usb_endpoint_descriptor *epd;
297 struct uwb_rccb *cmd = i1480->cmd_buf;
298 u8 iface_no;
299
300
301 iface_no = i1480_usb->usb_iface->cur_altsetting->desc.bInterfaceNumber;
302 epd = &i1480_usb->usb_iface->cur_altsetting->endpoint[0].desc;
303 usb_fill_int_urb(
304 i1480_usb->neep_urb, i1480_usb->usb_dev,
305 usb_rcvintpipe(i1480_usb->usb_dev, epd->bEndpointAddress),
306 i1480->evt_buf, i1480->buf_size,
307 i1480_usb_neep_cb, i1480, epd->bInterval);
308 result = usb_submit_urb(i1480_usb->neep_urb, GFP_KERNEL);
309 if (result < 0) {
310 dev_err(dev, "%s: cannot submit NEEP read: %d\n",
311 cmd_name, result);
312 goto error_submit_ep1;
313 }
314
315 result = usb_control_msg(
316 i1480_usb->usb_dev, usb_sndctrlpipe(i1480_usb->usb_dev, 0),
317 WA_EXEC_RC_CMD,
318 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
319 0, iface_no,
320 cmd, cmd_size,
321 100 );
322 if (result < 0) {
323 dev_err(dev, "%s: control request failed: %d\n",
324 cmd_name, result);
325 goto error_submit_ep0;
326 }
327 return result;
328
329error_submit_ep0:
330 usb_kill_urb(i1480_usb->neep_urb);
331error_submit_ep1:
332 return result;
333}
334
335
336
337
338
339
340
341static
342int i1480_usb_probe(struct usb_interface *iface, const struct usb_device_id *id)
343{
344 struct i1480_usb *i1480_usb;
345 struct i1480 *i1480;
346 struct device *dev = &iface->dev;
347 int result;
348
349 result = -ENODEV;
350 if (iface->cur_altsetting->desc.bInterfaceNumber != 0) {
351 dev_dbg(dev, "not attaching to iface %d\n",
352 iface->cur_altsetting->desc.bInterfaceNumber);
353 goto error;
354 }
355 if (iface->num_altsetting > 1
356 && interface_to_usbdev(iface)->descriptor.idProduct == 0xbabe) {
357
358 result = usb_set_interface(interface_to_usbdev(iface), 0, 1);
359 if (result < 0)
360 dev_warn(dev,
361 "can't set altsetting 1 on iface 0: %d\n",
362 result);
363 }
364
365 result = -ENOMEM;
366 i1480_usb = kzalloc(sizeof(*i1480_usb), GFP_KERNEL);
367 if (i1480_usb == NULL) {
368 dev_err(dev, "Unable to allocate instance\n");
369 goto error;
370 }
371 i1480_usb_init(i1480_usb);
372
373 i1480 = &i1480_usb->i1480;
374 i1480->buf_size = 512;
375 i1480->cmd_buf = kmalloc(2 * i1480->buf_size, GFP_KERNEL);
376 if (i1480->cmd_buf == NULL) {
377 dev_err(dev, "Cannot allocate transfer buffers\n");
378 result = -ENOMEM;
379 goto error_buf_alloc;
380 }
381 i1480->evt_buf = i1480->cmd_buf + i1480->buf_size;
382
383 result = i1480_usb_create(i1480_usb, iface);
384 if (result < 0) {
385 dev_err(dev, "Cannot create instance: %d\n", result);
386 goto error_create;
387 }
388
389
390 i1480->pre_fw_name = "i1480-pre-phy-0.0.bin";
391 i1480->mac_fw_name = "i1480-usb-0.0.bin";
392 i1480->mac_fw_name_deprecate = "ptc-0.0.bin";
393 i1480->phy_fw_name = "i1480-phy-0.0.bin";
394 i1480->dev = &iface->dev;
395 i1480->write = i1480_usb_write;
396 i1480->read = i1480_usb_read;
397 i1480->rc_setup = NULL;
398 i1480->wait_init_done = i1480_usb_wait_init_done;
399 i1480->cmd = i1480_usb_cmd;
400
401 result = i1480_fw_upload(&i1480_usb->i1480);
402 if (result >= 0) {
403 usb_reset_device(i1480_usb->usb_dev);
404 result = -ENODEV;
405 }
406 i1480_usb_destroy(i1480_usb);
407error_create:
408 kfree(i1480->cmd_buf);
409error_buf_alloc:
410 kfree(i1480_usb);
411error:
412 return result;
413}
414
415MODULE_FIRMWARE("i1480-pre-phy-0.0.bin");
416MODULE_FIRMWARE("i1480-usb-0.0.bin");
417MODULE_FIRMWARE("i1480-phy-0.0.bin");
418
419#define i1480_USB_DEV(v, p) \
420{ \
421 .match_flags = USB_DEVICE_ID_MATCH_DEVICE \
422 | USB_DEVICE_ID_MATCH_DEV_INFO \
423 | USB_DEVICE_ID_MATCH_INT_INFO, \
424 .idVendor = (v), \
425 .idProduct = (p), \
426 .bDeviceClass = 0xff, \
427 .bDeviceSubClass = 0xff, \
428 .bDeviceProtocol = 0xff, \
429 .bInterfaceClass = 0xff, \
430 .bInterfaceSubClass = 0xff, \
431 .bInterfaceProtocol = 0xff, \
432}
433
434
435
436static const struct usb_device_id i1480_usb_id_table[] = {
437 i1480_USB_DEV(0x8086, 0xdf3b),
438 i1480_USB_DEV(0x15a9, 0x0005),
439 i1480_USB_DEV(0x07d1, 0x3802),
440 i1480_USB_DEV(0x050d, 0x305a),
441 i1480_USB_DEV(0x3495, 0x3007),
442 {},
443};
444MODULE_DEVICE_TABLE(usb, i1480_usb_id_table);
445
446
447static struct usb_driver i1480_dfu_driver = {
448 .name = "i1480-dfu-usb",
449 .id_table = i1480_usb_id_table,
450 .probe = i1480_usb_probe,
451 .disconnect = NULL,
452};
453
454module_usb_driver(i1480_dfu_driver);
455
456MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
457MODULE_DESCRIPTION("Intel Wireless UWB Link 1480 firmware uploader for USB");
458MODULE_LICENSE("GPL");
459