Lines Matching refs:s
171 static int airspy_ctrl_msg(struct airspy *s, u8 request, u16 value, u16 index, in airspy_ctrl_msg() argument
181 pipe = usb_sndctrlpipe(s->udev, 0); in airspy_ctrl_msg()
192 pipe = usb_rcvctrlpipe(s->udev, 0); in airspy_ctrl_msg()
196 dev_err(s->dev, "Unknown command %02x\n", request); in airspy_ctrl_msg()
203 memcpy(s->buf, data, size); in airspy_ctrl_msg()
205 ret = usb_control_msg(s->udev, pipe, request, requesttype, value, in airspy_ctrl_msg()
206 index, s->buf, size, 1000); in airspy_ctrl_msg()
207 airspy_dbg_usb_control_msg(s->dev, request, requesttype, value, in airspy_ctrl_msg()
208 index, s->buf, size); in airspy_ctrl_msg()
210 dev_err(s->dev, "usb_control_msg() failed %d request %02x\n", in airspy_ctrl_msg()
217 memcpy(data, s->buf, size); in airspy_ctrl_msg()
225 static struct airspy_frame_buf *airspy_get_next_fill_buf(struct airspy *s) in airspy_get_next_fill_buf() argument
230 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_get_next_fill_buf()
231 if (list_empty(&s->queued_bufs)) in airspy_get_next_fill_buf()
234 buf = list_entry(s->queued_bufs.next, in airspy_get_next_fill_buf()
238 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_get_next_fill_buf()
242 static unsigned int airspy_convert_stream(struct airspy *s, in airspy_convert_stream() argument
247 if (s->pixelformat == V4L2_SDR_FMT_RU12LE) { in airspy_convert_stream()
255 if (unlikely(time_is_before_jiffies(s->jiffies_next))) { in airspy_convert_stream()
258 s->jiffies_next + msecs_to_jiffies(MSECS)); in airspy_convert_stream()
259 unsigned int samples = s->sample - s->sample_measured; in airspy_convert_stream()
261 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS); in airspy_convert_stream()
262 s->sample_measured = s->sample; in airspy_convert_stream()
263 dev_dbg(s->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n", in airspy_convert_stream()
269 s->sample += src_len / 2; in airspy_convert_stream()
280 struct airspy *s = urb->context; in airspy_urb_complete() local
283 dev_dbg_ratelimited(s->dev, "status=%d length=%d/%d errors=%d\n", in airspy_urb_complete()
296 dev_err_ratelimited(s->dev, "URB failed %d\n", urb->status); in airspy_urb_complete()
304 fbuf = airspy_get_next_fill_buf(s); in airspy_urb_complete()
306 s->vb_full++; in airspy_urb_complete()
307 dev_notice_ratelimited(s->dev, in airspy_urb_complete()
309 s->vb_full); in airspy_urb_complete()
315 len = airspy_convert_stream(s, ptr, urb->transfer_buffer, in airspy_urb_complete()
319 fbuf->vb.sequence = s->sequence++; in airspy_urb_complete()
326 static int airspy_kill_urbs(struct airspy *s) in airspy_kill_urbs() argument
330 for (i = s->urbs_submitted - 1; i >= 0; i--) { in airspy_kill_urbs()
331 dev_dbg(s->dev, "kill urb=%d\n", i); in airspy_kill_urbs()
333 usb_kill_urb(s->urb_list[i]); in airspy_kill_urbs()
335 s->urbs_submitted = 0; in airspy_kill_urbs()
340 static int airspy_submit_urbs(struct airspy *s) in airspy_submit_urbs() argument
344 for (i = 0; i < s->urbs_initialized; i++) { in airspy_submit_urbs()
345 dev_dbg(s->dev, "submit urb=%d\n", i); in airspy_submit_urbs()
346 ret = usb_submit_urb(s->urb_list[i], GFP_ATOMIC); in airspy_submit_urbs()
348 dev_err(s->dev, "Could not submit URB no. %d - get them all back\n", in airspy_submit_urbs()
350 airspy_kill_urbs(s); in airspy_submit_urbs()
353 s->urbs_submitted++; in airspy_submit_urbs()
359 static int airspy_free_stream_bufs(struct airspy *s) in airspy_free_stream_bufs() argument
361 if (test_bit(USB_STATE_URB_BUF, &s->flags)) { in airspy_free_stream_bufs()
362 while (s->buf_num) { in airspy_free_stream_bufs()
363 s->buf_num--; in airspy_free_stream_bufs()
364 dev_dbg(s->dev, "free buf=%d\n", s->buf_num); in airspy_free_stream_bufs()
365 usb_free_coherent(s->udev, s->buf_size, in airspy_free_stream_bufs()
366 s->buf_list[s->buf_num], in airspy_free_stream_bufs()
367 s->dma_addr[s->buf_num]); in airspy_free_stream_bufs()
370 clear_bit(USB_STATE_URB_BUF, &s->flags); in airspy_free_stream_bufs()
375 static int airspy_alloc_stream_bufs(struct airspy *s) in airspy_alloc_stream_bufs() argument
377 s->buf_num = 0; in airspy_alloc_stream_bufs()
378 s->buf_size = BULK_BUFFER_SIZE; in airspy_alloc_stream_bufs()
380 dev_dbg(s->dev, "all in all I will use %u bytes for streaming\n", in airspy_alloc_stream_bufs()
383 for (s->buf_num = 0; s->buf_num < MAX_BULK_BUFS; s->buf_num++) { in airspy_alloc_stream_bufs()
384 s->buf_list[s->buf_num] = usb_alloc_coherent(s->udev, in airspy_alloc_stream_bufs()
386 &s->dma_addr[s->buf_num]); in airspy_alloc_stream_bufs()
387 if (!s->buf_list[s->buf_num]) { in airspy_alloc_stream_bufs()
388 dev_dbg(s->dev, "alloc buf=%d failed\n", s->buf_num); in airspy_alloc_stream_bufs()
389 airspy_free_stream_bufs(s); in airspy_alloc_stream_bufs()
393 dev_dbg(s->dev, "alloc buf=%d %p (dma %llu)\n", s->buf_num, in airspy_alloc_stream_bufs()
394 s->buf_list[s->buf_num], in airspy_alloc_stream_bufs()
395 (long long)s->dma_addr[s->buf_num]); in airspy_alloc_stream_bufs()
396 set_bit(USB_STATE_URB_BUF, &s->flags); in airspy_alloc_stream_bufs()
402 static int airspy_free_urbs(struct airspy *s) in airspy_free_urbs() argument
406 airspy_kill_urbs(s); in airspy_free_urbs()
408 for (i = s->urbs_initialized - 1; i >= 0; i--) { in airspy_free_urbs()
409 if (s->urb_list[i]) { in airspy_free_urbs()
410 dev_dbg(s->dev, "free urb=%d\n", i); in airspy_free_urbs()
412 usb_free_urb(s->urb_list[i]); in airspy_free_urbs()
415 s->urbs_initialized = 0; in airspy_free_urbs()
420 static int airspy_alloc_urbs(struct airspy *s) in airspy_alloc_urbs() argument
426 dev_dbg(s->dev, "alloc urb=%d\n", i); in airspy_alloc_urbs()
427 s->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC); in airspy_alloc_urbs()
428 if (!s->urb_list[i]) { in airspy_alloc_urbs()
430 usb_free_urb(s->urb_list[j]); in airspy_alloc_urbs()
433 usb_fill_bulk_urb(s->urb_list[i], in airspy_alloc_urbs()
434 s->udev, in airspy_alloc_urbs()
435 usb_rcvbulkpipe(s->udev, 0x81), in airspy_alloc_urbs()
436 s->buf_list[i], in airspy_alloc_urbs()
438 airspy_urb_complete, s); in airspy_alloc_urbs()
440 s->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; in airspy_alloc_urbs()
441 s->urb_list[i]->transfer_dma = s->dma_addr[i]; in airspy_alloc_urbs()
442 s->urbs_initialized++; in airspy_alloc_urbs()
449 static void airspy_cleanup_queued_bufs(struct airspy *s) in airspy_cleanup_queued_bufs() argument
453 dev_dbg(s->dev, "\n"); in airspy_cleanup_queued_bufs()
455 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_cleanup_queued_bufs()
456 while (!list_empty(&s->queued_bufs)) { in airspy_cleanup_queued_bufs()
459 buf = list_entry(s->queued_bufs.next, in airspy_cleanup_queued_bufs()
464 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_cleanup_queued_bufs()
471 struct airspy *s = container_of(v, struct airspy, v4l2_dev); in airspy_disconnect() local
473 dev_dbg(s->dev, "\n"); in airspy_disconnect()
475 mutex_lock(&s->vb_queue_lock); in airspy_disconnect()
476 mutex_lock(&s->v4l2_lock); in airspy_disconnect()
478 s->udev = NULL; in airspy_disconnect()
479 v4l2_device_disconnect(&s->v4l2_dev); in airspy_disconnect()
480 video_unregister_device(&s->vdev); in airspy_disconnect()
481 mutex_unlock(&s->v4l2_lock); in airspy_disconnect()
482 mutex_unlock(&s->vb_queue_lock); in airspy_disconnect()
484 v4l2_device_put(&s->v4l2_dev); in airspy_disconnect()
492 struct airspy *s = vb2_get_drv_priv(vq); in airspy_queue_setup() local
494 dev_dbg(s->dev, "nbuffers=%d\n", *nbuffers); in airspy_queue_setup()
500 sizes[0] = PAGE_ALIGN(s->buffersize); in airspy_queue_setup()
502 dev_dbg(s->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]); in airspy_queue_setup()
509 struct airspy *s = vb2_get_drv_priv(vb->vb2_queue); in airspy_buf_queue() local
515 if (unlikely(!s->udev)) { in airspy_buf_queue()
520 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_buf_queue()
521 list_add_tail(&buf->list, &s->queued_bufs); in airspy_buf_queue()
522 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_buf_queue()
527 struct airspy *s = vb2_get_drv_priv(vq); in airspy_start_streaming() local
530 dev_dbg(s->dev, "\n"); in airspy_start_streaming()
532 if (!s->udev) in airspy_start_streaming()
535 mutex_lock(&s->v4l2_lock); in airspy_start_streaming()
537 s->sequence = 0; in airspy_start_streaming()
539 set_bit(POWER_ON, &s->flags); in airspy_start_streaming()
541 ret = airspy_alloc_stream_bufs(s); in airspy_start_streaming()
545 ret = airspy_alloc_urbs(s); in airspy_start_streaming()
549 ret = airspy_submit_urbs(s); in airspy_start_streaming()
554 ret = airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 1, 0, NULL, 0); in airspy_start_streaming()
561 airspy_kill_urbs(s); in airspy_start_streaming()
563 airspy_free_urbs(s); in airspy_start_streaming()
565 airspy_free_stream_bufs(s); in airspy_start_streaming()
567 clear_bit(POWER_ON, &s->flags); in airspy_start_streaming()
573 list_for_each_entry_safe(buf, tmp, &s->queued_bufs, list) { in airspy_start_streaming()
581 mutex_unlock(&s->v4l2_lock); in airspy_start_streaming()
588 struct airspy *s = vb2_get_drv_priv(vq); in airspy_stop_streaming() local
590 dev_dbg(s->dev, "\n"); in airspy_stop_streaming()
592 mutex_lock(&s->v4l2_lock); in airspy_stop_streaming()
595 airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 0, 0, NULL, 0); in airspy_stop_streaming()
597 airspy_kill_urbs(s); in airspy_stop_streaming()
598 airspy_free_urbs(s); in airspy_stop_streaming()
599 airspy_free_stream_bufs(s); in airspy_stop_streaming()
601 airspy_cleanup_queued_bufs(s); in airspy_stop_streaming()
603 clear_bit(POWER_ON, &s->flags); in airspy_stop_streaming()
605 mutex_unlock(&s->v4l2_lock); in airspy_stop_streaming()
620 struct airspy *s = video_drvdata(file); in airspy_querycap() local
623 strlcpy(cap->card, s->vdev.name, sizeof(cap->card)); in airspy_querycap()
624 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); in airspy_querycap()
647 struct airspy *s = video_drvdata(file); in airspy_g_fmt_sdr_cap() local
649 f->fmt.sdr.pixelformat = s->pixelformat; in airspy_g_fmt_sdr_cap()
650 f->fmt.sdr.buffersize = s->buffersize; in airspy_g_fmt_sdr_cap()
659 struct airspy *s = video_drvdata(file); in airspy_s_fmt_sdr_cap() local
660 struct vb2_queue *q = &s->vb_queue; in airspy_s_fmt_sdr_cap()
669 s->pixelformat = formats[i].pixelformat; in airspy_s_fmt_sdr_cap()
670 s->buffersize = formats[i].buffersize; in airspy_s_fmt_sdr_cap()
676 s->pixelformat = formats[0].pixelformat; in airspy_s_fmt_sdr_cap()
677 s->buffersize = formats[0].buffersize; in airspy_s_fmt_sdr_cap()
746 struct airspy *s = video_drvdata(file); in airspy_g_frequency() local
751 f->frequency = s->f_adc; in airspy_g_frequency()
752 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc); in airspy_g_frequency()
756 f->frequency = s->f_rf; in airspy_g_frequency()
757 dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf); in airspy_g_frequency()
769 struct airspy *s = video_drvdata(file); in airspy_s_frequency() local
774 s->f_adc = clamp_t(unsigned int, f->frequency, in airspy_s_frequency()
777 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc); in airspy_s_frequency()
780 s->f_rf = clamp_t(unsigned int, f->frequency, in airspy_s_frequency()
783 dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf); in airspy_s_frequency()
784 buf[0] = (s->f_rf >> 0) & 0xff; in airspy_s_frequency()
785 buf[1] = (s->f_rf >> 8) & 0xff; in airspy_s_frequency()
786 buf[2] = (s->f_rf >> 16) & 0xff; in airspy_s_frequency()
787 buf[3] = (s->f_rf >> 24) & 0xff; in airspy_s_frequency()
788 ret = airspy_ctrl_msg(s, CMD_SET_FREQ, 0, 0, buf, 4); in airspy_s_frequency()
871 struct airspy *s = container_of(v, struct airspy, v4l2_dev); in airspy_video_release() local
873 v4l2_ctrl_handler_free(&s->hdl); in airspy_video_release()
874 v4l2_device_unregister(&s->v4l2_dev); in airspy_video_release()
875 kfree(s); in airspy_video_release()
878 static int airspy_set_lna_gain(struct airspy *s) in airspy_set_lna_gain() argument
883 dev_dbg(s->dev, "lna auto=%d->%d val=%d->%d\n", in airspy_set_lna_gain()
884 s->lna_gain_auto->cur.val, s->lna_gain_auto->val, in airspy_set_lna_gain()
885 s->lna_gain->cur.val, s->lna_gain->val); in airspy_set_lna_gain()
887 ret = airspy_ctrl_msg(s, CMD_SET_LNA_AGC, 0, s->lna_gain_auto->val, in airspy_set_lna_gain()
892 if (s->lna_gain_auto->val == false) { in airspy_set_lna_gain()
893 ret = airspy_ctrl_msg(s, CMD_SET_LNA_GAIN, 0, s->lna_gain->val, in airspy_set_lna_gain()
900 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_lna_gain()
905 static int airspy_set_mixer_gain(struct airspy *s) in airspy_set_mixer_gain() argument
910 dev_dbg(s->dev, "mixer auto=%d->%d val=%d->%d\n", in airspy_set_mixer_gain()
911 s->mixer_gain_auto->cur.val, s->mixer_gain_auto->val, in airspy_set_mixer_gain()
912 s->mixer_gain->cur.val, s->mixer_gain->val); in airspy_set_mixer_gain()
914 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_AGC, 0, s->mixer_gain_auto->val, in airspy_set_mixer_gain()
919 if (s->mixer_gain_auto->val == false) { in airspy_set_mixer_gain()
920 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_GAIN, 0, in airspy_set_mixer_gain()
921 s->mixer_gain->val, &u8tmp, 1); in airspy_set_mixer_gain()
927 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_mixer_gain()
932 static int airspy_set_if_gain(struct airspy *s) in airspy_set_if_gain() argument
937 dev_dbg(s->dev, "val=%d->%d\n", s->if_gain->cur.val, s->if_gain->val); in airspy_set_if_gain()
939 ret = airspy_ctrl_msg(s, CMD_SET_VGA_GAIN, 0, s->if_gain->val, in airspy_set_if_gain()
942 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_if_gain()
949 struct airspy *s = container_of(ctrl->handler, struct airspy, hdl); in airspy_s_ctrl() local
955 ret = airspy_set_lna_gain(s); in airspy_s_ctrl()
959 ret = airspy_set_mixer_gain(s); in airspy_s_ctrl()
962 ret = airspy_set_if_gain(s); in airspy_s_ctrl()
965 dev_dbg(s->dev, "unknown ctrl: id=%d name=%s\n", in airspy_s_ctrl()
980 struct airspy *s; in airspy_probe() local
984 s = kzalloc(sizeof(struct airspy), GFP_KERNEL); in airspy_probe()
985 if (s == NULL) { in airspy_probe()
990 mutex_init(&s->v4l2_lock); in airspy_probe()
991 mutex_init(&s->vb_queue_lock); in airspy_probe()
992 spin_lock_init(&s->queued_bufs_lock); in airspy_probe()
993 INIT_LIST_HEAD(&s->queued_bufs); in airspy_probe()
994 s->dev = &intf->dev; in airspy_probe()
995 s->udev = interface_to_usbdev(intf); in airspy_probe()
996 s->f_adc = bands[0].rangelow; in airspy_probe()
997 s->f_rf = bands_rf[0].rangelow; in airspy_probe()
998 s->pixelformat = formats[0].pixelformat; in airspy_probe()
999 s->buffersize = formats[0].buffersize; in airspy_probe()
1002 ret = airspy_ctrl_msg(s, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1); in airspy_probe()
1004 ret = airspy_ctrl_msg(s, CMD_VERSION_STRING_READ, 0, 0, in airspy_probe()
1007 dev_err(s->dev, "Could not detect board\n"); in airspy_probe()
1013 dev_info(s->dev, "Board ID: %02x\n", u8tmp); in airspy_probe()
1014 dev_info(s->dev, "Firmware version: %s\n", buf); in airspy_probe()
1017 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; in airspy_probe()
1018 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; in airspy_probe()
1019 s->vb_queue.drv_priv = s; in airspy_probe()
1020 s->vb_queue.buf_struct_size = sizeof(struct airspy_frame_buf); in airspy_probe()
1021 s->vb_queue.ops = &airspy_vb2_ops; in airspy_probe()
1022 s->vb_queue.mem_ops = &vb2_vmalloc_memops; in airspy_probe()
1023 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; in airspy_probe()
1024 ret = vb2_queue_init(&s->vb_queue); in airspy_probe()
1026 dev_err(s->dev, "Could not initialize vb2 queue\n"); in airspy_probe()
1031 s->vdev = airspy_template; in airspy_probe()
1032 s->vdev.queue = &s->vb_queue; in airspy_probe()
1033 s->vdev.queue->lock = &s->vb_queue_lock; in airspy_probe()
1034 video_set_drvdata(&s->vdev, s); in airspy_probe()
1037 s->v4l2_dev.release = airspy_video_release; in airspy_probe()
1038 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev); in airspy_probe()
1040 dev_err(s->dev, "Failed to register v4l2-device (%d)\n", ret); in airspy_probe()
1045 v4l2_ctrl_handler_init(&s->hdl, 5); in airspy_probe()
1046 s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1048 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1050 v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false); in airspy_probe()
1051 s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1053 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1055 v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false); in airspy_probe()
1056 s->if_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1058 if (s->hdl.error) { in airspy_probe()
1059 ret = s->hdl.error; in airspy_probe()
1060 dev_err(s->dev, "Could not initialize controls\n"); in airspy_probe()
1064 v4l2_ctrl_handler_setup(&s->hdl); in airspy_probe()
1066 s->v4l2_dev.ctrl_handler = &s->hdl; in airspy_probe()
1067 s->vdev.v4l2_dev = &s->v4l2_dev; in airspy_probe()
1068 s->vdev.lock = &s->v4l2_lock; in airspy_probe()
1070 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1); in airspy_probe()
1072 dev_err(s->dev, "Failed to register as video device (%d)\n", in airspy_probe()
1076 dev_info(s->dev, "Registered as %s\n", in airspy_probe()
1077 video_device_node_name(&s->vdev)); in airspy_probe()
1078 …dev_notice(s->dev, "SDR API is still slightly experimental and functionality changes may follow\n"… in airspy_probe()
1082 v4l2_ctrl_handler_free(&s->hdl); in airspy_probe()
1083 v4l2_device_unregister(&s->v4l2_dev); in airspy_probe()
1085 kfree(s); in airspy_probe()