Commit | Line | Data |
---|---|---|
8d4f9d0e ST |
1 | /* |
2 | * Driver for the Siano SMS10xx USB dongle | |
3 | * | |
85447060 MK |
4 | * author: Anatoly Greenblat |
5 | * | |
6 | * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc. | |
8d4f9d0e ST |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
85447060 MK |
9 | * it under the terms of the GNU General Public License version 3 as |
10 | * published by the Free Software Foundation; | |
8d4f9d0e | 11 | * |
85447060 MK |
12 | * Software distributed under the License is distributed on an "AS IS" |
13 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. | |
8d4f9d0e | 14 | * |
85447060 | 15 | * See the GNU General Public License for more details. |
8d4f9d0e ST |
16 | * |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
21 | ||
2e5c1ec8 MK |
22 | #include <linux/kernel.h> |
23 | #include <linux/init.h> | |
2e5c1ec8 MK |
24 | #include <linux/usb.h> |
25 | #include <linux/firmware.h> | |
26 | ||
2e5c1ec8 | 27 | #include "smscoreapi.h" |
2e5c1ec8 | 28 | |
2e5c1ec8 MK |
29 | #define USB1_BUFFER_SIZE 0x1000 |
30 | #define USB2_BUFFER_SIZE 0x4000 | |
31 | ||
32 | #define MAX_BUFFERS 50 | |
33 | #define MAX_URBS 10 | |
34 | ||
35 | typedef struct _smsusb_device smsusb_device_t; | |
36 | ||
37 | typedef struct _smsusb_urb | |
38 | { | |
39 | smscore_buffer_t *cb; | |
40 | smsusb_device_t *dev; | |
41 | ||
42 | struct urb urb; | |
43 | } smsusb_urb_t; | |
44 | ||
45 | typedef struct _smsusb_device | |
46 | { | |
47 | struct usb_device* udev; | |
48 | smscore_device_t *coredev; | |
49 | ||
50 | smsusb_urb_t surbs[MAX_URBS]; | |
51 | ||
52 | int response_alignment; | |
53 | int buffer_size; | |
54 | } *psmsusb_device_t; | |
55 | ||
2e5c1ec8 MK |
56 | int smsusb_submit_urb(smsusb_device_t* dev, smsusb_urb_t* surb); |
57 | ||
58 | void smsusb_onresponse(struct urb *urb) | |
59 | { | |
60 | smsusb_urb_t *surb = (smsusb_urb_t *) urb->context; | |
61 | smsusb_device_t *dev = surb->dev; | |
62 | ||
63 | if (urb->status < 0) | |
64 | { | |
18658117 | 65 | printk(KERN_INFO "%s error, urb status %d, %d bytes\n", __func__, urb->status, urb->actual_length); |
2e5c1ec8 MK |
66 | return; |
67 | } | |
68 | ||
69 | if (urb->actual_length > 0) | |
70 | { | |
71 | SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) surb->cb->p; | |
72 | ||
73 | if (urb->actual_length >= phdr->msgLength) | |
74 | { | |
75 | surb->cb->size = phdr->msgLength; | |
76 | ||
77 | if (dev->response_alignment && (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) | |
78 | { | |
79 | surb->cb->offset = dev->response_alignment + ((phdr->msgFlags >> 8) & 3); | |
80 | ||
81 | // sanity check | |
82 | if (((int) phdr->msgLength + surb->cb->offset) > urb->actual_length) | |
83 | { | |
18658117 | 84 | printk("%s: invalid response msglen %d offset %d size %d\n", __func__, phdr->msgLength, surb->cb->offset, urb->actual_length); |
2e5c1ec8 MK |
85 | goto exit_and_resubmit; |
86 | } | |
87 | ||
88 | // move buffer pointer and copy header to its new location | |
89 | memcpy((char*) phdr + surb->cb->offset, phdr, sizeof(SmsMsgHdr_ST)); | |
90 | } | |
91 | else | |
92 | surb->cb->offset = 0; | |
93 | ||
94 | smscore_onresponse(dev->coredev, surb->cb); | |
95 | surb->cb = NULL; | |
96 | } | |
97 | else | |
98 | { | |
18658117 | 99 | printk("%s invalid response msglen %d actual %d\n", __func__, phdr->msgLength, urb->actual_length); |
2e5c1ec8 MK |
100 | } |
101 | } | |
102 | ||
103 | exit_and_resubmit: | |
104 | smsusb_submit_urb(dev, surb); | |
105 | } | |
106 | ||
107 | int smsusb_submit_urb(smsusb_device_t* dev, smsusb_urb_t* surb) | |
108 | { | |
109 | if (!surb->cb) | |
110 | { | |
111 | surb->cb = smscore_getbuffer(dev->coredev); | |
112 | if (!surb->cb) | |
113 | { | |
18658117 | 114 | printk(KERN_INFO "%s smscore_getbuffer(...) returned NULL\n", __func__); |
2e5c1ec8 MK |
115 | return -ENOMEM; |
116 | } | |
117 | } | |
118 | ||
119 | usb_fill_bulk_urb( | |
120 | &surb->urb, | |
121 | dev->udev, | |
122 | usb_rcvbulkpipe(dev->udev, 0x81), | |
123 | surb->cb->p, | |
124 | dev->buffer_size, | |
125 | smsusb_onresponse, | |
126 | surb | |
127 | ); | |
128 | surb->urb.transfer_dma = surb->cb->phys; | |
129 | surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
130 | ||
131 | return usb_submit_urb(&surb->urb, GFP_ATOMIC); | |
132 | } | |
133 | ||
134 | void smsusb_stop_streaming(smsusb_device_t* dev) | |
135 | { | |
136 | int i; | |
137 | ||
138 | for (i = 0; i < MAX_URBS; i ++) | |
139 | { | |
140 | usb_kill_urb(&dev->surbs[i].urb); | |
141 | ||
142 | if (dev->surbs[i].cb) | |
143 | { | |
144 | smscore_putbuffer(dev->coredev, dev->surbs[i].cb); | |
145 | dev->surbs[i].cb = NULL; | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | int smsusb_start_streaming(smsusb_device_t* dev) | |
151 | { | |
152 | int i, rc; | |
153 | ||
154 | for (i = 0; i < MAX_URBS; i ++) | |
155 | { | |
156 | rc = smsusb_submit_urb(dev, &dev->surbs[i]); | |
157 | if (rc < 0) | |
158 | { | |
18658117 | 159 | printk(KERN_INFO "%s smsusb_submit_urb(...) failed\n", __func__); |
2e5c1ec8 MK |
160 | smsusb_stop_streaming(dev); |
161 | break; | |
162 | } | |
163 | } | |
164 | ||
165 | return rc; | |
166 | } | |
167 | ||
168 | int smsusb_sendrequest(void *context, void *buffer, size_t size) | |
169 | { | |
170 | smsusb_device_t* dev = (smsusb_device_t*) context; | |
171 | int dummy; | |
172 | ||
173 | return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2), buffer, size, &dummy, 1000); | |
174 | } | |
175 | ||
176 | char *smsusb1_fw_lkup[] = | |
177 | { | |
178 | "dvbt_stellar_usb.inp", | |
179 | "dvbh_stellar_usb.inp", | |
180 | "tdmb_stellar_usb.inp", | |
181 | "none", | |
182 | "dvbt_bda_stellar_usb.inp", | |
183 | }; | |
184 | ||
185 | int smsusb1_load_firmware(struct usb_device *udev, int id) | |
186 | { | |
187 | const struct firmware *fw; | |
188 | u8* fw_buffer; | |
189 | int rc, dummy; | |
190 | ||
191 | if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) | |
192 | { | |
18658117 | 193 | printk(KERN_INFO "%s invalid firmware id specified %d\n", __func__, id); |
2e5c1ec8 MK |
194 | return -EINVAL; |
195 | } | |
196 | ||
197 | rc = request_firmware(&fw, smsusb1_fw_lkup[id], &udev->dev); | |
198 | if (rc < 0) | |
199 | { | |
18658117 | 200 | printk(KERN_INFO "%s failed to open \"%s\" mode %d\n", __func__, smsusb1_fw_lkup[id], id); |
2e5c1ec8 MK |
201 | return rc; |
202 | } | |
203 | ||
204 | fw_buffer = kmalloc(fw->size, GFP_KERNEL); | |
205 | if (fw_buffer) | |
206 | { | |
207 | memcpy(fw_buffer, fw->data, fw->size); | |
208 | ||
209 | rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2), fw_buffer, fw->size, &dummy, 1000); | |
210 | ||
18658117 | 211 | printk(KERN_INFO "%s: sent %d(%d) bytes, rc %d\n", __func__, fw->size, dummy, rc); |
2e5c1ec8 MK |
212 | |
213 | kfree(fw_buffer); | |
214 | } | |
215 | else | |
216 | { | |
217 | printk(KERN_INFO "failed to allocate firmware buffer\n"); | |
218 | rc = -ENOMEM; | |
219 | } | |
220 | ||
221 | release_firmware(fw); | |
222 | ||
223 | return rc; | |
224 | } | |
225 | ||
226 | void smsusb1_detectmode(void *context, int *mode) | |
227 | { | |
228 | char *product_string = ((smsusb_device_t *) context)->udev->product; | |
229 | ||
230 | *mode = DEVICE_MODE_NONE; | |
231 | ||
232 | if (!product_string) | |
233 | { | |
234 | product_string = "none"; | |
18658117 | 235 | printk("%s product string not found\n", __func__); |
2e5c1ec8 MK |
236 | } |
237 | else | |
238 | { | |
239 | if (strstr(product_string, "DVBH")) | |
240 | *mode = 1; | |
241 | else if (strstr(product_string, "BDA")) | |
242 | *mode = 4; | |
243 | else if (strstr(product_string, "DVBT")) | |
244 | *mode = 0; | |
245 | else if (strstr(product_string, "TDMB")) | |
246 | *mode = 2; | |
247 | } | |
248 | ||
18658117 | 249 | printk("%s: %d \"%s\"\n", __func__, *mode, product_string); |
2e5c1ec8 MK |
250 | } |
251 | ||
252 | int smsusb1_setmode(void *context, int mode) | |
253 | { | |
254 | SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK, sizeof(SmsMsgHdr_ST), 0 }; | |
255 | ||
256 | if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) | |
257 | { | |
18658117 | 258 | printk(KERN_INFO "%s invalid firmware id specified %d\n", __func__, mode); |
2e5c1ec8 MK |
259 | return -EINVAL; |
260 | } | |
261 | ||
262 | return smsusb_sendrequest(context, &Msg, sizeof(Msg)); | |
263 | } | |
264 | ||
265 | void smsusb_term_device(struct usb_interface *intf) | |
266 | { | |
267 | smsusb_device_t *dev = (smsusb_device_t*) usb_get_intfdata(intf); | |
268 | ||
269 | if (dev) | |
270 | { | |
271 | smsusb_stop_streaming(dev); | |
272 | ||
273 | // unregister from smscore | |
274 | if (dev->coredev) | |
275 | smscore_unregister_device(dev->coredev); | |
276 | ||
277 | kfree(dev); | |
278 | ||
18658117 | 279 | printk(KERN_INFO "%s device %p destroyed\n", __func__, dev); |
2e5c1ec8 MK |
280 | } |
281 | ||
282 | usb_set_intfdata(intf, NULL); | |
283 | } | |
284 | ||
285 | int smsusb_init_device(struct usb_interface *intf) | |
286 | { | |
287 | smsdevice_params_t params; | |
288 | smsusb_device_t* dev; | |
289 | int i, rc; | |
290 | ||
291 | // create device object | |
292 | dev = kzalloc(sizeof(smsusb_device_t), GFP_KERNEL); | |
293 | if (!dev) | |
294 | { | |
18658117 | 295 | printk(KERN_INFO "%s kzalloc(sizeof(smsusb_device_t) failed\n", __func__); |
2e5c1ec8 MK |
296 | return -ENOMEM; |
297 | } | |
298 | ||
299 | memset(¶ms, 0, sizeof(params)); | |
300 | usb_set_intfdata(intf, dev); | |
301 | dev->udev = interface_to_usbdev(intf); | |
302 | ||
303 | switch (dev->udev->descriptor.idProduct) | |
304 | { | |
7d18d2e9 | 305 | case 0x100: |
2e5c1ec8 MK |
306 | dev->buffer_size = USB1_BUFFER_SIZE; |
307 | ||
308 | params.setmode_handler = smsusb1_setmode; | |
309 | params.detectmode_handler = smsusb1_detectmode; | |
310 | break; | |
311 | ||
312 | default: | |
313 | dev->buffer_size = USB2_BUFFER_SIZE; | |
314 | dev->response_alignment = dev->udev->ep_in[1]->desc.wMaxPacketSize - sizeof(SmsMsgHdr_ST); | |
315 | ||
316 | params.flags |= SMS_DEVICE_FAMILY2; | |
317 | break; | |
318 | } | |
319 | ||
320 | params.device = &dev->udev->dev; | |
321 | params.buffer_size = dev->buffer_size; | |
322 | params.num_buffers = MAX_BUFFERS; | |
323 | params.sendrequest_handler = smsusb_sendrequest; | |
324 | params.context = dev; | |
325 | snprintf(params.devpath, sizeof(params.devpath), "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath); | |
326 | ||
327 | // register in smscore | |
328 | rc = smscore_register_device(¶ms, &dev->coredev); | |
329 | if (rc < 0) | |
330 | { | |
18658117 | 331 | printk(KERN_INFO "%s smscore_register_device(...) failed, rc %d\n", __func__, rc); |
2e5c1ec8 MK |
332 | smsusb_term_device(intf); |
333 | return rc; | |
334 | } | |
335 | ||
336 | // initialize urbs | |
337 | for (i = 0; i < MAX_URBS; i ++) | |
338 | { | |
339 | dev->surbs[i].dev = dev; | |
340 | usb_init_urb(&dev->surbs[i].urb); | |
341 | } | |
342 | ||
343 | rc = smsusb_start_streaming(dev); | |
344 | if (rc < 0) | |
345 | { | |
18658117 | 346 | printk(KERN_INFO "%s smsusb_start_streaming(...) failed\n", __func__); |
2e5c1ec8 MK |
347 | smsusb_term_device(intf); |
348 | return rc; | |
349 | } | |
350 | ||
351 | rc = smscore_start_device(dev->coredev); | |
352 | if (rc < 0) | |
353 | { | |
18658117 | 354 | printk(KERN_INFO "%s smscore_start_device(...) failed\n", __func__); |
2e5c1ec8 MK |
355 | smsusb_term_device(intf); |
356 | return rc; | |
357 | } | |
358 | ||
18658117 | 359 | printk(KERN_INFO "%s device %p created\n", __func__, dev); |
2e5c1ec8 MK |
360 | |
361 | return rc; | |
362 | } | |
363 | ||
364 | int smsusb_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
365 | { | |
366 | struct usb_device *udev = interface_to_usbdev(intf); | |
367 | char devpath[32]; | |
368 | int i, rc; | |
369 | ||
370 | if (intf->num_altsetting > 0) | |
371 | { | |
372 | rc = usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 0); | |
373 | if (rc < 0) | |
374 | { | |
18658117 | 375 | printk(KERN_INFO "%s usb_set_interface failed, rc %d\n", __func__, rc); |
2e5c1ec8 MK |
376 | return rc; |
377 | } | |
378 | } | |
379 | ||
380 | printk(KERN_INFO "smsusb_probe %d\n", intf->cur_altsetting->desc.bInterfaceNumber); | |
381 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i ++) | |
382 | printk(KERN_INFO "endpoint %d %02x %02x %d\n", i, intf->cur_altsetting->endpoint[i].desc.bEndpointAddress, intf->cur_altsetting->endpoint[i].desc.bmAttributes, intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize); | |
383 | ||
384 | if (udev->actconfig->desc.bNumInterfaces == 2 && intf->cur_altsetting->desc.bInterfaceNumber == 0) | |
385 | { | |
386 | printk(KERN_INFO "rom interface 0 is not used\n"); | |
387 | return -ENODEV; | |
388 | } | |
389 | ||
390 | if (intf->cur_altsetting->desc.bInterfaceNumber == 1) | |
391 | { | |
392 | snprintf(devpath, 32, "%d:%s", udev->bus->busnum, udev->devpath); | |
393 | return smsusb1_load_firmware(udev, smscore_registry_getmode(devpath)); | |
394 | } | |
395 | ||
396 | return smsusb_init_device(intf); | |
397 | } | |
398 | ||
399 | void smsusb_disconnect(struct usb_interface *intf) | |
400 | { | |
401 | smsusb_term_device(intf); | |
402 | } | |
403 | ||
7d18d2e9 ST |
404 | static struct usb_device_id smsusb_id_table [] = { |
405 | { USB_DEVICE(0x187F, 0x0010) }, | |
406 | { USB_DEVICE(0x187F, 0x0100) }, | |
407 | { USB_DEVICE(0x187F, 0x0200) }, | |
408 | { } /* Terminating entry */ | |
409 | }; | |
410 | MODULE_DEVICE_TABLE (usb, smsusb_id_table); | |
411 | ||
2e5c1ec8 MK |
412 | static struct usb_driver smsusb_driver = { |
413 | .name = "smsusb", | |
414 | .probe = smsusb_probe, | |
415 | .disconnect = smsusb_disconnect, | |
416 | .id_table = smsusb_id_table, | |
417 | }; | |
418 | ||
eae55660 | 419 | int smsusb_register(void) |
2e5c1ec8 MK |
420 | { |
421 | int rc = usb_register(&smsusb_driver); | |
422 | if (rc) | |
423 | printk(KERN_INFO "usb_register failed. Error number %d\n", rc); | |
424 | ||
18658117 | 425 | printk(KERN_INFO "%s\n", __func__); |
2e5c1ec8 MK |
426 | |
427 | return rc; | |
428 | } | |
429 | ||
eae55660 | 430 | void smsusb_unregister(void) |
2e5c1ec8 | 431 | { |
3dd24378 | 432 | /* Regular USB Cleanup */ |
2e5c1ec8 | 433 | usb_deregister(&smsusb_driver); |
18658117 | 434 | printk(KERN_INFO "%s\n", __func__); |
2e5c1ec8 MK |
435 | } |
436 |