2 * Driver for the Siano SMS1xxx USB dongle
4 * author: Anatoly Greenblat
6 * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation;
12 * Software distributed under the License is distributed on an "AS IS"
13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
15 * See the GNU General Public License for more details.
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.
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/usb.h>
25 #include <linux/firmware.h>
27 #include "smscoreapi.h"
28 #include "sms-cards.h"
30 #define USB1_BUFFER_SIZE 0x1000
31 #define USB2_BUFFER_SIZE 0x4000
33 #define MAX_BUFFERS 50
36 struct smsusb_device_t;
39 struct smscore_buffer_t *cb;
40 struct smsusb_device_t *dev;
45 struct smsusb_device_t {
46 struct usb_device *udev;
47 struct smscore_device_t *coredev;
49 struct smsusb_urb_t surbs[MAX_URBS];
51 int response_alignment;
55 static int smsusb_submit_urb(struct smsusb_device_t *dev,
56 struct smsusb_urb_t *surb);
58 static void smsusb_onresponse(struct urb *urb)
60 struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
61 struct smsusb_device_t *dev = surb->dev;
63 if (urb->status < 0) {
64 sms_err("error, urb status %d, %d bytes",
65 urb->status, urb->actual_length);
69 if (urb->actual_length > 0) {
70 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) surb->cb->p;
72 if (urb->actual_length >= phdr->msgLength) {
73 surb->cb->size = phdr->msgLength;
75 if (dev->response_alignment &&
76 (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) {
79 dev->response_alignment +
80 ((phdr->msgFlags >> 8) & 3);
83 if (((int) phdr->msgLength +
84 surb->cb->offset) > urb->actual_length) {
85 sms_err("invalid response "
86 "msglen %d offset %d "
91 goto exit_and_resubmit;
94 /* move buffer pointer and
95 * copy header to its new location */
96 memcpy((char *) phdr + surb->cb->offset,
97 phdr, sizeof(struct SmsMsgHdr_ST));
101 smscore_onresponse(dev->coredev, surb->cb);
104 sms_err("invalid response "
105 "msglen %d actual %d",
106 phdr->msgLength, urb->actual_length);
111 smsusb_submit_urb(dev, surb);
114 static int smsusb_submit_urb(struct smsusb_device_t *dev,
115 struct smsusb_urb_t *surb)
118 surb->cb = smscore_getbuffer(dev->coredev);
120 sms_err("smscore_getbuffer(...) returned NULL");
128 usb_rcvbulkpipe(dev->udev, 0x81),
134 surb->urb.transfer_dma = surb->cb->phys;
135 surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
137 return usb_submit_urb(&surb->urb, GFP_ATOMIC);
140 static void smsusb_stop_streaming(struct smsusb_device_t *dev)
144 for (i = 0; i < MAX_URBS; i++) {
145 usb_kill_urb(&dev->surbs[i].urb);
147 if (dev->surbs[i].cb) {
148 smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
149 dev->surbs[i].cb = NULL;
154 static int smsusb_start_streaming(struct smsusb_device_t *dev)
158 for (i = 0; i < MAX_URBS; i++) {
159 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
161 sms_err("smsusb_submit_urb(...) failed");
162 smsusb_stop_streaming(dev);
170 static int smsusb_sendrequest(void *context, void *buffer, size_t size)
172 struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
175 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
176 buffer, size, &dummy, 1000);
179 static char *smsusb1_fw_lkup[] = {
180 "dvbt_stellar_usb.inp",
181 "dvbh_stellar_usb.inp",
182 "tdmb_stellar_usb.inp",
184 "dvbt_bda_stellar_usb.inp",
187 static inline char *sms_get_fw_name(int mode, int board_id)
189 char **fw = sms_get_board(board_id)->fw;
190 return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
193 static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
195 const struct firmware *fw;
200 if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
201 sms_err("invalid firmware id specified %d", id);
205 fw_filename = sms_get_fw_name(id, board_id);
207 rc = request_firmware(&fw, fw_filename, &udev->dev);
209 sms_warn("failed to open \"%s\" mode %d, "
210 "trying again with default firmware", fw_filename, id);
212 fw_filename = smsusb1_fw_lkup[id];
213 rc = request_firmware(&fw, fw_filename, &udev->dev);
215 sms_warn("failed to open \"%s\" mode %d",
222 fw_buffer = kmalloc(fw->size, GFP_KERNEL);
224 memcpy(fw_buffer, fw->data, fw->size);
226 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
227 fw_buffer, fw->size, &dummy, 1000);
229 sms_info("sent %zd(%d) bytes, rc %d", fw->size, dummy, rc);
233 sms_err("failed to allocate firmware buffer");
236 sms_info("read FW %s, size=%zd", fw_filename, fw->size);
238 release_firmware(fw);
243 static void smsusb1_detectmode(void *context, int *mode)
245 char *product_string =
246 ((struct smsusb_device_t *) context)->udev->product;
248 *mode = DEVICE_MODE_NONE;
250 if (!product_string) {
251 product_string = "none";
252 sms_err("product string not found");
253 } else if (strstr(product_string, "DVBH"))
255 else if (strstr(product_string, "BDA"))
257 else if (strstr(product_string, "DVBT"))
259 else if (strstr(product_string, "TDMB"))
262 sms_info("%d \"%s\"", *mode, product_string);
265 static int smsusb1_setmode(void *context, int mode)
267 struct SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
268 sizeof(struct SmsMsgHdr_ST), 0 };
270 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
271 sms_err("invalid firmware id specified %d", mode);
275 return smsusb_sendrequest(context, &Msg, sizeof(Msg));
278 static void smsusb_term_device(struct usb_interface *intf)
280 struct smsusb_device_t *dev =
281 (struct smsusb_device_t *) usb_get_intfdata(intf);
284 smsusb_stop_streaming(dev);
286 /* unregister from smscore */
288 smscore_unregister_device(dev->coredev);
292 sms_info("device %p destroyed", dev);
295 usb_set_intfdata(intf, NULL);
298 static int smsusb_init_device(struct usb_interface *intf, int board_id)
300 struct smsdevice_params_t params;
301 struct smsusb_device_t *dev;
304 /* create device object */
305 dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
307 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
311 memset(¶ms, 0, sizeof(params));
312 usb_set_intfdata(intf, dev);
313 dev->udev = interface_to_usbdev(intf);
315 params.device_type = sms_get_board(board_id)->type;
317 switch (params.device_type) {
319 dev->buffer_size = USB1_BUFFER_SIZE;
321 params.setmode_handler = smsusb1_setmode;
322 params.detectmode_handler = smsusb1_detectmode;
325 sms_err("Unspecified sms device type!");
330 dev->buffer_size = USB2_BUFFER_SIZE;
331 dev->response_alignment =
332 dev->udev->ep_in[1]->desc.wMaxPacketSize -
333 sizeof(struct SmsMsgHdr_ST);
335 params.flags |= SMS_DEVICE_FAMILY2;
339 params.device = &dev->udev->dev;
340 params.buffer_size = dev->buffer_size;
341 params.num_buffers = MAX_BUFFERS;
342 params.sendrequest_handler = smsusb_sendrequest;
343 params.context = dev;
344 snprintf(params.devpath, sizeof(params.devpath),
345 "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
347 /* register in smscore */
348 rc = smscore_register_device(¶ms, &dev->coredev);
350 sms_err("smscore_register_device(...) failed, rc %d", rc);
351 smsusb_term_device(intf);
355 smscore_set_board_id(dev->coredev, board_id);
357 /* initialize urbs */
358 for (i = 0; i < MAX_URBS; i++) {
359 dev->surbs[i].dev = dev;
360 usb_init_urb(&dev->surbs[i].urb);
363 sms_info("smsusb_start_streaming(...).");
364 rc = smsusb_start_streaming(dev);
366 sms_err("smsusb_start_streaming(...) failed");
367 smsusb_term_device(intf);
371 rc = smscore_start_device(dev->coredev);
373 sms_err("smscore_start_device(...) failed");
374 smsusb_term_device(intf);
378 sms_info("device %p created", dev);
383 static int smsusb_probe(struct usb_interface *intf,
384 const struct usb_device_id *id)
386 struct usb_device *udev = interface_to_usbdev(intf);
390 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
391 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
393 if (intf->num_altsetting > 0) {
394 rc = usb_set_interface(
395 udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
397 sms_err("usb_set_interface failed, rc %d", rc);
402 sms_info("smsusb_probe %d",
403 intf->cur_altsetting->desc.bInterfaceNumber);
404 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
405 sms_info("endpoint %d %02x %02x %d", i,
406 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
407 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
408 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
410 if ((udev->actconfig->desc.bNumInterfaces == 2) &&
411 (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
412 sms_err("rom interface 0 is not used");
416 if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
417 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
418 udev->bus->busnum, udev->devpath);
419 sms_info("stellar device was found.");
420 return smsusb1_load_firmware(
421 udev, smscore_registry_getmode(devpath),
425 rc = smsusb_init_device(intf, id->driver_info);
426 sms_info("rc %d", rc);
430 static void smsusb_disconnect(struct usb_interface *intf)
432 smsusb_term_device(intf);
435 static struct usb_driver smsusb_driver = {
437 .probe = smsusb_probe,
438 .disconnect = smsusb_disconnect,
439 .id_table = smsusb_id_table,
442 int smsusb_register(void)
444 int rc = usb_register(&smsusb_driver);
446 sms_err("usb_register failed. Error number %d", rc);
453 void smsusb_unregister(void)
456 /* Regular USB Cleanup */
457 usb_deregister(&smsusb_driver);