Error Trace

[Home]

Bug # 45

Show/hide error trace
Error trace
Function bodies
Blocks
  • Others...
    Function bodies without model function calls
    Initialization function calls
    Initialization function bodies
    Entry point
    Entry point body
    Function calls
    Skipped function calls
    Formal parameter names
    Declarations
    Assumes
    Assume conditions
    Returns
    Return values
    DEG initialization
    DEG function calls
    Model function calls
    Model function bodies
    Model asserts
    Model state changes
    Model function function calls
    Model function function bodies
    Model returns
    Model others
    Identation
    Line numbers
    Expand signs
-__BLAST_initialize_/home/ldvuser/ref_launch/work/current--X--drivers--X--defaultlinux-3.16-rc1.tar.xz--X--132_1a/linux-3.16-rc1.tar.xz/csd_deg_dscv/8832/dscv_tempdir/dscv/rcv/132_1a/main-ldv_main0_sequence_infinite_withcheck_stateful/preprocess/1-cpp/drivers/media/usb/usbtv/usbtv-core.o.i()
{
return ;
}
-__BLAST_initialize_/home/ldvuser/ref_launch/work/current--X--drivers--X--defaultlinux-3.16-rc1.tar.xz--X--132_1a/linux-3.16-rc1.tar.xz/csd_deg_dscv/8832/dscv_tempdir/dscv/rcv/132_1a/main-ldv_main0_sequence_infinite_withcheck_stateful/preprocess/1-cpp/drivers/media/usb/usbtv/usbtv-video.o.i()
{
37 -norm_params[ 0 ].norm = 63744;
norm_params[ 0 ].cap_width = 720;
norm_params[ 0 ].cap_height = 480;
norm_params[ 1 ].norm = 255;
norm_params[ 1 ].cap_width = 720;
norm_params[ 1 ].cap_height = 576;
usbtv_ioctl_ops.vidioc_querycap = &(usbtv_querycap);
usbtv_ioctl_ops.vidioc_g_priority = 0;
usbtv_ioctl_ops.vidioc_s_priority = 0;
usbtv_ioctl_ops.vidioc_enum_fmt_vid_cap = &(usbtv_enum_fmt_vid_cap);
usbtv_ioctl_ops.vidioc_enum_fmt_vid_overlay = 0;
usbtv_ioctl_ops.vidioc_enum_fmt_vid_out = 0;
usbtv_ioctl_ops.vidioc_enum_fmt_vid_cap_mplane = 0;
usbtv_ioctl_ops.vidioc_enum_fmt_vid_out_mplane = 0;
usbtv_ioctl_ops.vidioc_enum_fmt_sdr_cap = 0;
usbtv_ioctl_ops.vidioc_g_fmt_vid_cap = &(usbtv_fmt_vid_cap);
usbtv_ioctl_ops.vidioc_g_fmt_vid_overlay = 0;
usbtv_ioctl_ops.vidioc_g_fmt_vid_out = 0;
usbtv_ioctl_ops.vidioc_g_fmt_vid_out_overlay = 0;
usbtv_ioctl_ops.vidioc_g_fmt_vbi_cap = 0;
usbtv_ioctl_ops.vidioc_g_fmt_vbi_out = 0;
usbtv_ioctl_ops.vidioc_g_fmt_sliced_vbi_cap = 0;
usbtv_ioctl_ops.vidioc_g_fmt_sliced_vbi_out = 0;
usbtv_ioctl_ops.vidioc_g_fmt_vid_cap_mplane = 0;
usbtv_ioctl_ops.vidioc_g_fmt_vid_out_mplane = 0;
usbtv_ioctl_ops.vidioc_g_fmt_sdr_cap = 0;
usbtv_ioctl_ops.vidioc_s_fmt_vid_cap = &(usbtv_fmt_vid_cap);
usbtv_ioctl_ops.vidioc_s_fmt_vid_overlay = 0;
usbtv_ioctl_ops.vidioc_s_fmt_vid_out = 0;
usbtv_ioctl_ops.vidioc_s_fmt_vid_out_overlay = 0;
usbtv_ioctl_ops.vidioc_s_fmt_vbi_cap = 0;
usbtv_ioctl_ops.vidioc_s_fmt_vbi_out = 0;
usbtv_ioctl_ops.vidioc_s_fmt_sliced_vbi_cap = 0;
usbtv_ioctl_ops.vidioc_s_fmt_sliced_vbi_out = 0;
usbtv_ioctl_ops.vidioc_s_fmt_vid_cap_mplane = 0;
usbtv_ioctl_ops.vidioc_s_fmt_vid_out_mplane = 0;
usbtv_ioctl_ops.vidioc_s_fmt_sdr_cap = 0;
usbtv_ioctl_ops.vidioc_try_fmt_vid_cap = &(usbtv_fmt_vid_cap);
usbtv_ioctl_ops.vidioc_try_fmt_vid_overlay = 0;
usbtv_ioctl_ops.vidioc_try_fmt_vid_out = 0;
usbtv_ioctl_ops.vidioc_try_fmt_vid_out_overlay = 0;
usbtv_ioctl_ops.vidioc_try_fmt_vbi_cap = 0;
usbtv_ioctl_ops.vidioc_try_fmt_vbi_out = 0;
usbtv_ioctl_ops.vidioc_try_fmt_sliced_vbi_cap = 0;
usbtv_ioctl_ops.vidioc_try_fmt_sliced_vbi_out = 0;
usbtv_ioctl_ops.vidioc_try_fmt_vid_cap_mplane = 0;
usbtv_ioctl_ops.vidioc_try_fmt_vid_out_mplane = 0;
usbtv_ioctl_ops.vidioc_try_fmt_sdr_cap = 0;
usbtv_ioctl_ops.vidioc_reqbufs = &(vb2_ioctl_reqbufs);
usbtv_ioctl_ops.vidioc_querybuf = &(vb2_ioctl_querybuf);
usbtv_ioctl_ops.vidioc_qbuf = &(vb2_ioctl_qbuf);
usbtv_ioctl_ops.vidioc_expbuf = 0;
usbtv_ioctl_ops.vidioc_dqbuf = &(vb2_ioctl_dqbuf);
usbtv_ioctl_ops.vidioc_create_bufs = &(vb2_ioctl_create_bufs);
usbtv_ioctl_ops.vidioc_prepare_buf = &(vb2_ioctl_prepare_buf);
usbtv_ioctl_ops.vidioc_overlay = 0;
usbtv_ioctl_ops.vidioc_g_fbuf = 0;
usbtv_ioctl_ops.vidioc_s_fbuf = 0;
usbtv_ioctl_ops.vidioc_streamon = &(vb2_ioctl_streamon);
usbtv_ioctl_ops.vidioc_streamoff = &(vb2_ioctl_streamoff);
usbtv_ioctl_ops.vidioc_g_std = &(usbtv_g_std);
usbtv_ioctl_ops.vidioc_s_std = &(usbtv_s_std);
usbtv_ioctl_ops.vidioc_querystd = 0;
usbtv_ioctl_ops.vidioc_enum_input = &(usbtv_enum_input);
usbtv_ioctl_ops.vidioc_g_input = &(usbtv_g_input);
usbtv_ioctl_ops.vidioc_s_input = &(usbtv_s_input);
usbtv_ioctl_ops.vidioc_enum_output = 0;
usbtv_ioctl_ops.vidioc_g_output = 0;
usbtv_ioctl_ops.vidioc_s_output = 0;
usbtv_ioctl_ops.vidioc_queryctrl = 0;
usbtv_ioctl_ops.vidioc_g_ctrl = 0;
usbtv_ioctl_ops.vidioc_s_ctrl = 0;
usbtv_ioctl_ops.vidioc_g_ext_ctrls = 0;
usbtv_ioctl_ops.vidioc_s_ext_ctrls = 0;
usbtv_ioctl_ops.vidioc_try_ext_ctrls = 0;
usbtv_ioctl_ops.vidioc_querymenu = 0;
usbtv_ioctl_ops.vidioc_enumaudio = 0;
usbtv_ioctl_ops.vidioc_g_audio = 0;
usbtv_ioctl_ops.vidioc_s_audio = 0;
usbtv_ioctl_ops.vidioc_enumaudout = 0;
usbtv_ioctl_ops.vidioc_g_audout = 0;
usbtv_ioctl_ops.vidioc_s_audout = 0;
usbtv_ioctl_ops.vidioc_g_modulator = 0;
usbtv_ioctl_ops.vidioc_s_modulator = 0;
usbtv_ioctl_ops.vidioc_cropcap = 0;
usbtv_ioctl_ops.vidioc_g_crop = 0;
usbtv_ioctl_ops.vidioc_s_crop = 0;
usbtv_ioctl_ops.vidioc_g_selection = 0;
usbtv_ioctl_ops.vidioc_s_selection = 0;
usbtv_ioctl_ops.vidioc_g_jpegcomp = 0;
usbtv_ioctl_ops.vidioc_s_jpegcomp = 0;
usbtv_ioctl_ops.vidioc_g_enc_index = 0;
usbtv_ioctl_ops.vidioc_encoder_cmd = 0;
usbtv_ioctl_ops.vidioc_try_encoder_cmd = 0;
usbtv_ioctl_ops.vidioc_decoder_cmd = 0;
usbtv_ioctl_ops.vidioc_try_decoder_cmd = 0;
usbtv_ioctl_ops.vidioc_g_parm = 0;
usbtv_ioctl_ops.vidioc_s_parm = 0;
usbtv_ioctl_ops.vidioc_g_tuner = 0;
usbtv_ioctl_ops.vidioc_s_tuner = 0;
usbtv_ioctl_ops.vidioc_g_frequency = 0;
usbtv_ioctl_ops.vidioc_s_frequency = 0;
usbtv_ioctl_ops.vidioc_enum_freq_bands = 0;
usbtv_ioctl_ops.vidioc_g_sliced_vbi_cap = 0;
usbtv_ioctl_ops.vidioc_log_status = 0;
usbtv_ioctl_ops.vidioc_s_hw_freq_seek = 0;
usbtv_ioctl_ops.vidioc_g_register = 0;
usbtv_ioctl_ops.vidioc_s_register = 0;
usbtv_ioctl_ops.vidioc_g_chip_info = 0;
usbtv_ioctl_ops.vidioc_enum_framesizes = 0;
usbtv_ioctl_ops.vidioc_enum_frameintervals = 0;
usbtv_ioctl_ops.vidioc_s_dv_timings = 0;
usbtv_ioctl_ops.vidioc_g_dv_timings = 0;
usbtv_ioctl_ops.vidioc_query_dv_timings = 0;
usbtv_ioctl_ops.vidioc_enum_dv_timings = 0;
usbtv_ioctl_ops.vidioc_dv_timings_cap = 0;
usbtv_ioctl_ops.vidioc_g_edid = 0;
usbtv_ioctl_ops.vidioc_s_edid = 0;
usbtv_ioctl_ops.vidioc_subscribe_event = 0;
usbtv_ioctl_ops.vidioc_unsubscribe_event = 0;
usbtv_ioctl_ops.vidioc_default = 0;
usbtv_fops.owner = &(__this_module);
usbtv_fops.read = &(vb2_fop_read);
usbtv_fops.write = 0;
usbtv_fops.poll = &(vb2_fop_poll);
usbtv_fops.ioctl = 0;
usbtv_fops.unlocked_ioctl = &(video_ioctl2);
usbtv_fops.compat_ioctl32 = 0;
usbtv_fops.get_unmapped_area = 0;
usbtv_fops.mmap = &(vb2_fop_mmap);
usbtv_fops.open = &(v4l2_fh_open);
usbtv_fops.release = &(vb2_fop_release);
usbtv_vb2_ops.queue_setup = &(usbtv_queue_setup);
usbtv_vb2_ops.wait_prepare = 0;
usbtv_vb2_ops.wait_finish = 0;
usbtv_vb2_ops.buf_init = 0;
usbtv_vb2_ops.buf_prepare = 0;
usbtv_vb2_ops.buf_finish = 0;
usbtv_vb2_ops.buf_cleanup = 0;
usbtv_vb2_ops.start_streaming = &(usbtv_start_streaming);
usbtv_vb2_ops.stop_streaming = &(usbtv_stop_streaming);
usbtv_vb2_ops.buf_queue = &(usbtv_buf_queue);
return ;
}
-__BLAST_initialize_/home/ldvuser/ref_launch/work/current--X--drivers--X--defaultlinux-3.16-rc1.tar.xz--X--132_1a/linux-3.16-rc1.tar.xz/csd_deg_dscv/8832/dscv_tempdir/dscv/rcv/132_1a/main-ldv_main0_sequence_infinite_withcheck_stateful/preprocess/1-cpp//home/ldvuser/ref_launch/work/current--X--drivers--X--defaultlinux-3.16-rc1.tar.xz--X--132_1a/linux-3.16-rc1.tar.xz/csd_deg_dscv/8832/dscv_tempdir/rule-instrumentor/132_1a/common-model/ldv_common_model.o.i()
{
19 ldv_usb_dev_state = 0;
return ;
}
-entry_point
{
208 -ldv_s_usbtv_usb_driver_usb_driver = 0;
LDV_IN_INTERRUPT = 1;
207 ldv_initialize() { /* Function call is skipped due to function is undefined */}
211 tmp___0 = nondet_int() { /* Function call is skipped due to function is undefined */}
211 assume(tmp___0 != 0);
215 tmp = nondet_int() { /* Function call is skipped due to function is undefined */}
217 assume(tmp == 0);
220 assume(ldv_s_usbtv_usb_driver_usb_driver == 0);
225 -res_usbtv_probe_0 = usbtv_probe(var_group1 /* intf */, var_usbtv_probe_0_p1 /* id */)
{
59 dev = &(intf)->dev;
63 assume(*(intf).num_altsetting == 2);
65 assume(*(*(intf).altsetting + 1).desc.bNumEndpoints == 4);
70 cil_11 = *(*(intf).altsetting + 1).endpoint;
70 -size = usb_endpoint_maxp(&(cil_11)->desc /* epd */)
{
605 __retres2 = *(epd).wMaxPacketSize;
603 return __retres2;
}
71 size = size & 2047 * size & 6144 >> 11 + 1;
74 -tmp = kzalloc(3504 /* size */, 208 /* flags */)
{
639 -tmp = kmalloc(size /* size */, flags | 32768 /* flags */)
{
462 tmp___2 = __kmalloc(size, flags) { /* Function call is skipped due to function is undefined */}
462 return tmp___2;
}
639 return tmp;
}
74 usbtv = tmp;
75 assume(usbtv != 0);
77 *(usbtv).dev = dev;
78 -tmp___0 = interface_to_usbdev(intf /* intf */)
{
46 -tmp = ldv_interface_to_usbdev_5(intf /* intf */)
{
597 __mptr = *(intf).dev.parent;
597 __retres3 = __mptr + -144;
595 return __retres3;
}
46 ldv_func_res = tmp;
48 -ldv_interface_to_usbdev()
{
25 assume(ldv_usb_dev_state == 0);
27 ldv_usb_dev_state = 1;
22 return ;
}
50 return ldv_func_res;
}
78 -*(usbtv).udev = ldv_usb_get_dev_6(tmp___0 /* ldv_func_arg1 */)
{
57 tmp = usb_get_dev(ldv_func_arg1) { /* Function call is skipped due to function is undefined */}
57 ldv_func_res = tmp;
59 -ldv_usb_get_dev()
{
34 assume(ldv_usb_dev_state > 0);
39 ldv_usb_dev_state = ldv_usb_dev_state + 1;
31 return ;
}
61 return ldv_func_res;
}
80 *(usbtv).iso_size = size;
82 -usb_set_intfdata(intf /* intf */, usbtv /* data */)
{
201 -dev_set_drvdata(&(intf)->dev /* dev */, data /* data */)
{
844 *(dev).driver_data = data;
842 return ;
}
199 return ;
}
84 -ret = usbtv_video_init(usbtv /* usbtv */)
{
666 -usbtv_configure_for_norm(usbtv /* usbtv */, 63744 /* norm */)
{
52 -ret = 0;
params = 0;
i = 0;
55 assume(i <= 1);
56 assume(norm_params[ i ].norm & norm != 0);
57 params = &(norm_params) + i;
62 assume(params != 0);
63 -*(usbtv).width = *(params).cap_width;
*(usbtv).height = *(params).cap_height;
*(usbtv).n_chunks = *(usbtv).width * *(usbtv).height / 960;
*(usbtv).norm = *(params).norm;
71 return ret;
}
668 -spinlock_check(&(usbtv)->buflock /* lock */)
{
292 __retres2 = &(&(lock)->__annonCompField19)->rlock;
290 return __retres2;
}
668 __raw_spin_lock_init(&(&(&(usbtv)->buflock)->__annonCompField19)->rlock, "&(&usbtv->buflock)->rlock", &(__key)) { /* Function call is skipped due to function is undefined */}
669 __mutex_init(&(usbtv)->v4l2_lock, "&usbtv->v4l2_lock", &(__key___0)) { /* Function call is skipped due to function is undefined */}
670 __mutex_init(&(usbtv)->vb2q_lock, "&usbtv->vb2q_lock", &(__key___1)) { /* Function call is skipped due to function is undefined */}
671 -INIT_LIST_HEAD(&(usbtv)->bufs /* list */)
{
26 -*(list).next = list;
*(list).prev = list;
24 return ;
}
674 -*(usbtv).vb2q.type = 1;
*(usbtv).vb2q.io_modes = 7;
*(usbtv).vb2q.drv_priv = usbtv;
*(usbtv).vb2q.buf_struct_size = 944;
*(usbtv).vb2q.ops = &(usbtv_vb2_ops);
*(usbtv).vb2q.mem_ops = &(vb2_vmalloc_memops);
*(usbtv).vb2q.timestamp_flags = 8192;
*(usbtv).vb2q.lock = &(usbtv)->vb2q_lock;
682 ret = vb2_queue_init(&(usbtv)->vb2q) { /* Function call is skipped due to function is undefined */}
683 assume(ret < 0);
684 dev_warn(*(usbtv).dev, "Could not initialize videobuf2 queue\n") { /* Function call is skipped due to function is undefined */}
685 __retres6 = ret;
662 return __retres6;
}
85 assume(ret < 0);
95 kfree(usbtv) { /* Function call is skipped due to function is undefined */}
97 __retres12 = ret;
54 return __retres12;
}
226 ldv_check_return_value(res_usbtv_probe_0) { /* Function call is skipped due to function is undefined */}
227 -ldv_check_return_value_probe(res_usbtv_probe_0 /* retval */)
{
60 assume(retval != 0);
62 assume(ldv_usb_dev_state > 1);
62 -ldv_error()
{
}
}
}
Source code
1 2 /* 3 * Fushicai USBTV007 Video Grabber Driver 4 * 5 * Product web site: 6 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html 7 * 8 * Following LWN articles were very useful in construction of this driver: 9 * Video4Linux2 API series: http://lwn.net/Articles/203924/ 10 * videobuf2 API explanation: http://lwn.net/Articles/447435/ 11 * Thanks go to Jonathan Corbet for providing this quality documentation. 12 * He is awesome. 13 * 14 * Copyright (c) 2013 Lubomir Rintel 15 * All rights reserved. 16 * No physical hardware was harmed running Windows during the 17 * reverse-engineering activity 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions, and the following disclaimer, 24 * without modification. 25 * 2. The name of the author may not be used to endorse or promote products 26 * derived from this software without specific prior written permission. 27 * 28 * Alternatively, this software may be distributed under the terms of the 29 * GNU General Public License ("GPL"). 30 */ 31 32 #include "usbtv.h" 33 34 int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size) 35 { 36 int ret; 37 int pipe = usb_rcvctrlpipe(usbtv->udev, 0); 38 int i; 39 40 for (i = 0; i < size; i++) { 41 u16 index = regs[i][0]; 42 u16 value = regs[i][1]; 43 44 ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG, 45 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 46 value, index, NULL, 0, 0); 47 if (ret < 0) 48 return ret; 49 } 50 51 return 0; 52 } 53 54 static int usbtv_probe(struct usb_interface *intf, 55 const struct usb_device_id *id) 56 { 57 int ret; 58 int size; 59 struct device *dev = &intf->dev; 60 struct usbtv *usbtv; 61 62 /* Checks that the device is what we think it is. */ 63 if (intf->num_altsetting != 2) 64 return -ENODEV; 65 if (intf->altsetting[1].desc.bNumEndpoints != 4) 66 return -ENODEV; 67 68 /* Packet size is split into 11 bits of base size and count of 69 * extra multiplies of it.*/ 70 size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc); 71 size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1); 72 73 /* Device structure */ 74 usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL); 75 if (usbtv == NULL) 76 return -ENOMEM; 77 usbtv->dev = dev; 78 usbtv->udev = usb_get_dev(interface_to_usbdev(intf)); 79 80 usbtv->iso_size = size; 81 82 usb_set_intfdata(intf, usbtv); 83 84 ret = usbtv_video_init(usbtv); 85 if (ret < 0) 86 goto usbtv_video_fail; 87 88 /* for simplicity we exploit the v4l2_device reference counting */ 89 v4l2_device_get(&usbtv->v4l2_dev); 90 91 dev_info(dev, "Fushicai USBTV007 Video Grabber\n"); 92 return 0; 93 94 usbtv_video_fail: 95 kfree(usbtv); 96 97 return ret; 98 } 99 100 static void usbtv_disconnect(struct usb_interface *intf) 101 { 102 struct usbtv *usbtv = usb_get_intfdata(intf); 103 usb_set_intfdata(intf, NULL); 104 105 if (!usbtv) 106 return; 107 108 usbtv_video_free(usbtv); 109 110 usb_put_dev(usbtv->udev); 111 usbtv->udev = NULL; 112 113 /* the usbtv structure will be deallocated when v4l2 will be 114 done using it */ 115 v4l2_device_put(&usbtv->v4l2_dev); 116 } 117 118 static struct usb_device_id usbtv_id_table[] = { 119 { USB_DEVICE(0x1b71, 0x3002) }, 120 {} 121 }; 122 MODULE_DEVICE_TABLE(usb, usbtv_id_table); 123 124 MODULE_AUTHOR("Lubomir Rintel"); 125 MODULE_DESCRIPTION("Fushicai USBTV007 Video Grabber Driver"); 126 MODULE_LICENSE("Dual BSD/GPL"); 127 128 static struct usb_driver usbtv_usb_driver = { 129 .name = "usbtv", 130 .id_table = usbtv_id_table, 131 .probe = usbtv_probe, 132 .disconnect = usbtv_disconnect, 133 }; 134 135 module_usb_driver(usbtv_usb_driver); 136 137 138 139 140 141 /* LDV_COMMENT_BEGIN_MAIN */ 142 #ifdef LDV_MAIN0_sequence_infinite_withcheck_stateful 143 144 /*###########################################################################*/ 145 146 /*############## Driver Environment Generator 0.2 output ####################*/ 147 148 /*###########################################################################*/ 149 150 151 152 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test if all kernel resources are correctly released by driver before driver will be unloaded. */ 153 void ldv_check_final_state(void); 154 155 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test correct return result. */ 156 void ldv_check_return_value(int res); 157 158 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test correct return result of probe() function. */ 159 void ldv_check_return_value_probe(int res); 160 161 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Initializes the model. */ 162 void ldv_initialize(void); 163 164 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Reinitializes the model between distinct model function calls. */ 165 void ldv_handler_precall(void); 166 167 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Returns arbitrary interger value. */ 168 int nondet_int(void); 169 170 /* LDV_COMMENT_VAR_DECLARE_LDV Special variable for LDV verifier. */ 171 int LDV_IN_INTERRUPT; 172 173 /* LDV_COMMENT_FUNCTION_MAIN Main function for LDV verifier. */ 174 void ldv_main0_sequence_infinite_withcheck_stateful(void) { 175 176 177 178 /* LDV_COMMENT_BEGIN_VARIABLE_DECLARATION_PART */ 179 /*============================= VARIABLE DECLARATION PART =============================*/ 180 /** STRUCT: struct type: usb_driver, struct name: usbtv_usb_driver **/ 181 /* content: static int usbtv_probe(struct usb_interface *intf, const struct usb_device_id *id)*/ 182 /* LDV_COMMENT_END_PREP */ 183 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_probe" */ 184 struct usb_interface * var_group1; 185 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_probe" */ 186 const struct usb_device_id * var_usbtv_probe_0_p1; 187 /* LDV_COMMENT_VAR_DECLARE Variable declaration for test return result from function call "usbtv_probe" */ 188 static int res_usbtv_probe_0; 189 /* content: static void usbtv_disconnect(struct usb_interface *intf)*/ 190 /* LDV_COMMENT_END_PREP */ 191 192 193 194 195 /* LDV_COMMENT_END_VARIABLE_DECLARATION_PART */ 196 /* LDV_COMMENT_BEGIN_VARIABLE_INITIALIZING_PART */ 197 /*============================= VARIABLE INITIALIZING PART =============================*/ 198 LDV_IN_INTERRUPT=1; 199 200 201 202 203 /* LDV_COMMENT_END_VARIABLE_INITIALIZING_PART */ 204 /* LDV_COMMENT_BEGIN_FUNCTION_CALL_SECTION */ 205 /*============================= FUNCTION CALL SECTION =============================*/ 206 /* LDV_COMMENT_FUNCTION_CALL Initialize LDV model. */ 207 ldv_initialize(); 208 int ldv_s_usbtv_usb_driver_usb_driver = 0; 209 210 211 while( nondet_int() 212 || !(ldv_s_usbtv_usb_driver_usb_driver == 0) 213 ) { 214 215 switch(nondet_int()) { 216 217 case 0: { 218 219 /** STRUCT: struct type: usb_driver, struct name: usbtv_usb_driver **/ 220 if(ldv_s_usbtv_usb_driver_usb_driver==0) { 221 222 /* content: static int usbtv_probe(struct usb_interface *intf, const struct usb_device_id *id)*/ 223 /* LDV_COMMENT_END_PREP */ 224 /* LDV_COMMENT_FUNCTION_CALL Function from field "probe" from driver structure with callbacks "usbtv_usb_driver". Standart function test for correct return result. */ 225 res_usbtv_probe_0 = usbtv_probe( var_group1, var_usbtv_probe_0_p1); 226 ldv_check_return_value(res_usbtv_probe_0); 227 ldv_check_return_value_probe(res_usbtv_probe_0); 228 if(res_usbtv_probe_0) 229 goto ldv_module_exit; 230 ldv_s_usbtv_usb_driver_usb_driver++; 231 232 } 233 234 } 235 236 break; 237 case 1: { 238 239 /** STRUCT: struct type: usb_driver, struct name: usbtv_usb_driver **/ 240 if(ldv_s_usbtv_usb_driver_usb_driver==1) { 241 242 /* content: static void usbtv_disconnect(struct usb_interface *intf)*/ 243 /* LDV_COMMENT_END_PREP */ 244 /* LDV_COMMENT_FUNCTION_CALL Function from field "disconnect" from driver structure with callbacks "usbtv_usb_driver" */ 245 ldv_handler_precall(); 246 usbtv_disconnect( var_group1); 247 ldv_s_usbtv_usb_driver_usb_driver=0; 248 249 } 250 251 } 252 253 break; 254 default: break; 255 256 } 257 258 } 259 260 ldv_module_exit: 261 262 /* LDV_COMMENT_FUNCTION_CALL Checks that all resources and locks are correctly released before the driver will be unloaded. */ 263 ldv_final: ldv_check_final_state(); 264 265 /* LDV_COMMENT_END_FUNCTION_CALL_SECTION */ 266 return; 267 268 } 269 #endif 270 271 /* LDV_COMMENT_END_MAIN */
1 2 #include <linux/kernel.h> 3 bool ldv_is_err(const void *ptr); 4 bool ldv_is_err_or_null(const void *ptr); 5 void* ldv_err_ptr(long error); 6 long ldv_ptr_err(const void *ptr); 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 11 #include <linux/usb.h> 12 13 // Provide model function prototypes before their usage. 14 void ldv_interface_to_usbdev(void); 15 void ldv_usb_get_dev(void); 16 void ldv_usb_put_dev(void); 17 #line 1 "/home/ldvuser/ref_launch/work/current--X--drivers--X--defaultlinux-3.16-rc1.tar.xz--X--132_1a/linux-3.16-rc1.tar.xz/csd_deg_dscv/8832/dscv_tempdir/dscv/ri/132_1a/drivers/media/usb/usbtv/usbtv-core.c" 18 19 /* 20 * Fushicai USBTV007 Video Grabber Driver 21 * 22 * Product web site: 23 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html 24 * 25 * Following LWN articles were very useful in construction of this driver: 26 * Video4Linux2 API series: http://lwn.net/Articles/203924/ 27 * videobuf2 API explanation: http://lwn.net/Articles/447435/ 28 * Thanks go to Jonathan Corbet for providing this quality documentation. 29 * He is awesome. 30 * 31 * Copyright (c) 2013 Lubomir Rintel 32 * All rights reserved. 33 * No physical hardware was harmed running Windows during the 34 * reverse-engineering activity 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions, and the following disclaimer, 41 * without modification. 42 * 2. The name of the author may not be used to endorse or promote products 43 * derived from this software without specific prior written permission. 44 * 45 * Alternatively, this software may be distributed under the terms of the 46 * GNU General Public License ("GPL"). 47 */ 48 49 #include "usbtv.h" 50 51 int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size) 52 { 53 int ret; 54 int pipe = usb_rcvctrlpipe(usbtv->udev, 0); 55 int i; 56 57 for (i = 0; i < size; i++) { 58 u16 index = regs[i][0]; 59 u16 value = regs[i][1]; 60 61 ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG, 62 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 63 value, index, NULL, 0, 0); 64 if (ret < 0) 65 return ret; 66 } 67 68 return 0; 69 } 70 71 static int usbtv_probe(struct usb_interface *intf, 72 const struct usb_device_id *id) 73 { 74 int ret; 75 int size; 76 struct device *dev = &intf->dev; 77 struct usbtv *usbtv; 78 79 /* Checks that the device is what we think it is. */ 80 if (intf->num_altsetting != 2) 81 return -ENODEV; 82 if (intf->altsetting[1].desc.bNumEndpoints != 4) 83 return -ENODEV; 84 85 /* Packet size is split into 11 bits of base size and count of 86 * extra multiplies of it.*/ 87 size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc); 88 size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1); 89 90 /* Device structure */ 91 usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL); 92 if (usbtv == NULL) 93 return -ENOMEM; 94 usbtv->dev = dev; 95 usbtv->udev = usb_get_dev(interface_to_usbdev(intf)); 96 97 usbtv->iso_size = size; 98 99 usb_set_intfdata(intf, usbtv); 100 101 ret = usbtv_video_init(usbtv); 102 if (ret < 0) 103 goto usbtv_video_fail; 104 105 /* for simplicity we exploit the v4l2_device reference counting */ 106 v4l2_device_get(&usbtv->v4l2_dev); 107 108 dev_info(dev, "Fushicai USBTV007 Video Grabber\n"); 109 return 0; 110 111 usbtv_video_fail: 112 kfree(usbtv); 113 114 return ret; 115 } 116 117 static void usbtv_disconnect(struct usb_interface *intf) 118 { 119 struct usbtv *usbtv = usb_get_intfdata(intf); 120 usb_set_intfdata(intf, NULL); 121 122 if (!usbtv) 123 return; 124 125 usbtv_video_free(usbtv); 126 127 usb_put_dev(usbtv->udev); 128 usbtv->udev = NULL; 129 130 /* the usbtv structure will be deallocated when v4l2 will be 131 done using it */ 132 v4l2_device_put(&usbtv->v4l2_dev); 133 } 134 135 static struct usb_device_id usbtv_id_table[] = { 136 { USB_DEVICE(0x1b71, 0x3002) }, 137 {} 138 }; 139 MODULE_DEVICE_TABLE(usb, usbtv_id_table); 140 141 MODULE_AUTHOR("Lubomir Rintel"); 142 MODULE_DESCRIPTION("Fushicai USBTV007 Video Grabber Driver"); 143 MODULE_LICENSE("Dual BSD/GPL"); 144 145 static struct usb_driver usbtv_usb_driver = { 146 .name = "usbtv", 147 .id_table = usbtv_id_table, 148 .probe = usbtv_probe, 149 .disconnect = usbtv_disconnect, 150 }; 151 152 module_usb_driver(usbtv_usb_driver); 153 154 155 156 157 158 /* LDV_COMMENT_BEGIN_MAIN */ 159 #ifdef LDV_MAIN0_sequence_infinite_withcheck_stateful 160 161 /*###########################################################################*/ 162 163 /*############## Driver Environment Generator 0.2 output ####################*/ 164 165 /*###########################################################################*/ 166 167 168 169 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test if all kernel resources are correctly released by driver before driver will be unloaded. */ 170 void ldv_check_final_state(void); 171 172 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test correct return result. */ 173 void ldv_check_return_value(int res); 174 175 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test correct return result of probe() function. */ 176 void ldv_check_return_value_probe(int res); 177 178 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Initializes the model. */ 179 void ldv_initialize(void); 180 181 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Reinitializes the model between distinct model function calls. */ 182 void ldv_handler_precall(void); 183 184 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Returns arbitrary interger value. */ 185 int nondet_int(void); 186 187 /* LDV_COMMENT_VAR_DECLARE_LDV Special variable for LDV verifier. */ 188 int LDV_IN_INTERRUPT; 189 190 /* LDV_COMMENT_FUNCTION_MAIN Main function for LDV verifier. */ 191 void ldv_main0_sequence_infinite_withcheck_stateful(void) { 192 193 194 195 /* LDV_COMMENT_BEGIN_VARIABLE_DECLARATION_PART */ 196 /*============================= VARIABLE DECLARATION PART =============================*/ 197 /** STRUCT: struct type: usb_driver, struct name: usbtv_usb_driver **/ 198 /* content: static int usbtv_probe(struct usb_interface *intf, const struct usb_device_id *id)*/ 199 /* LDV_COMMENT_END_PREP */ 200 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_probe" */ 201 struct usb_interface * var_group1; 202 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_probe" */ 203 const struct usb_device_id * var_usbtv_probe_0_p1; 204 /* LDV_COMMENT_VAR_DECLARE Variable declaration for test return result from function call "usbtv_probe" */ 205 static int res_usbtv_probe_0; 206 /* content: static void usbtv_disconnect(struct usb_interface *intf)*/ 207 /* LDV_COMMENT_END_PREP */ 208 209 210 211 212 /* LDV_COMMENT_END_VARIABLE_DECLARATION_PART */ 213 /* LDV_COMMENT_BEGIN_VARIABLE_INITIALIZING_PART */ 214 /*============================= VARIABLE INITIALIZING PART =============================*/ 215 LDV_IN_INTERRUPT=1; 216 217 218 219 220 /* LDV_COMMENT_END_VARIABLE_INITIALIZING_PART */ 221 /* LDV_COMMENT_BEGIN_FUNCTION_CALL_SECTION */ 222 /*============================= FUNCTION CALL SECTION =============================*/ 223 /* LDV_COMMENT_FUNCTION_CALL Initialize LDV model. */ 224 ldv_initialize(); 225 int ldv_s_usbtv_usb_driver_usb_driver = 0; 226 227 228 while( nondet_int() 229 || !(ldv_s_usbtv_usb_driver_usb_driver == 0) 230 ) { 231 232 switch(nondet_int()) { 233 234 case 0: { 235 236 /** STRUCT: struct type: usb_driver, struct name: usbtv_usb_driver **/ 237 if(ldv_s_usbtv_usb_driver_usb_driver==0) { 238 239 /* content: static int usbtv_probe(struct usb_interface *intf, const struct usb_device_id *id)*/ 240 /* LDV_COMMENT_END_PREP */ 241 /* LDV_COMMENT_FUNCTION_CALL Function from field "probe" from driver structure with callbacks "usbtv_usb_driver". Standart function test for correct return result. */ 242 res_usbtv_probe_0 = usbtv_probe( var_group1, var_usbtv_probe_0_p1); 243 ldv_check_return_value(res_usbtv_probe_0); 244 ldv_check_return_value_probe(res_usbtv_probe_0); 245 if(res_usbtv_probe_0) 246 goto ldv_module_exit; 247 ldv_s_usbtv_usb_driver_usb_driver++; 248 249 } 250 251 } 252 253 break; 254 case 1: { 255 256 /** STRUCT: struct type: usb_driver, struct name: usbtv_usb_driver **/ 257 if(ldv_s_usbtv_usb_driver_usb_driver==1) { 258 259 /* content: static void usbtv_disconnect(struct usb_interface *intf)*/ 260 /* LDV_COMMENT_END_PREP */ 261 /* LDV_COMMENT_FUNCTION_CALL Function from field "disconnect" from driver structure with callbacks "usbtv_usb_driver" */ 262 ldv_handler_precall(); 263 usbtv_disconnect( var_group1); 264 ldv_s_usbtv_usb_driver_usb_driver=0; 265 266 } 267 268 } 269 270 break; 271 default: break; 272 273 } 274 275 } 276 277 ldv_module_exit: 278 279 /* LDV_COMMENT_FUNCTION_CALL Checks that all resources and locks are correctly released before the driver will be unloaded. */ 280 ldv_final: ldv_check_final_state(); 281 282 /* LDV_COMMENT_END_FUNCTION_CALL_SECTION */ 283 return; 284 285 } 286 #endif 287 288 /* LDV_COMMENT_END_MAIN */ 289 290 #line 17 "/home/ldvuser/ref_launch/work/current--X--drivers--X--defaultlinux-3.16-rc1.tar.xz--X--132_1a/linux-3.16-rc1.tar.xz/csd_deg_dscv/8832/dscv_tempdir/dscv/ri/132_1a/drivers/media/usb/usbtv/usbtv-core.o.c.prepared"
1 2 /* 3 * Fushicai USBTV007 Video Grabber Driver 4 * 5 * Product web site: 6 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html 7 * 8 * Following LWN articles were very useful in construction of this driver: 9 * Video4Linux2 API series: http://lwn.net/Articles/203924/ 10 * videobuf2 API explanation: http://lwn.net/Articles/447435/ 11 * Thanks go to Jonathan Corbet for providing this quality documentation. 12 * He is awesome. 13 * 14 * Copyright (c) 2013 Lubomir Rintel 15 * All rights reserved. 16 * No physical hardware was harmed running Windows during the 17 * reverse-engineering activity 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions, and the following disclaimer, 24 * without modification. 25 * 2. The name of the author may not be used to endorse or promote products 26 * derived from this software without specific prior written permission. 27 * 28 * Alternatively, this software may be distributed under the terms of the 29 * GNU General Public License ("GPL"). 30 */ 31 32 #include <media/v4l2-ioctl.h> 33 #include <media/videobuf2-core.h> 34 35 #include "usbtv.h" 36 37 static struct usbtv_norm_params norm_params[] = { 38 { 39 .norm = V4L2_STD_525_60, 40 .cap_width = 720, 41 .cap_height = 480, 42 }, 43 { 44 .norm = V4L2_STD_PAL, 45 .cap_width = 720, 46 .cap_height = 576, 47 } 48 }; 49 50 static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm) 51 { 52 int i, ret = 0; 53 struct usbtv_norm_params *params = NULL; 54 55 for (i = 0; i < ARRAY_SIZE(norm_params); i++) { 56 if (norm_params[i].norm & norm) { 57 params = &norm_params[i]; 58 break; 59 } 60 } 61 62 if (params) { 63 usbtv->width = params->cap_width; 64 usbtv->height = params->cap_height; 65 usbtv->n_chunks = usbtv->width * usbtv->height 66 / 4 / USBTV_CHUNK; 67 usbtv->norm = params->norm; 68 } else 69 ret = -EINVAL; 70 71 return ret; 72 } 73 74 static int usbtv_select_input(struct usbtv *usbtv, int input) 75 { 76 int ret; 77 78 static const u16 composite[][2] = { 79 { USBTV_BASE + 0x0105, 0x0060 }, 80 { USBTV_BASE + 0x011f, 0x00f2 }, 81 { USBTV_BASE + 0x0127, 0x0060 }, 82 { USBTV_BASE + 0x00ae, 0x0010 }, 83 { USBTV_BASE + 0x0284, 0x00aa }, 84 { USBTV_BASE + 0x0239, 0x0060 }, 85 }; 86 87 static const u16 svideo[][2] = { 88 { USBTV_BASE + 0x0105, 0x0010 }, 89 { USBTV_BASE + 0x011f, 0x00ff }, 90 { USBTV_BASE + 0x0127, 0x0060 }, 91 { USBTV_BASE + 0x00ae, 0x0030 }, 92 { USBTV_BASE + 0x0284, 0x0088 }, 93 { USBTV_BASE + 0x0239, 0x0060 }, 94 }; 95 96 switch (input) { 97 case USBTV_COMPOSITE_INPUT: 98 ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite)); 99 break; 100 case USBTV_SVIDEO_INPUT: 101 ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo)); 102 break; 103 default: 104 ret = -EINVAL; 105 } 106 107 if (!ret) 108 usbtv->input = input; 109 110 return ret; 111 } 112 113 static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm) 114 { 115 int ret; 116 static const u16 pal[][2] = { 117 { USBTV_BASE + 0x001a, 0x0068 }, 118 { USBTV_BASE + 0x010e, 0x0072 }, 119 { USBTV_BASE + 0x010f, 0x00a2 }, 120 { USBTV_BASE + 0x0112, 0x00b0 }, 121 { USBTV_BASE + 0x0117, 0x0001 }, 122 { USBTV_BASE + 0x0118, 0x002c }, 123 { USBTV_BASE + 0x012d, 0x0010 }, 124 { USBTV_BASE + 0x012f, 0x0020 }, 125 { USBTV_BASE + 0x024f, 0x0002 }, 126 { USBTV_BASE + 0x0254, 0x0059 }, 127 { USBTV_BASE + 0x025a, 0x0016 }, 128 { USBTV_BASE + 0x025b, 0x0035 }, 129 { USBTV_BASE + 0x0263, 0x0017 }, 130 { USBTV_BASE + 0x0266, 0x0016 }, 131 { USBTV_BASE + 0x0267, 0x0036 } 132 }; 133 134 static const u16 ntsc[][2] = { 135 { USBTV_BASE + 0x001a, 0x0079 }, 136 { USBTV_BASE + 0x010e, 0x0068 }, 137 { USBTV_BASE + 0x010f, 0x009c }, 138 { USBTV_BASE + 0x0112, 0x00f0 }, 139 { USBTV_BASE + 0x0117, 0x0000 }, 140 { USBTV_BASE + 0x0118, 0x00fc }, 141 { USBTV_BASE + 0x012d, 0x0004 }, 142 { USBTV_BASE + 0x012f, 0x0008 }, 143 { USBTV_BASE + 0x024f, 0x0001 }, 144 { USBTV_BASE + 0x0254, 0x005f }, 145 { USBTV_BASE + 0x025a, 0x0012 }, 146 { USBTV_BASE + 0x025b, 0x0001 }, 147 { USBTV_BASE + 0x0263, 0x001c }, 148 { USBTV_BASE + 0x0266, 0x0011 }, 149 { USBTV_BASE + 0x0267, 0x0005 } 150 }; 151 152 ret = usbtv_configure_for_norm(usbtv, norm); 153 154 if (!ret) { 155 if (norm & V4L2_STD_525_60) 156 ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc)); 157 else if (norm & V4L2_STD_PAL) 158 ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal)); 159 } 160 161 return ret; 162 } 163 164 static int usbtv_setup_capture(struct usbtv *usbtv) 165 { 166 int ret; 167 static const u16 setup[][2] = { 168 /* These seem to enable the device. */ 169 { USBTV_BASE + 0x0008, 0x0001 }, 170 { USBTV_BASE + 0x01d0, 0x00ff }, 171 { USBTV_BASE + 0x01d9, 0x0002 }, 172 173 /* These seem to influence color parameters, such as 174 * brightness, etc. */ 175 { USBTV_BASE + 0x0239, 0x0040 }, 176 { USBTV_BASE + 0x0240, 0x0000 }, 177 { USBTV_BASE + 0x0241, 0x0000 }, 178 { USBTV_BASE + 0x0242, 0x0002 }, 179 { USBTV_BASE + 0x0243, 0x0080 }, 180 { USBTV_BASE + 0x0244, 0x0012 }, 181 { USBTV_BASE + 0x0245, 0x0090 }, 182 { USBTV_BASE + 0x0246, 0x0000 }, 183 184 { USBTV_BASE + 0x0278, 0x002d }, 185 { USBTV_BASE + 0x0279, 0x000a }, 186 { USBTV_BASE + 0x027a, 0x0032 }, 187 { 0xf890, 0x000c }, 188 { 0xf894, 0x0086 }, 189 190 { USBTV_BASE + 0x00ac, 0x00c0 }, 191 { USBTV_BASE + 0x00ad, 0x0000 }, 192 { USBTV_BASE + 0x00a2, 0x0012 }, 193 { USBTV_BASE + 0x00a3, 0x00e0 }, 194 { USBTV_BASE + 0x00a4, 0x0028 }, 195 { USBTV_BASE + 0x00a5, 0x0082 }, 196 { USBTV_BASE + 0x00a7, 0x0080 }, 197 { USBTV_BASE + 0x0000, 0x0014 }, 198 { USBTV_BASE + 0x0006, 0x0003 }, 199 { USBTV_BASE + 0x0090, 0x0099 }, 200 { USBTV_BASE + 0x0091, 0x0090 }, 201 { USBTV_BASE + 0x0094, 0x0068 }, 202 { USBTV_BASE + 0x0095, 0x0070 }, 203 { USBTV_BASE + 0x009c, 0x0030 }, 204 { USBTV_BASE + 0x009d, 0x00c0 }, 205 { USBTV_BASE + 0x009e, 0x00e0 }, 206 { USBTV_BASE + 0x0019, 0x0006 }, 207 { USBTV_BASE + 0x008c, 0x00ba }, 208 { USBTV_BASE + 0x0101, 0x00ff }, 209 { USBTV_BASE + 0x010c, 0x00b3 }, 210 { USBTV_BASE + 0x01b2, 0x0080 }, 211 { USBTV_BASE + 0x01b4, 0x00a0 }, 212 { USBTV_BASE + 0x014c, 0x00ff }, 213 { USBTV_BASE + 0x014d, 0x00ca }, 214 { USBTV_BASE + 0x0113, 0x0053 }, 215 { USBTV_BASE + 0x0119, 0x008a }, 216 { USBTV_BASE + 0x013c, 0x0003 }, 217 { USBTV_BASE + 0x0150, 0x009c }, 218 { USBTV_BASE + 0x0151, 0x0071 }, 219 { USBTV_BASE + 0x0152, 0x00c6 }, 220 { USBTV_BASE + 0x0153, 0x0084 }, 221 { USBTV_BASE + 0x0154, 0x00bc }, 222 { USBTV_BASE + 0x0155, 0x00a0 }, 223 { USBTV_BASE + 0x0156, 0x00a0 }, 224 { USBTV_BASE + 0x0157, 0x009c }, 225 { USBTV_BASE + 0x0158, 0x001f }, 226 { USBTV_BASE + 0x0159, 0x0006 }, 227 { USBTV_BASE + 0x015d, 0x0000 }, 228 229 { USBTV_BASE + 0x0284, 0x0088 }, 230 { USBTV_BASE + 0x0003, 0x0004 }, 231 { USBTV_BASE + 0x0100, 0x00d3 }, 232 { USBTV_BASE + 0x0115, 0x0015 }, 233 { USBTV_BASE + 0x0220, 0x002e }, 234 { USBTV_BASE + 0x0225, 0x0008 }, 235 { USBTV_BASE + 0x024e, 0x0002 }, 236 { USBTV_BASE + 0x024e, 0x0002 }, 237 { USBTV_BASE + 0x024f, 0x0002 }, 238 }; 239 240 ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup)); 241 if (ret) 242 return ret; 243 244 ret = usbtv_select_norm(usbtv, usbtv->norm); 245 if (ret) 246 return ret; 247 248 ret = usbtv_select_input(usbtv, usbtv->input); 249 if (ret) 250 return ret; 251 252 return 0; 253 } 254 255 /* Copy data from chunk into a frame buffer, deinterlacing the data 256 * into every second line. Unfortunately, they don't align nicely into 257 * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels. 258 * Therefore, we break down the chunk into two halves before copyting, 259 * so that we can interleave a line if needed. */ 260 static void usbtv_chunk_to_vbuf(u32 *frame, u32 *src, int chunk_no, int odd) 261 { 262 int half; 263 264 for (half = 0; half < 2; half++) { 265 int part_no = chunk_no * 2 + half; 266 int line = part_no / 3; 267 int part_index = (line * 2 + !odd) * 3 + (part_no % 3); 268 269 u32 *dst = &frame[part_index * USBTV_CHUNK/2]; 270 memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src)); 271 src += USBTV_CHUNK/2; 272 } 273 } 274 275 /* Called for each 256-byte image chunk. 276 * First word identifies the chunk, followed by 240 words of image 277 * data and padding. */ 278 static void usbtv_image_chunk(struct usbtv *usbtv, u32 *chunk) 279 { 280 int frame_id, odd, chunk_no; 281 u32 *frame; 282 struct usbtv_buf *buf; 283 unsigned long flags; 284 285 /* Ignore corrupted lines. */ 286 if (!USBTV_MAGIC_OK(chunk)) 287 return; 288 frame_id = USBTV_FRAME_ID(chunk); 289 odd = USBTV_ODD(chunk); 290 chunk_no = USBTV_CHUNK_NO(chunk); 291 if (chunk_no >= usbtv->n_chunks) 292 return; 293 294 /* Beginning of a frame. */ 295 if (chunk_no == 0) { 296 usbtv->frame_id = frame_id; 297 usbtv->chunks_done = 0; 298 } 299 300 if (usbtv->frame_id != frame_id) 301 return; 302 303 spin_lock_irqsave(&usbtv->buflock, flags); 304 if (list_empty(&usbtv->bufs)) { 305 /* No free buffers. Userspace likely too slow. */ 306 spin_unlock_irqrestore(&usbtv->buflock, flags); 307 return; 308 } 309 310 /* First available buffer. */ 311 buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list); 312 frame = vb2_plane_vaddr(&buf->vb, 0); 313 314 /* Copy the chunk data. */ 315 usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd); 316 usbtv->chunks_done++; 317 318 /* Last chunk in a frame, signalling an end */ 319 if (odd && chunk_no == usbtv->n_chunks-1) { 320 int size = vb2_plane_size(&buf->vb, 0); 321 enum vb2_buffer_state state = usbtv->chunks_done == 322 usbtv->n_chunks ? 323 VB2_BUF_STATE_DONE : 324 VB2_BUF_STATE_ERROR; 325 326 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED; 327 buf->vb.v4l2_buf.sequence = usbtv->sequence++; 328 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp); 329 vb2_set_plane_payload(&buf->vb, 0, size); 330 vb2_buffer_done(&buf->vb, state); 331 list_del(&buf->list); 332 } 333 334 spin_unlock_irqrestore(&usbtv->buflock, flags); 335 } 336 337 /* Got image data. Each packet contains a number of 256-word chunks we 338 * compose the image from. */ 339 static void usbtv_iso_cb(struct urb *ip) 340 { 341 int ret; 342 int i; 343 struct usbtv *usbtv = (struct usbtv *)ip->context; 344 345 switch (ip->status) { 346 /* All fine. */ 347 case 0: 348 break; 349 /* Device disconnected or capture stopped? */ 350 case -ENODEV: 351 case -ENOENT: 352 case -ECONNRESET: 353 case -ESHUTDOWN: 354 return; 355 /* Unknown error. Retry. */ 356 default: 357 dev_warn(usbtv->dev, "Bad response for ISO request.\n"); 358 goto resubmit; 359 } 360 361 for (i = 0; i < ip->number_of_packets; i++) { 362 int size = ip->iso_frame_desc[i].actual_length; 363 unsigned char *data = ip->transfer_buffer + 364 ip->iso_frame_desc[i].offset; 365 int offset; 366 367 for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++) 368 usbtv_image_chunk(usbtv, 369 (u32 *)&data[USBTV_CHUNK_SIZE * offset]); 370 } 371 372 resubmit: 373 ret = usb_submit_urb(ip, GFP_ATOMIC); 374 if (ret < 0) 375 dev_warn(usbtv->dev, "Could not resubmit ISO URB\n"); 376 } 377 378 static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv) 379 { 380 struct urb *ip; 381 int size = usbtv->iso_size; 382 int i; 383 384 ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL); 385 if (ip == NULL) 386 return NULL; 387 388 ip->dev = usbtv->udev; 389 ip->context = usbtv; 390 ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP); 391 ip->interval = 1; 392 ip->transfer_flags = URB_ISO_ASAP; 393 ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS, 394 GFP_KERNEL); 395 ip->complete = usbtv_iso_cb; 396 ip->number_of_packets = USBTV_ISOC_PACKETS; 397 ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS; 398 for (i = 0; i < USBTV_ISOC_PACKETS; i++) { 399 ip->iso_frame_desc[i].offset = size * i; 400 ip->iso_frame_desc[i].length = size; 401 } 402 403 return ip; 404 } 405 406 static void usbtv_stop(struct usbtv *usbtv) 407 { 408 int i; 409 unsigned long flags; 410 411 /* Cancel running transfers. */ 412 for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { 413 struct urb *ip = usbtv->isoc_urbs[i]; 414 if (ip == NULL) 415 continue; 416 usb_kill_urb(ip); 417 kfree(ip->transfer_buffer); 418 usb_free_urb(ip); 419 usbtv->isoc_urbs[i] = NULL; 420 } 421 422 /* Return buffers to userspace. */ 423 spin_lock_irqsave(&usbtv->buflock, flags); 424 while (!list_empty(&usbtv->bufs)) { 425 struct usbtv_buf *buf = list_first_entry(&usbtv->bufs, 426 struct usbtv_buf, list); 427 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); 428 list_del(&buf->list); 429 } 430 spin_unlock_irqrestore(&usbtv->buflock, flags); 431 } 432 433 static int usbtv_start(struct usbtv *usbtv) 434 { 435 int i; 436 int ret; 437 438 ret = usb_set_interface(usbtv->udev, 0, 0); 439 if (ret < 0) 440 return ret; 441 442 ret = usbtv_setup_capture(usbtv); 443 if (ret < 0) 444 return ret; 445 446 ret = usb_set_interface(usbtv->udev, 0, 1); 447 if (ret < 0) 448 return ret; 449 450 for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { 451 struct urb *ip; 452 453 ip = usbtv_setup_iso_transfer(usbtv); 454 if (ip == NULL) { 455 ret = -ENOMEM; 456 goto start_fail; 457 } 458 usbtv->isoc_urbs[i] = ip; 459 460 ret = usb_submit_urb(ip, GFP_KERNEL); 461 if (ret < 0) 462 goto start_fail; 463 } 464 465 return 0; 466 467 start_fail: 468 usbtv_stop(usbtv); 469 return ret; 470 } 471 472 static int usbtv_querycap(struct file *file, void *priv, 473 struct v4l2_capability *cap) 474 { 475 struct usbtv *dev = video_drvdata(file); 476 477 strlcpy(cap->driver, "usbtv", sizeof(cap->driver)); 478 strlcpy(cap->card, "usbtv", sizeof(cap->card)); 479 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 480 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE; 481 cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; 482 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 483 return 0; 484 } 485 486 static int usbtv_enum_input(struct file *file, void *priv, 487 struct v4l2_input *i) 488 { 489 struct usbtv *dev = video_drvdata(file); 490 491 switch (i->index) { 492 case USBTV_COMPOSITE_INPUT: 493 strlcpy(i->name, "Composite", sizeof(i->name)); 494 break; 495 case USBTV_SVIDEO_INPUT: 496 strlcpy(i->name, "S-Video", sizeof(i->name)); 497 break; 498 default: 499 return -EINVAL; 500 } 501 502 i->type = V4L2_INPUT_TYPE_CAMERA; 503 i->std = dev->vdev.tvnorms; 504 return 0; 505 } 506 507 static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv, 508 struct v4l2_fmtdesc *f) 509 { 510 if (f->index > 0) 511 return -EINVAL; 512 513 strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed", 514 sizeof(f->description)); 515 f->pixelformat = V4L2_PIX_FMT_YUYV; 516 return 0; 517 } 518 519 static int usbtv_fmt_vid_cap(struct file *file, void *priv, 520 struct v4l2_format *f) 521 { 522 struct usbtv *usbtv = video_drvdata(file); 523 524 f->fmt.pix.width = usbtv->width; 525 f->fmt.pix.height = usbtv->height; 526 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; 527 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 528 f->fmt.pix.bytesperline = usbtv->width * 2; 529 f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height); 530 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 531 532 return 0; 533 } 534 535 static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm) 536 { 537 struct usbtv *usbtv = video_drvdata(file); 538 *norm = usbtv->norm; 539 return 0; 540 } 541 542 static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm) 543 { 544 int ret = -EINVAL; 545 struct usbtv *usbtv = video_drvdata(file); 546 547 if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL)) 548 ret = usbtv_select_norm(usbtv, norm); 549 550 return ret; 551 } 552 553 static int usbtv_g_input(struct file *file, void *priv, unsigned int *i) 554 { 555 struct usbtv *usbtv = video_drvdata(file); 556 *i = usbtv->input; 557 return 0; 558 } 559 560 static int usbtv_s_input(struct file *file, void *priv, unsigned int i) 561 { 562 struct usbtv *usbtv = video_drvdata(file); 563 return usbtv_select_input(usbtv, i); 564 } 565 566 static struct v4l2_ioctl_ops usbtv_ioctl_ops = { 567 .vidioc_querycap = usbtv_querycap, 568 .vidioc_enum_input = usbtv_enum_input, 569 .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap, 570 .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap, 571 .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap, 572 .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap, 573 .vidioc_g_std = usbtv_g_std, 574 .vidioc_s_std = usbtv_s_std, 575 .vidioc_g_input = usbtv_g_input, 576 .vidioc_s_input = usbtv_s_input, 577 578 .vidioc_reqbufs = vb2_ioctl_reqbufs, 579 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 580 .vidioc_querybuf = vb2_ioctl_querybuf, 581 .vidioc_create_bufs = vb2_ioctl_create_bufs, 582 .vidioc_qbuf = vb2_ioctl_qbuf, 583 .vidioc_dqbuf = vb2_ioctl_dqbuf, 584 .vidioc_streamon = vb2_ioctl_streamon, 585 .vidioc_streamoff = vb2_ioctl_streamoff, 586 }; 587 588 static struct v4l2_file_operations usbtv_fops = { 589 .owner = THIS_MODULE, 590 .unlocked_ioctl = video_ioctl2, 591 .mmap = vb2_fop_mmap, 592 .open = v4l2_fh_open, 593 .release = vb2_fop_release, 594 .read = vb2_fop_read, 595 .poll = vb2_fop_poll, 596 }; 597 598 static int usbtv_queue_setup(struct vb2_queue *vq, 599 const struct v4l2_format *v4l_fmt, unsigned int *nbuffers, 600 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) 601 { 602 struct usbtv *usbtv = vb2_get_drv_priv(vq); 603 604 if (*nbuffers < 2) 605 *nbuffers = 2; 606 *nplanes = 1; 607 sizes[0] = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32); 608 609 return 0; 610 } 611 612 static void usbtv_buf_queue(struct vb2_buffer *vb) 613 { 614 struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue); 615 struct usbtv_buf *buf = container_of(vb, struct usbtv_buf, vb); 616 unsigned long flags; 617 618 if (usbtv->udev == NULL) { 619 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); 620 return; 621 } 622 623 spin_lock_irqsave(&usbtv->buflock, flags); 624 list_add_tail(&buf->list, &usbtv->bufs); 625 spin_unlock_irqrestore(&usbtv->buflock, flags); 626 } 627 628 static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count) 629 { 630 struct usbtv *usbtv = vb2_get_drv_priv(vq); 631 632 if (usbtv->udev == NULL) 633 return -ENODEV; 634 635 return usbtv_start(usbtv); 636 } 637 638 static void usbtv_stop_streaming(struct vb2_queue *vq) 639 { 640 struct usbtv *usbtv = vb2_get_drv_priv(vq); 641 642 if (usbtv->udev) 643 usbtv_stop(usbtv); 644 } 645 646 static struct vb2_ops usbtv_vb2_ops = { 647 .queue_setup = usbtv_queue_setup, 648 .buf_queue = usbtv_buf_queue, 649 .start_streaming = usbtv_start_streaming, 650 .stop_streaming = usbtv_stop_streaming, 651 }; 652 653 static void usbtv_release(struct v4l2_device *v4l2_dev) 654 { 655 struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev); 656 657 v4l2_device_unregister(&usbtv->v4l2_dev); 658 vb2_queue_release(&usbtv->vb2q); 659 kfree(usbtv); 660 } 661 662 int usbtv_video_init(struct usbtv *usbtv) 663 { 664 int ret; 665 666 (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60); 667 668 spin_lock_init(&usbtv->buflock); 669 mutex_init(&usbtv->v4l2_lock); 670 mutex_init(&usbtv->vb2q_lock); 671 INIT_LIST_HEAD(&usbtv->bufs); 672 673 /* videobuf2 structure */ 674 usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 675 usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; 676 usbtv->vb2q.drv_priv = usbtv; 677 usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf); 678 usbtv->vb2q.ops = &usbtv_vb2_ops; 679 usbtv->vb2q.mem_ops = &vb2_vmalloc_memops; 680 usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 681 usbtv->vb2q.lock = &usbtv->vb2q_lock; 682 ret = vb2_queue_init(&usbtv->vb2q); 683 if (ret < 0) { 684 dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n"); 685 return ret; 686 } 687 688 /* v4l2 structure */ 689 usbtv->v4l2_dev.release = usbtv_release; 690 ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev); 691 if (ret < 0) { 692 dev_warn(usbtv->dev, "Could not register v4l2 device\n"); 693 goto v4l2_fail; 694 } 695 696 /* Video structure */ 697 strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name)); 698 usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev; 699 usbtv->vdev.release = video_device_release_empty; 700 usbtv->vdev.fops = &usbtv_fops; 701 usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops; 702 usbtv->vdev.tvnorms = USBTV_TV_STD; 703 usbtv->vdev.queue = &usbtv->vb2q; 704 usbtv->vdev.lock = &usbtv->v4l2_lock; 705 set_bit(V4L2_FL_USE_FH_PRIO, &usbtv->vdev.flags); 706 video_set_drvdata(&usbtv->vdev, usbtv); 707 ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1); 708 if (ret < 0) { 709 dev_warn(usbtv->dev, "Could not register video device\n"); 710 goto vdev_fail; 711 } 712 713 return 0; 714 715 vdev_fail: 716 v4l2_device_unregister(&usbtv->v4l2_dev); 717 v4l2_fail: 718 vb2_queue_release(&usbtv->vb2q); 719 720 return ret; 721 } 722 723 void usbtv_video_free(struct usbtv *usbtv) 724 { 725 mutex_lock(&usbtv->vb2q_lock); 726 mutex_lock(&usbtv->v4l2_lock); 727 728 usbtv_stop(usbtv); 729 video_unregister_device(&usbtv->vdev); 730 v4l2_device_disconnect(&usbtv->v4l2_dev); 731 732 mutex_unlock(&usbtv->v4l2_lock); 733 mutex_unlock(&usbtv->vb2q_lock); 734 735 v4l2_device_put(&usbtv->v4l2_dev); 736 } 737 738 739 740 741 742 /* LDV_COMMENT_BEGIN_MAIN */ 743 #ifdef LDV_MAIN1_sequence_infinite_withcheck_stateful 744 745 /*###########################################################################*/ 746 747 /*############## Driver Environment Generator 0.2 output ####################*/ 748 749 /*###########################################################################*/ 750 751 752 753 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test if all kernel resources are correctly released by driver before driver will be unloaded. */ 754 void ldv_check_final_state(void); 755 756 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test correct return result. */ 757 void ldv_check_return_value(int res); 758 759 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Test correct return result of probe() function. */ 760 void ldv_check_return_value_probe(int res); 761 762 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Initializes the model. */ 763 void ldv_initialize(void); 764 765 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Reinitializes the model between distinct model function calls. */ 766 void ldv_handler_precall(void); 767 768 /* LDV_COMMENT_FUNCTION_DECLARE_LDV Special function for LDV verifier. Returns arbitrary interger value. */ 769 int nondet_int(void); 770 771 /* LDV_COMMENT_VAR_DECLARE_LDV Special variable for LDV verifier. */ 772 int LDV_IN_INTERRUPT; 773 774 /* LDV_COMMENT_FUNCTION_MAIN Main function for LDV verifier. */ 775 void ldv_main1_sequence_infinite_withcheck_stateful(void) { 776 777 778 779 /* LDV_COMMENT_BEGIN_VARIABLE_DECLARATION_PART */ 780 /*============================= VARIABLE DECLARATION PART =============================*/ 781 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 782 /* content: static int usbtv_querycap(struct file *file, void *priv, struct v4l2_capability *cap)*/ 783 /* LDV_COMMENT_END_PREP */ 784 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_querycap" */ 785 struct file * var_group1; 786 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_querycap" */ 787 void * var_usbtv_querycap_10_p1; 788 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_querycap" */ 789 struct v4l2_capability * var_usbtv_querycap_10_p2; 790 /* content: static int usbtv_enum_input(struct file *file, void *priv, struct v4l2_input *i)*/ 791 /* LDV_COMMENT_END_PREP */ 792 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_enum_input" */ 793 void * var_usbtv_enum_input_11_p1; 794 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_enum_input" */ 795 struct v4l2_input * var_usbtv_enum_input_11_p2; 796 /* content: static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *f)*/ 797 /* LDV_COMMENT_END_PREP */ 798 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_enum_fmt_vid_cap" */ 799 void * var_usbtv_enum_fmt_vid_cap_12_p1; 800 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_enum_fmt_vid_cap" */ 801 struct v4l2_fmtdesc * var_usbtv_enum_fmt_vid_cap_12_p2; 802 /* content: static int usbtv_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)*/ 803 /* LDV_COMMENT_END_PREP */ 804 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_fmt_vid_cap" */ 805 void * var_usbtv_fmt_vid_cap_13_p1; 806 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_fmt_vid_cap" */ 807 struct v4l2_format * var_usbtv_fmt_vid_cap_13_p2; 808 /* content: static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)*/ 809 /* LDV_COMMENT_END_PREP */ 810 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_g_std" */ 811 void * var_usbtv_g_std_14_p1; 812 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_g_std" */ 813 v4l2_std_id * var_usbtv_g_std_14_p2; 814 /* content: static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)*/ 815 /* LDV_COMMENT_END_PREP */ 816 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_s_std" */ 817 void * var_usbtv_s_std_15_p1; 818 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_s_std" */ 819 v4l2_std_id var_usbtv_s_std_15_p2; 820 /* content: static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)*/ 821 /* LDV_COMMENT_END_PREP */ 822 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_g_input" */ 823 void * var_usbtv_g_input_16_p1; 824 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_g_input" */ 825 unsigned int * var_usbtv_g_input_16_p2; 826 /* content: static int usbtv_s_input(struct file *file, void *priv, unsigned int i)*/ 827 /* LDV_COMMENT_END_PREP */ 828 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_s_input" */ 829 void * var_usbtv_s_input_17_p1; 830 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_s_input" */ 831 unsigned int var_usbtv_s_input_17_p2; 832 833 /** STRUCT: struct type: vb2_ops, struct name: usbtv_vb2_ops **/ 834 /* content: static void usbtv_buf_queue(struct vb2_buffer *vb)*/ 835 /* LDV_COMMENT_END_PREP */ 836 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_buf_queue" */ 837 struct vb2_buffer * var_group2; 838 /* content: static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)*/ 839 /* LDV_COMMENT_END_PREP */ 840 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_start_streaming" */ 841 struct vb2_queue * var_group3; 842 /* LDV_COMMENT_VAR_DECLARE Variable declaration for function "usbtv_start_streaming" */ 843 unsigned int var_usbtv_start_streaming_19_p1; 844 /* content: static void usbtv_stop_streaming(struct vb2_queue *vq)*/ 845 /* LDV_COMMENT_END_PREP */ 846 847 848 849 850 /* LDV_COMMENT_END_VARIABLE_DECLARATION_PART */ 851 /* LDV_COMMENT_BEGIN_VARIABLE_INITIALIZING_PART */ 852 /*============================= VARIABLE INITIALIZING PART =============================*/ 853 LDV_IN_INTERRUPT=1; 854 855 856 857 858 /* LDV_COMMENT_END_VARIABLE_INITIALIZING_PART */ 859 /* LDV_COMMENT_BEGIN_FUNCTION_CALL_SECTION */ 860 /*============================= FUNCTION CALL SECTION =============================*/ 861 /* LDV_COMMENT_FUNCTION_CALL Initialize LDV model. */ 862 ldv_initialize(); 863 864 865 866 867 868 while( nondet_int() 869 ) { 870 871 switch(nondet_int()) { 872 873 case 0: { 874 875 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 876 877 878 /* content: static int usbtv_querycap(struct file *file, void *priv, struct v4l2_capability *cap)*/ 879 /* LDV_COMMENT_END_PREP */ 880 /* LDV_COMMENT_FUNCTION_CALL Function from field "vidioc_querycap" from driver structure with callbacks "usbtv_ioctl_ops" */ 881 ldv_handler_precall(); 882 usbtv_querycap( var_group1, var_usbtv_querycap_10_p1, var_usbtv_querycap_10_p2); 883 884 885 886 887 } 888 889 break; 890 case 1: { 891 892 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 893 894 895 /* content: static int usbtv_enum_input(struct file *file, void *priv, struct v4l2_input *i)*/ 896 /* LDV_COMMENT_END_PREP */ 897 /* LDV_COMMENT_FUNCTION_CALL Function from field "vidioc_enum_input" from driver structure with callbacks "usbtv_ioctl_ops" */ 898 ldv_handler_precall(); 899 usbtv_enum_input( var_group1, var_usbtv_enum_input_11_p1, var_usbtv_enum_input_11_p2); 900 901 902 903 904 } 905 906 break; 907 case 2: { 908 909 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 910 911 912 /* content: static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *f)*/ 913 /* LDV_COMMENT_END_PREP */ 914 /* LDV_COMMENT_FUNCTION_CALL Function from field "vidioc_enum_fmt_vid_cap" from driver structure with callbacks "usbtv_ioctl_ops" */ 915 ldv_handler_precall(); 916 usbtv_enum_fmt_vid_cap( var_group1, var_usbtv_enum_fmt_vid_cap_12_p1, var_usbtv_enum_fmt_vid_cap_12_p2); 917 918 919 920 921 } 922 923 break; 924 case 3: { 925 926 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 927 928 929 /* content: static int usbtv_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)*/ 930 /* LDV_COMMENT_END_PREP */ 931 /* LDV_COMMENT_FUNCTION_CALL Function from field "vidioc_g_fmt_vid_cap" from driver structure with callbacks "usbtv_ioctl_ops" */ 932 ldv_handler_precall(); 933 usbtv_fmt_vid_cap( var_group1, var_usbtv_fmt_vid_cap_13_p1, var_usbtv_fmt_vid_cap_13_p2); 934 935 936 937 938 } 939 940 break; 941 case 4: { 942 943 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 944 945 946 /* content: static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)*/ 947 /* LDV_COMMENT_END_PREP */ 948 /* LDV_COMMENT_FUNCTION_CALL Function from field "vidioc_g_std" from driver structure with callbacks "usbtv_ioctl_ops" */ 949 ldv_handler_precall(); 950 usbtv_g_std( var_group1, var_usbtv_g_std_14_p1, var_usbtv_g_std_14_p2); 951 952 953 954 955 } 956 957 break; 958 case 5: { 959 960 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 961 962 963 /* content: static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)*/ 964 /* LDV_COMMENT_END_PREP */ 965 /* LDV_COMMENT_FUNCTION_CALL Function from field "vidioc_s_std" from driver structure with callbacks "usbtv_ioctl_ops" */ 966 ldv_handler_precall(); 967 usbtv_s_std( var_group1, var_usbtv_s_std_15_p1, var_usbtv_s_std_15_p2); 968 969 970 971 972 } 973 974 break; 975 case 6: { 976 977 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 978 979 980 /* content: static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)*/ 981 /* LDV_COMMENT_END_PREP */ 982 /* LDV_COMMENT_FUNCTION_CALL Function from field "vidioc_g_input" from driver structure with callbacks "usbtv_ioctl_ops" */ 983 ldv_handler_precall(); 984 usbtv_g_input( var_group1, var_usbtv_g_input_16_p1, var_usbtv_g_input_16_p2); 985 986 987 988 989 } 990 991 break; 992 case 7: { 993 994 /** STRUCT: struct type: v4l2_ioctl_ops, struct name: usbtv_ioctl_ops **/ 995 996 997 /* content: static int usbtv_s_input(struct file *file, void *priv, unsigned int i)*/ 998 /* LDV_COMMENT_END_PREP */ 999 /* LDV_COMMENT_FUNCTION_CALL Function from field "vidioc_s_input" from driver structure with callbacks "usbtv_ioctl_ops" */ 1000 ldv_handler_precall(); 1001 usbtv_s_input( var_group1, var_usbtv_s_input_17_p1, var_usbtv_s_input_17_p2); 1002 1003 1004 1005 1006 } 1007 1008 break; 1009 case 8: { 1010 1011 /** STRUCT: struct type: vb2_ops, struct name: usbtv_vb2_ops **/ 1012 1013 1014 /* content: static void usbtv_buf_queue(struct vb2_buffer *vb)*/ 1015 /* LDV_COMMENT_END_PREP */ 1016 /* LDV_COMMENT_FUNCTION_CALL Function from field "buf_queue" from driver structure with callbacks "usbtv_vb2_ops" */ 1017 ldv_handler_precall(); 1018 usbtv_buf_queue( var_group2); 1019 1020 1021 1022 1023 } 1024 1025 break; 1026 case 9: { 1027 1028 /** STRUCT: struct type: vb2_ops, struct name: usbtv_vb2_ops **/ 1029 1030 1031 /* content: static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)*/ 1032 /* LDV_COMMENT_END_PREP */ 1033 /* LDV_COMMENT_FUNCTION_CALL Function from field "start_streaming" from driver structure with callbacks "usbtv_vb2_ops" */ 1034 ldv_handler_precall(); 1035 usbtv_start_streaming( var_group3, var_usbtv_start_streaming_19_p1); 1036 1037 1038 1039 1040 } 1041 1042 break; 1043 case 10: { 1044 1045 /** STRUCT: struct type: vb2_ops, struct name: usbtv_vb2_ops **/ 1046 1047 1048 /* content: static void usbtv_stop_streaming(struct vb2_queue *vq)*/ 1049 /* LDV_COMMENT_END_PREP */ 1050 /* LDV_COMMENT_FUNCTION_CALL Function from field "stop_streaming" from driver structure with callbacks "usbtv_vb2_ops" */ 1051 ldv_handler_precall(); 1052 usbtv_stop_streaming( var_group3); 1053 1054 1055 1056 1057 } 1058 1059 break; 1060 default: break; 1061 1062 } 1063 1064 } 1065 1066 ldv_module_exit: 1067 1068 /* LDV_COMMENT_FUNCTION_CALL Checks that all resources and locks are correctly released before the driver will be unloaded. */ 1069 ldv_final: ldv_check_final_state(); 1070 1071 /* LDV_COMMENT_END_FUNCTION_CALL_SECTION */ 1072 return; 1073 1074 } 1075 #endif 1076 1077 /* LDV_COMMENT_END_MAIN */
1 2 #include <linux/kernel.h> 3 #include <linux/module.h> 4 5 #include <linux/usb.h> 6 7 #include <verifier/rcv.h> // For LDV auxiliary routines. 8 #include <kernel-model/ERR.inc> 9 10 // There are 3 possible states of usb device reference counter 11 enum 12 { 13 LDV_USB_DEV_ZERO_STATE = 0, // Usb device reference hasn't been acquired 14 LDV_USB_DEV_ACQUIRED = 1, // Usb device reference acquired 15 LDV_USB_DEV_INCREASED = 2 // Usb device reference counter increased 16 }; 17 18 /* LDV_COMMENT_OTHER The model automaton state (one of thee possible ones). */ 19 int ldv_usb_dev_state = LDV_USB_DEV_ZERO_STATE; 20 21 /* LDV_COMMENT_MODEL_FUNCTION_DEFINITION(name='ldv_interface_to_usbdev') Change state state after acquiring a reference to usb_device. */ 22 void ldv_interface_to_usbdev(void) 23 { 24 /* LDV_COMMENT_OTHER Initially we suppose this function is used to acquire a reference to usb_device. */ 25 if (ldv_usb_dev_state == LDV_USB_DEV_ZERO_STATE) 26 /* LDV_COMMENT_CHANGE_STATE Usb device reference acquired. */ 27 ldv_usb_dev_state = LDV_USB_DEV_ACQUIRED; 28 } 29 30 /* LDV_COMMENT_MODEL_FUNCTION_DEFINITION(name='ldv_usb_get_dev') Change state after increasing the reference counter with usb_get_dev. */ 31 void ldv_usb_get_dev(void) 32 { 33 /* LDV_COMMENT_OTHER Here the reference has surely been acquired somewhere. */ 34 if (ldv_usb_dev_state < LDV_USB_DEV_ACQUIRED) { 35 /* LDV_COMMENT_CHANGE_STATE The reference has already been acquired. */ 36 ldv_usb_dev_state = LDV_USB_DEV_ACQUIRED; 37 } 38 /* LDV_COMMENT_CHANGE_STATE Increase reference counter. */ 39 ldv_usb_dev_state++; 40 } 41 42 /* LDV_COMMENT_MODEL_FUNCTION_DEFINITION(name='ldv_usb_put_dev') Change state after decreasing the reference counter with usb_put_dev. */ 43 void ldv_usb_put_dev(void) 44 { 45 /* LDV_COMMENT_ASSERT Check usb device reference counter has been increased. */ 46 ldv_assert(ldv_usb_dev_state >= LDV_USB_DEV_INCREASED); 47 /* LDV_COMMENT_CHANGE_STATE Decrease reference counter. */ 48 ldv_usb_dev_state--; 49 /* LDV_COMMENT_OTHER LDV_USB_DEV_ACQUIRED is special (for when the one has forgotten to increase the counter). Not intednded to be used here. */ 50 if (ldv_usb_dev_state == LDV_USB_DEV_ACQUIRED) { 51 /* LDV_COMMENT_CHANGE_STATE Re-zero the model variable. */ 52 ldv_usb_dev_state = LDV_USB_DEV_ZERO_STATE; 53 } 54 } 55 56 /* LDV_COMMENT_MODEL_FUNCTION_DEFINITION(name='ldv_check_return_value_probe') Check the probe function leaved the model in the proper state. */ 57 void ldv_check_return_value_probe(int retval) 58 { 59 /* LDV_COMMENT_OTHER Probe finished unsuccessfully and returned an error. */ 60 if (retval) { 61 /* LDV_COMMENT_ASSERT Check usb device reference counter is not increased. */ 62 ldv_assert(ldv_usb_dev_state < LDV_USB_DEV_INCREASED); 63 /* LDV_COMMENT_OTHER LDV_USB_DEV_ACQUIRED is special (for when the one has forgotten to increase the counter). Not this case. */ 64 if (ldv_usb_dev_state == LDV_USB_DEV_ACQUIRED) 65 /* LDV_COMMENT_CHANGE_STATE Re-zero the model variable. */ 66 ldv_usb_dev_state = LDV_USB_DEV_ZERO_STATE; 67 } // else /* LDV_COMMENT_OTHER Probe finished successfully and returned 0. */ 68 // /* LDV_COMMENT_ASSERT Check usb device reference counter is not acquired or has been increased. */ 69 // ldv_assert(ldv_usb_dev_state != LDV_USB_DEV_ACQUIRED); 70 } 71 72 /* LDV_COMMENT_MODEL_FUNCTION_DEFINITION(name='ldv_check_final_state') Check that usb device reference hasn't been acquired or the counter has been decreased. */ 73 void ldv_check_final_state(void) 74 { 75 /* LDV_COMMENT_ASSERT Check that usb device reference hasn't been acquired or the counter has been decreased. */ 76 ldv_assert(ldv_usb_dev_state < LDV_USB_DEV_INCREASED); 77 }
1 /* 2 * device.h - generic, centralized driver model 3 * 4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org> 5 * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de> 6 * Copyright (c) 2008-2009 Novell Inc. 7 * 8 * This file is released under the GPLv2 9 * 10 * See Documentation/driver-model/ for more information. 11 */ 12 13 #ifndef _DEVICE_H_ 14 #define _DEVICE_H_ 15 16 #include <linux/ioport.h> 17 #include <linux/kobject.h> 18 #include <linux/klist.h> 19 #include <linux/list.h> 20 #include <linux/lockdep.h> 21 #include <linux/compiler.h> 22 #include <linux/types.h> 23 #include <linux/mutex.h> 24 #include <linux/pinctrl/devinfo.h> 25 #include <linux/pm.h> 26 #include <linux/atomic.h> 27 #include <linux/ratelimit.h> 28 #include <linux/uidgid.h> 29 #include <linux/gfp.h> 30 #include <asm/device.h> 31 32 struct device; 33 struct device_private; 34 struct device_driver; 35 struct driver_private; 36 struct module; 37 struct class; 38 struct subsys_private; 39 struct bus_type; 40 struct device_node; 41 struct iommu_ops; 42 struct iommu_group; 43 44 struct bus_attribute { 45 struct attribute attr; 46 ssize_t (*show)(struct bus_type *bus, char *buf); 47 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count); 48 }; 49 50 #define BUS_ATTR(_name, _mode, _show, _store) \ 51 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store) 52 #define BUS_ATTR_RW(_name) \ 53 struct bus_attribute bus_attr_##_name = __ATTR_RW(_name) 54 #define BUS_ATTR_RO(_name) \ 55 struct bus_attribute bus_attr_##_name = __ATTR_RO(_name) 56 57 extern int __must_check bus_create_file(struct bus_type *, 58 struct bus_attribute *); 59 extern void bus_remove_file(struct bus_type *, struct bus_attribute *); 60 61 /** 62 * struct bus_type - The bus type of the device 63 * 64 * @name: The name of the bus. 65 * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id). 66 * @dev_root: Default device to use as the parent. 67 * @dev_attrs: Default attributes of the devices on the bus. 68 * @bus_groups: Default attributes of the bus. 69 * @dev_groups: Default attributes of the devices on the bus. 70 * @drv_groups: Default attributes of the device drivers on the bus. 71 * @match: Called, perhaps multiple times, whenever a new device or driver 72 * is added for this bus. It should return a nonzero value if the 73 * given device can be handled by the given driver. 74 * @uevent: Called when a device is added, removed, or a few other things 75 * that generate uevents to add the environment variables. 76 * @probe: Called when a new device or driver add to this bus, and callback 77 * the specific driver's probe to initial the matched device. 78 * @remove: Called when a device removed from this bus. 79 * @shutdown: Called at shut-down time to quiesce the device. 80 * 81 * @online: Called to put the device back online (after offlining it). 82 * @offline: Called to put the device offline for hot-removal. May fail. 83 * 84 * @suspend: Called when a device on this bus wants to go to sleep mode. 85 * @resume: Called to bring a device on this bus out of sleep mode. 86 * @pm: Power management operations of this bus, callback the specific 87 * device driver's pm-ops. 88 * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU 89 * driver implementations to a bus and allow the driver to do 90 * bus-specific setup 91 * @p: The private data of the driver core, only the driver core can 92 * touch this. 93 * @lock_key: Lock class key for use by the lock validator 94 * 95 * A bus is a channel between the processor and one or more devices. For the 96 * purposes of the device model, all devices are connected via a bus, even if 97 * it is an internal, virtual, "platform" bus. Buses can plug into each other. 98 * A USB controller is usually a PCI device, for example. The device model 99 * represents the actual connections between buses and the devices they control. 100 * A bus is represented by the bus_type structure. It contains the name, the 101 * default attributes, the bus' methods, PM operations, and the driver core's 102 * private data. 103 */ 104 struct bus_type { 105 const char *name; 106 const char *dev_name; 107 struct device *dev_root; 108 struct device_attribute *dev_attrs; /* use dev_groups instead */ 109 const struct attribute_group **bus_groups; 110 const struct attribute_group **dev_groups; 111 const struct attribute_group **drv_groups; 112 113 int (*match)(struct device *dev, struct device_driver *drv); 114 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 115 int (*probe)(struct device *dev); 116 int (*remove)(struct device *dev); 117 void (*shutdown)(struct device *dev); 118 119 int (*online)(struct device *dev); 120 int (*offline)(struct device *dev); 121 122 int (*suspend)(struct device *dev, pm_message_t state); 123 int (*resume)(struct device *dev); 124 125 const struct dev_pm_ops *pm; 126 127 struct iommu_ops *iommu_ops; 128 129 struct subsys_private *p; 130 struct lock_class_key lock_key; 131 }; 132 133 extern int __must_check bus_register(struct bus_type *bus); 134 135 extern void bus_unregister(struct bus_type *bus); 136 137 extern int __must_check bus_rescan_devices(struct bus_type *bus); 138 139 /* iterator helpers for buses */ 140 struct subsys_dev_iter { 141 struct klist_iter ki; 142 const struct device_type *type; 143 }; 144 void subsys_dev_iter_init(struct subsys_dev_iter *iter, 145 struct bus_type *subsys, 146 struct device *start, 147 const struct device_type *type); 148 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter); 149 void subsys_dev_iter_exit(struct subsys_dev_iter *iter); 150 151 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, 152 int (*fn)(struct device *dev, void *data)); 153 struct device *bus_find_device(struct bus_type *bus, struct device *start, 154 void *data, 155 int (*match)(struct device *dev, void *data)); 156 struct device *bus_find_device_by_name(struct bus_type *bus, 157 struct device *start, 158 const char *name); 159 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id, 160 struct device *hint); 161 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 162 void *data, int (*fn)(struct device_driver *, void *)); 163 void bus_sort_breadthfirst(struct bus_type *bus, 164 int (*compare)(const struct device *a, 165 const struct device *b)); 166 /* 167 * Bus notifiers: Get notified of addition/removal of devices 168 * and binding/unbinding of drivers to devices. 169 * In the long run, it should be a replacement for the platform 170 * notify hooks. 171 */ 172 struct notifier_block; 173 174 extern int bus_register_notifier(struct bus_type *bus, 175 struct notifier_block *nb); 176 extern int bus_unregister_notifier(struct bus_type *bus, 177 struct notifier_block *nb); 178 179 /* All 4 notifers below get called with the target struct device * 180 * as an argument. Note that those functions are likely to be called 181 * with the device lock held in the core, so be careful. 182 */ 183 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */ 184 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */ 185 #define BUS_NOTIFY_BIND_DRIVER 0x00000003 /* driver about to be 186 bound */ 187 #define BUS_NOTIFY_BOUND_DRIVER 0x00000004 /* driver bound to device */ 188 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000005 /* driver about to be 189 unbound */ 190 #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006 /* driver is unbound 191 from the device */ 192 193 extern struct kset *bus_get_kset(struct bus_type *bus); 194 extern struct klist *bus_get_device_klist(struct bus_type *bus); 195 196 /** 197 * struct device_driver - The basic device driver structure 198 * @name: Name of the device driver. 199 * @bus: The bus which the device of this driver belongs to. 200 * @owner: The module owner. 201 * @mod_name: Used for built-in modules. 202 * @suppress_bind_attrs: Disables bind/unbind via sysfs. 203 * @of_match_table: The open firmware table. 204 * @acpi_match_table: The ACPI match table. 205 * @probe: Called to query the existence of a specific device, 206 * whether this driver can work with it, and bind the driver 207 * to a specific device. 208 * @remove: Called when the device is removed from the system to 209 * unbind a device from this driver. 210 * @shutdown: Called at shut-down time to quiesce the device. 211 * @suspend: Called to put the device to sleep mode. Usually to a 212 * low power state. 213 * @resume: Called to bring a device from sleep mode. 214 * @groups: Default attributes that get created by the driver core 215 * automatically. 216 * @pm: Power management operations of the device which matched 217 * this driver. 218 * @p: Driver core's private data, no one other than the driver 219 * core can touch this. 220 * 221 * The device driver-model tracks all of the drivers known to the system. 222 * The main reason for this tracking is to enable the driver core to match 223 * up drivers with new devices. Once drivers are known objects within the 224 * system, however, a number of other things become possible. Device drivers 225 * can export information and configuration variables that are independent 226 * of any specific device. 227 */ 228 struct device_driver { 229 const char *name; 230 struct bus_type *bus; 231 232 struct module *owner; 233 const char *mod_name; /* used for built-in modules */ 234 235 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */ 236 237 const struct of_device_id *of_match_table; 238 const struct acpi_device_id *acpi_match_table; 239 240 int (*probe) (struct device *dev); 241 int (*remove) (struct device *dev); 242 void (*shutdown) (struct device *dev); 243 int (*suspend) (struct device *dev, pm_message_t state); 244 int (*resume) (struct device *dev); 245 const struct attribute_group **groups; 246 247 const struct dev_pm_ops *pm; 248 249 struct driver_private *p; 250 }; 251 252 253 extern int __must_check driver_register(struct device_driver *drv); 254 extern void driver_unregister(struct device_driver *drv); 255 256 extern struct device_driver *driver_find(const char *name, 257 struct bus_type *bus); 258 extern int driver_probe_done(void); 259 extern void wait_for_device_probe(void); 260 261 262 /* sysfs interface for exporting driver attributes */ 263 264 struct driver_attribute { 265 struct attribute attr; 266 ssize_t (*show)(struct device_driver *driver, char *buf); 267 ssize_t (*store)(struct device_driver *driver, const char *buf, 268 size_t count); 269 }; 270 271 #define DRIVER_ATTR(_name, _mode, _show, _store) \ 272 struct driver_attribute driver_attr_##_name = __ATTR(_name, _mode, _show, _store) 273 #define DRIVER_ATTR_RW(_name) \ 274 struct driver_attribute driver_attr_##_name = __ATTR_RW(_name) 275 #define DRIVER_ATTR_RO(_name) \ 276 struct driver_attribute driver_attr_##_name = __ATTR_RO(_name) 277 #define DRIVER_ATTR_WO(_name) \ 278 struct driver_attribute driver_attr_##_name = __ATTR_WO(_name) 279 280 extern int __must_check driver_create_file(struct device_driver *driver, 281 const struct driver_attribute *attr); 282 extern void driver_remove_file(struct device_driver *driver, 283 const struct driver_attribute *attr); 284 285 extern int __must_check driver_for_each_device(struct device_driver *drv, 286 struct device *start, 287 void *data, 288 int (*fn)(struct device *dev, 289 void *)); 290 struct device *driver_find_device(struct device_driver *drv, 291 struct device *start, void *data, 292 int (*match)(struct device *dev, void *data)); 293 294 /** 295 * struct subsys_interface - interfaces to device functions 296 * @name: name of the device function 297 * @subsys: subsytem of the devices to attach to 298 * @node: the list of functions registered at the subsystem 299 * @add_dev: device hookup to device function handler 300 * @remove_dev: device hookup to device function handler 301 * 302 * Simple interfaces attached to a subsystem. Multiple interfaces can 303 * attach to a subsystem and its devices. Unlike drivers, they do not 304 * exclusively claim or control devices. Interfaces usually represent 305 * a specific functionality of a subsystem/class of devices. 306 */ 307 struct subsys_interface { 308 const char *name; 309 struct bus_type *subsys; 310 struct list_head node; 311 int (*add_dev)(struct device *dev, struct subsys_interface *sif); 312 int (*remove_dev)(struct device *dev, struct subsys_interface *sif); 313 }; 314 315 int subsys_interface_register(struct subsys_interface *sif); 316 void subsys_interface_unregister(struct subsys_interface *sif); 317 318 int subsys_system_register(struct bus_type *subsys, 319 const struct attribute_group **groups); 320 int subsys_virtual_register(struct bus_type *subsys, 321 const struct attribute_group **groups); 322 323 /** 324 * struct class - device classes 325 * @name: Name of the class. 326 * @owner: The module owner. 327 * @class_attrs: Default attributes of this class. 328 * @dev_groups: Default attributes of the devices that belong to the class. 329 * @dev_kobj: The kobject that represents this class and links it into the hierarchy. 330 * @dev_uevent: Called when a device is added, removed from this class, or a 331 * few other things that generate uevents to add the environment 332 * variables. 333 * @devnode: Callback to provide the devtmpfs. 334 * @class_release: Called to release this class. 335 * @dev_release: Called to release the device. 336 * @suspend: Used to put the device to sleep mode, usually to a low power 337 * state. 338 * @resume: Used to bring the device from the sleep mode. 339 * @ns_type: Callbacks so sysfs can detemine namespaces. 340 * @namespace: Namespace of the device belongs to this class. 341 * @pm: The default device power management operations of this class. 342 * @p: The private data of the driver core, no one other than the 343 * driver core can touch this. 344 * 345 * A class is a higher-level view of a device that abstracts out low-level 346 * implementation details. Drivers may see a SCSI disk or an ATA disk, but, 347 * at the class level, they are all simply disks. Classes allow user space 348 * to work with devices based on what they do, rather than how they are 349 * connected or how they work. 350 */ 351 struct class { 352 const char *name; 353 struct module *owner; 354 355 struct class_attribute *class_attrs; 356 const struct attribute_group **dev_groups; 357 struct kobject *dev_kobj; 358 359 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); 360 char *(*devnode)(struct device *dev, umode_t *mode); 361 362 void (*class_release)(struct class *class); 363 void (*dev_release)(struct device *dev); 364 365 int (*suspend)(struct device *dev, pm_message_t state); 366 int (*resume)(struct device *dev); 367 368 const struct kobj_ns_type_operations *ns_type; 369 const void *(*namespace)(struct device *dev); 370 371 const struct dev_pm_ops *pm; 372 373 struct subsys_private *p; 374 }; 375 376 struct class_dev_iter { 377 struct klist_iter ki; 378 const struct device_type *type; 379 }; 380 381 extern struct kobject *sysfs_dev_block_kobj; 382 extern struct kobject *sysfs_dev_char_kobj; 383 extern int __must_check __class_register(struct class *class, 384 struct lock_class_key *key); 385 extern void class_unregister(struct class *class); 386 387 /* This is a #define to keep the compiler from merging different 388 * instances of the __key variable */ 389 #define class_register(class) \ 390 ({ \ 391 static struct lock_class_key __key; \ 392 __class_register(class, &__key); \ 393 }) 394 395 struct class_compat; 396 struct class_compat *class_compat_register(const char *name); 397 void class_compat_unregister(struct class_compat *cls); 398 int class_compat_create_link(struct class_compat *cls, struct device *dev, 399 struct device *device_link); 400 void class_compat_remove_link(struct class_compat *cls, struct device *dev, 401 struct device *device_link); 402 403 extern void class_dev_iter_init(struct class_dev_iter *iter, 404 struct class *class, 405 struct device *start, 406 const struct device_type *type); 407 extern struct device *class_dev_iter_next(struct class_dev_iter *iter); 408 extern void class_dev_iter_exit(struct class_dev_iter *iter); 409 410 extern int class_for_each_device(struct class *class, struct device *start, 411 void *data, 412 int (*fn)(struct device *dev, void *data)); 413 extern struct device *class_find_device(struct class *class, 414 struct device *start, const void *data, 415 int (*match)(struct device *, const void *)); 416 417 struct class_attribute { 418 struct attribute attr; 419 ssize_t (*show)(struct class *class, struct class_attribute *attr, 420 char *buf); 421 ssize_t (*store)(struct class *class, struct class_attribute *attr, 422 const char *buf, size_t count); 423 }; 424 425 #define CLASS_ATTR(_name, _mode, _show, _store) \ 426 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store) 427 #define CLASS_ATTR_RW(_name) \ 428 struct class_attribute class_attr_##_name = __ATTR_RW(_name) 429 #define CLASS_ATTR_RO(_name) \ 430 struct class_attribute class_attr_##_name = __ATTR_RO(_name) 431 432 extern int __must_check class_create_file_ns(struct class *class, 433 const struct class_attribute *attr, 434 const void *ns); 435 extern void class_remove_file_ns(struct class *class, 436 const struct class_attribute *attr, 437 const void *ns); 438 439 static inline int __must_check class_create_file(struct class *class, 440 const struct class_attribute *attr) 441 { 442 return class_create_file_ns(class, attr, NULL); 443 } 444 445 static inline void class_remove_file(struct class *class, 446 const struct class_attribute *attr) 447 { 448 return class_remove_file_ns(class, attr, NULL); 449 } 450 451 /* Simple class attribute that is just a static string */ 452 struct class_attribute_string { 453 struct class_attribute attr; 454 char *str; 455 }; 456 457 /* Currently read-only only */ 458 #define _CLASS_ATTR_STRING(_name, _mode, _str) \ 459 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } 460 #define CLASS_ATTR_STRING(_name, _mode, _str) \ 461 struct class_attribute_string class_attr_##_name = \ 462 _CLASS_ATTR_STRING(_name, _mode, _str) 463 464 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr, 465 char *buf); 466 467 struct class_interface { 468 struct list_head node; 469 struct class *class; 470 471 int (*add_dev) (struct device *, struct class_interface *); 472 void (*remove_dev) (struct device *, struct class_interface *); 473 }; 474 475 extern int __must_check class_interface_register(struct class_interface *); 476 extern void class_interface_unregister(struct class_interface *); 477 478 extern struct class * __must_check __class_create(struct module *owner, 479 const char *name, 480 struct lock_class_key *key); 481 extern void class_destroy(struct class *cls); 482 483 /* This is a #define to keep the compiler from merging different 484 * instances of the __key variable */ 485 #define class_create(owner, name) \ 486 ({ \ 487 static struct lock_class_key __key; \ 488 __class_create(owner, name, &__key); \ 489 }) 490 491 /* 492 * The type of device, "struct device" is embedded in. A class 493 * or bus can contain devices of different types 494 * like "partitions" and "disks", "mouse" and "event". 495 * This identifies the device type and carries type-specific 496 * information, equivalent to the kobj_type of a kobject. 497 * If "name" is specified, the uevent will contain it in 498 * the DEVTYPE variable. 499 */ 500 struct device_type { 501 const char *name; 502 const struct attribute_group **groups; 503 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 504 char *(*devnode)(struct device *dev, umode_t *mode, 505 kuid_t *uid, kgid_t *gid); 506 void (*release)(struct device *dev); 507 508 const struct dev_pm_ops *pm; 509 }; 510 511 /* interface for exporting device attributes */ 512 struct device_attribute { 513 struct attribute attr; 514 ssize_t (*show)(struct device *dev, struct device_attribute *attr, 515 char *buf); 516 ssize_t (*store)(struct device *dev, struct device_attribute *attr, 517 const char *buf, size_t count); 518 }; 519 520 struct dev_ext_attribute { 521 struct device_attribute attr; 522 void *var; 523 }; 524 525 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr, 526 char *buf); 527 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr, 528 const char *buf, size_t count); 529 ssize_t device_show_int(struct device *dev, struct device_attribute *attr, 530 char *buf); 531 ssize_t device_store_int(struct device *dev, struct device_attribute *attr, 532 const char *buf, size_t count); 533 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, 534 char *buf); 535 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, 536 const char *buf, size_t count); 537 538 #define DEVICE_ATTR(_name, _mode, _show, _store) \ 539 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) 540 #define DEVICE_ATTR_RW(_name) \ 541 struct device_attribute dev_attr_##_name = __ATTR_RW(_name) 542 #define DEVICE_ATTR_RO(_name) \ 543 struct device_attribute dev_attr_##_name = __ATTR_RO(_name) 544 #define DEVICE_ATTR_WO(_name) \ 545 struct device_attribute dev_attr_##_name = __ATTR_WO(_name) 546 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \ 547 struct dev_ext_attribute dev_attr_##_name = \ 548 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } 549 #define DEVICE_INT_ATTR(_name, _mode, _var) \ 550 struct dev_ext_attribute dev_attr_##_name = \ 551 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) } 552 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \ 553 struct dev_ext_attribute dev_attr_##_name = \ 554 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) } 555 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ 556 struct device_attribute dev_attr_##_name = \ 557 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) 558 559 extern int device_create_file(struct device *device, 560 const struct device_attribute *entry); 561 extern void device_remove_file(struct device *dev, 562 const struct device_attribute *attr); 563 extern bool device_remove_file_self(struct device *dev, 564 const struct device_attribute *attr); 565 extern int __must_check device_create_bin_file(struct device *dev, 566 const struct bin_attribute *attr); 567 extern void device_remove_bin_file(struct device *dev, 568 const struct bin_attribute *attr); 569 570 /* device resource management */ 571 typedef void (*dr_release_t)(struct device *dev, void *res); 572 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data); 573 574 #ifdef CONFIG_DEBUG_DEVRES 575 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp, 576 const char *name); 577 #define devres_alloc(release, size, gfp) \ 578 __devres_alloc(release, size, gfp, #release) 579 #else 580 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp); 581 #endif 582 extern void devres_for_each_res(struct device *dev, dr_release_t release, 583 dr_match_t match, void *match_data, 584 void (*fn)(struct device *, void *, void *), 585 void *data); 586 extern void devres_free(void *res); 587 extern void devres_add(struct device *dev, void *res); 588 extern void *devres_find(struct device *dev, dr_release_t release, 589 dr_match_t match, void *match_data); 590 extern void *devres_get(struct device *dev, void *new_res, 591 dr_match_t match, void *match_data); 592 extern void *devres_remove(struct device *dev, dr_release_t release, 593 dr_match_t match, void *match_data); 594 extern int devres_destroy(struct device *dev, dr_release_t release, 595 dr_match_t match, void *match_data); 596 extern int devres_release(struct device *dev, dr_release_t release, 597 dr_match_t match, void *match_data); 598 599 /* devres group */ 600 extern void * __must_check devres_open_group(struct device *dev, void *id, 601 gfp_t gfp); 602 extern void devres_close_group(struct device *dev, void *id); 603 extern void devres_remove_group(struct device *dev, void *id); 604 extern int devres_release_group(struct device *dev, void *id); 605 606 /* managed devm_k.alloc/kfree for device drivers */ 607 extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp); 608 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) 609 { 610 return devm_kmalloc(dev, size, gfp | __GFP_ZERO); 611 } 612 static inline void *devm_kmalloc_array(struct device *dev, 613 size_t n, size_t size, gfp_t flags) 614 { 615 if (size != 0 && n > SIZE_MAX / size) 616 return NULL; 617 return devm_kmalloc(dev, n * size, flags); 618 } 619 static inline void *devm_kcalloc(struct device *dev, 620 size_t n, size_t size, gfp_t flags) 621 { 622 return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO); 623 } 624 extern void devm_kfree(struct device *dev, void *p); 625 extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp); 626 extern void *devm_kmemdup(struct device *dev, const void *src, size_t len, 627 gfp_t gfp); 628 629 extern unsigned long devm_get_free_pages(struct device *dev, 630 gfp_t gfp_mask, unsigned int order); 631 extern void devm_free_pages(struct device *dev, unsigned long addr); 632 633 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res); 634 void __iomem *devm_request_and_ioremap(struct device *dev, 635 struct resource *res); 636 637 /* allows to add/remove a custom action to devres stack */ 638 int devm_add_action(struct device *dev, void (*action)(void *), void *data); 639 void devm_remove_action(struct device *dev, void (*action)(void *), void *data); 640 641 struct device_dma_parameters { 642 /* 643 * a low level driver may set these to teach IOMMU code about 644 * sg limitations. 645 */ 646 unsigned int max_segment_size; 647 unsigned long segment_boundary_mask; 648 }; 649 650 struct acpi_device; 651 652 struct acpi_dev_node { 653 #ifdef CONFIG_ACPI 654 struct acpi_device *companion; 655 #endif 656 }; 657 658 /** 659 * struct device - The basic device structure 660 * @parent: The device's "parent" device, the device to which it is attached. 661 * In most cases, a parent device is some sort of bus or host 662 * controller. If parent is NULL, the device, is a top-level device, 663 * which is not usually what you want. 664 * @p: Holds the private data of the driver core portions of the device. 665 * See the comment of the struct device_private for detail. 666 * @kobj: A top-level, abstract class from which other classes are derived. 667 * @init_name: Initial name of the device. 668 * @type: The type of device. 669 * This identifies the device type and carries type-specific 670 * information. 671 * @mutex: Mutex to synchronize calls to its driver. 672 * @bus: Type of bus device is on. 673 * @driver: Which driver has allocated this 674 * @platform_data: Platform data specific to the device. 675 * Example: For devices on custom boards, as typical of embedded 676 * and SOC based hardware, Linux often uses platform_data to point 677 * to board-specific structures describing devices and how they 678 * are wired. That can include what ports are available, chip 679 * variants, which GPIO pins act in what additional roles, and so 680 * on. This shrinks the "Board Support Packages" (BSPs) and 681 * minimizes board-specific #ifdefs in drivers. 682 * @driver_data: Private pointer for driver specific info. 683 * @power: For device power management. 684 * See Documentation/power/devices.txt for details. 685 * @pm_domain: Provide callbacks that are executed during system suspend, 686 * hibernation, system resume and during runtime PM transitions 687 * along with subsystem-level and driver-level callbacks. 688 * @pins: For device pin management. 689 * See Documentation/pinctrl.txt for details. 690 * @numa_node: NUMA node this device is close to. 691 * @dma_mask: Dma mask (if dma'ble device). 692 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 693 * hardware supports 64-bit addresses for consistent allocations 694 * such descriptors. 695 * @dma_pfn_offset: offset of DMA memory range relatively of RAM 696 * @dma_parms: A low level driver may set these to teach IOMMU code about 697 * segment limitations. 698 * @dma_pools: Dma pools (if dma'ble device). 699 * @dma_mem: Internal for coherent mem override. 700 * @cma_area: Contiguous memory area for dma allocations 701 * @archdata: For arch-specific additions. 702 * @of_node: Associated device tree node. 703 * @acpi_node: Associated ACPI device node. 704 * @devt: For creating the sysfs "dev". 705 * @id: device instance 706 * @devres_lock: Spinlock to protect the resource of the device. 707 * @devres_head: The resources list of the device. 708 * @knode_class: The node used to add the device to the class list. 709 * @class: The class of the device. 710 * @groups: Optional attribute groups. 711 * @release: Callback to free the device after all references have 712 * gone away. This should be set by the allocator of the 713 * device (i.e. the bus driver that discovered the device). 714 * @iommu_group: IOMMU group the device belongs to. 715 * 716 * @offline_disabled: If set, the device is permanently online. 717 * @offline: Set after successful invocation of bus type's .offline(). 718 * 719 * At the lowest level, every device in a Linux system is represented by an 720 * instance of struct device. The device structure contains the information 721 * that the device model core needs to model the system. Most subsystems, 722 * however, track additional information about the devices they host. As a 723 * result, it is rare for devices to be represented by bare device structures; 724 * instead, that structure, like kobject structures, is usually embedded within 725 * a higher-level representation of the device. 726 */ 727 struct device { 728 struct device *parent; 729 730 struct device_private *p; 731 732 struct kobject kobj; 733 const char *init_name; /* initial name of the device */ 734 const struct device_type *type; 735 736 struct mutex mutex; /* mutex to synchronize calls to 737 * its driver. 738 */ 739 740 struct bus_type *bus; /* type of bus device is on */ 741 struct device_driver *driver; /* which driver has allocated this 742 device */ 743 void *platform_data; /* Platform specific data, device 744 core doesn't touch it */ 745 void *driver_data; /* Driver data, set and get with 746 dev_set/get_drvdata */ 747 struct dev_pm_info power; 748 struct dev_pm_domain *pm_domain; 749 750 #ifdef CONFIG_PINCTRL 751 struct dev_pin_info *pins; 752 #endif 753 754 #ifdef CONFIG_NUMA 755 int numa_node; /* NUMA node this device is close to */ 756 #endif 757 u64 *dma_mask; /* dma mask (if dma'able device) */ 758 u64 coherent_dma_mask;/* Like dma_mask, but for 759 alloc_coherent mappings as 760 not all hardware supports 761 64 bit addresses for consistent 762 allocations such descriptors. */ 763 unsigned long dma_pfn_offset; 764 765 struct device_dma_parameters *dma_parms; 766 767 struct list_head dma_pools; /* dma pools (if dma'ble) */ 768 769 struct dma_coherent_mem *dma_mem; /* internal for coherent mem 770 override */ 771 #ifdef CONFIG_DMA_CMA 772 struct cma *cma_area; /* contiguous memory area for dma 773 allocations */ 774 #endif 775 /* arch specific additions */ 776 struct dev_archdata archdata; 777 778 struct device_node *of_node; /* associated device tree node */ 779 struct acpi_dev_node acpi_node; /* associated ACPI device node */ 780 781 dev_t devt; /* dev_t, creates the sysfs "dev" */ 782 u32 id; /* device instance */ 783 784 spinlock_t devres_lock; 785 struct list_head devres_head; 786 787 struct klist_node knode_class; 788 struct class *class; 789 const struct attribute_group **groups; /* optional groups */ 790 791 void (*release)(struct device *dev); 792 struct iommu_group *iommu_group; 793 794 bool offline_disabled:1; 795 bool offline:1; 796 }; 797 798 static inline struct device *kobj_to_dev(struct kobject *kobj) 799 { 800 return container_of(kobj, struct device, kobj); 801 } 802 803 /* Get the wakeup routines, which depend on struct device */ 804 #include <linux/pm_wakeup.h> 805 806 static inline const char *dev_name(const struct device *dev) 807 { 808 /* Use the init name until the kobject becomes available */ 809 if (dev->init_name) 810 return dev->init_name; 811 812 return kobject_name(&dev->kobj); 813 } 814 815 extern __printf(2, 3) 816 int dev_set_name(struct device *dev, const char *name, ...); 817 818 #ifdef CONFIG_NUMA 819 static inline int dev_to_node(struct device *dev) 820 { 821 return dev->numa_node; 822 } 823 static inline void set_dev_node(struct device *dev, int node) 824 { 825 dev->numa_node = node; 826 } 827 #else 828 static inline int dev_to_node(struct device *dev) 829 { 830 return -1; 831 } 832 static inline void set_dev_node(struct device *dev, int node) 833 { 834 } 835 #endif 836 837 static inline void *dev_get_drvdata(const struct device *dev) 838 { 839 return dev->driver_data; 840 } 841 842 static inline void dev_set_drvdata(struct device *dev, void *data) 843 { 844 dev->driver_data = data; 845 } 846 847 static inline struct pm_subsys_data *dev_to_psd(struct device *dev) 848 { 849 return dev ? dev->power.subsys_data : NULL; 850 } 851 852 static inline unsigned int dev_get_uevent_suppress(const struct device *dev) 853 { 854 return dev->kobj.uevent_suppress; 855 } 856 857 static inline void dev_set_uevent_suppress(struct device *dev, int val) 858 { 859 dev->kobj.uevent_suppress = val; 860 } 861 862 static inline int device_is_registered(struct device *dev) 863 { 864 return dev->kobj.state_in_sysfs; 865 } 866 867 static inline void device_enable_async_suspend(struct device *dev) 868 { 869 if (!dev->power.is_prepared) 870 dev->power.async_suspend = true; 871 } 872 873 static inline void device_disable_async_suspend(struct device *dev) 874 { 875 if (!dev->power.is_prepared) 876 dev->power.async_suspend = false; 877 } 878 879 static inline bool device_async_suspend_enabled(struct device *dev) 880 { 881 return !!dev->power.async_suspend; 882 } 883 884 static inline void pm_suspend_ignore_children(struct device *dev, bool enable) 885 { 886 dev->power.ignore_children = enable; 887 } 888 889 static inline void dev_pm_syscore_device(struct device *dev, bool val) 890 { 891 #ifdef CONFIG_PM_SLEEP 892 dev->power.syscore = val; 893 #endif 894 } 895 896 static inline void device_lock(struct device *dev) 897 { 898 mutex_lock(&dev->mutex); 899 } 900 901 static inline int device_trylock(struct device *dev) 902 { 903 return mutex_trylock(&dev->mutex); 904 } 905 906 static inline void device_unlock(struct device *dev) 907 { 908 mutex_unlock(&dev->mutex); 909 } 910 911 void driver_init(void); 912 913 /* 914 * High level routines for use by the bus drivers 915 */ 916 extern int __must_check device_register(struct device *dev); 917 extern void device_unregister(struct device *dev); 918 extern void device_initialize(struct device *dev); 919 extern int __must_check device_add(struct device *dev); 920 extern void device_del(struct device *dev); 921 extern int device_for_each_child(struct device *dev, void *data, 922 int (*fn)(struct device *dev, void *data)); 923 extern struct device *device_find_child(struct device *dev, void *data, 924 int (*match)(struct device *dev, void *data)); 925 extern int device_rename(struct device *dev, const char *new_name); 926 extern int device_move(struct device *dev, struct device *new_parent, 927 enum dpm_order dpm_order); 928 extern const char *device_get_devnode(struct device *dev, 929 umode_t *mode, kuid_t *uid, kgid_t *gid, 930 const char **tmp); 931 932 static inline bool device_supports_offline(struct device *dev) 933 { 934 return dev->bus && dev->bus->offline && dev->bus->online; 935 } 936 937 extern void lock_device_hotplug(void); 938 extern void unlock_device_hotplug(void); 939 extern int lock_device_hotplug_sysfs(void); 940 extern int device_offline(struct device *dev); 941 extern int device_online(struct device *dev); 942 /* 943 * Root device objects for grouping under /sys/devices 944 */ 945 extern struct device *__root_device_register(const char *name, 946 struct module *owner); 947 948 /* This is a macro to avoid include problems with THIS_MODULE */ 949 #define root_device_register(name) \ 950 __root_device_register(name, THIS_MODULE) 951 952 extern void root_device_unregister(struct device *root); 953 954 static inline void *dev_get_platdata(const struct device *dev) 955 { 956 return dev->platform_data; 957 } 958 959 /* 960 * Manual binding of a device to driver. See drivers/base/bus.c 961 * for information on use. 962 */ 963 extern int __must_check device_bind_driver(struct device *dev); 964 extern void device_release_driver(struct device *dev); 965 extern int __must_check device_attach(struct device *dev); 966 extern int __must_check driver_attach(struct device_driver *drv); 967 extern int __must_check device_reprobe(struct device *dev); 968 969 /* 970 * Easy functions for dynamically creating devices on the fly 971 */ 972 extern struct device *device_create_vargs(struct class *cls, 973 struct device *parent, 974 dev_t devt, 975 void *drvdata, 976 const char *fmt, 977 va_list vargs); 978 extern __printf(5, 6) 979 struct device *device_create(struct class *cls, struct device *parent, 980 dev_t devt, void *drvdata, 981 const char *fmt, ...); 982 extern __printf(6, 7) 983 struct device *device_create_with_groups(struct class *cls, 984 struct device *parent, dev_t devt, void *drvdata, 985 const struct attribute_group **groups, 986 const char *fmt, ...); 987 extern void device_destroy(struct class *cls, dev_t devt); 988 989 /* 990 * Platform "fixup" functions - allow the platform to have their say 991 * about devices and actions that the general device layer doesn't 992 * know about. 993 */ 994 /* Notify platform of device discovery */ 995 extern int (*platform_notify)(struct device *dev); 996 997 extern int (*platform_notify_remove)(struct device *dev); 998 999 1000 /* 1001 * get_device - atomically increment the reference count for the device. 1002 * 1003 */ 1004 extern struct device *get_device(struct device *dev); 1005 extern void put_device(struct device *dev); 1006 1007 #ifdef CONFIG_DEVTMPFS 1008 extern int devtmpfs_create_node(struct device *dev); 1009 extern int devtmpfs_delete_node(struct device *dev); 1010 extern int devtmpfs_mount(const char *mntdir); 1011 #else 1012 static inline int devtmpfs_create_node(struct device *dev) { return 0; } 1013 static inline int devtmpfs_delete_node(struct device *dev) { return 0; } 1014 static inline int devtmpfs_mount(const char *mountpoint) { return 0; } 1015 #endif 1016 1017 /* drivers/base/power/shutdown.c */ 1018 extern void device_shutdown(void); 1019 1020 /* debugging and troubleshooting/diagnostic helpers. */ 1021 extern const char *dev_driver_string(const struct device *dev); 1022 1023 1024 #ifdef CONFIG_PRINTK 1025 1026 extern __printf(3, 0) 1027 int dev_vprintk_emit(int level, const struct device *dev, 1028 const char *fmt, va_list args); 1029 extern __printf(3, 4) 1030 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...); 1031 1032 extern __printf(3, 4) 1033 int dev_printk(const char *level, const struct device *dev, 1034 const char *fmt, ...); 1035 extern __printf(2, 3) 1036 int dev_emerg(const struct device *dev, const char *fmt, ...); 1037 extern __printf(2, 3) 1038 int dev_alert(const struct device *dev, const char *fmt, ...); 1039 extern __printf(2, 3) 1040 int dev_crit(const struct device *dev, const char *fmt, ...); 1041 extern __printf(2, 3) 1042 int dev_err(const struct device *dev, const char *fmt, ...); 1043 extern __printf(2, 3) 1044 int dev_warn(const struct device *dev, const char *fmt, ...); 1045 extern __printf(2, 3) 1046 int dev_notice(const struct device *dev, const char *fmt, ...); 1047 extern __printf(2, 3) 1048 int _dev_info(const struct device *dev, const char *fmt, ...); 1049 1050 #else 1051 1052 static inline __printf(3, 0) 1053 int dev_vprintk_emit(int level, const struct device *dev, 1054 const char *fmt, va_list args) 1055 { return 0; } 1056 static inline __printf(3, 4) 1057 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) 1058 { return 0; } 1059 1060 static inline int __dev_printk(const char *level, const struct device *dev, 1061 struct va_format *vaf) 1062 { return 0; } 1063 static inline __printf(3, 4) 1064 int dev_printk(const char *level, const struct device *dev, 1065 const char *fmt, ...) 1066 { return 0; } 1067 1068 static inline __printf(2, 3) 1069 int dev_emerg(const struct device *dev, const char *fmt, ...) 1070 { return 0; } 1071 static inline __printf(2, 3) 1072 int dev_crit(const struct device *dev, const char *fmt, ...) 1073 { return 0; } 1074 static inline __printf(2, 3) 1075 int dev_alert(const struct device *dev, const char *fmt, ...) 1076 { return 0; } 1077 static inline __printf(2, 3) 1078 int dev_err(const struct device *dev, const char *fmt, ...) 1079 { return 0; } 1080 static inline __printf(2, 3) 1081 int dev_warn(const struct device *dev, const char *fmt, ...) 1082 { return 0; } 1083 static inline __printf(2, 3) 1084 int dev_notice(const struct device *dev, const char *fmt, ...) 1085 { return 0; } 1086 static inline __printf(2, 3) 1087 int _dev_info(const struct device *dev, const char *fmt, ...) 1088 { return 0; } 1089 1090 #endif 1091 1092 /* 1093 * Stupid hackaround for existing uses of non-printk uses dev_info 1094 * 1095 * Note that the definition of dev_info below is actually _dev_info 1096 * and a macro is used to avoid redefining dev_info 1097 */ 1098 1099 #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg) 1100 1101 #if defined(CONFIG_DYNAMIC_DEBUG) 1102 #define dev_dbg(dev, format, ...) \ 1103 do { \ 1104 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \ 1105 } while (0) 1106 #elif defined(DEBUG) 1107 #define dev_dbg(dev, format, arg...) \ 1108 dev_printk(KERN_DEBUG, dev, format, ##arg) 1109 #else 1110 #define dev_dbg(dev, format, arg...) \ 1111 ({ \ 1112 if (0) \ 1113 dev_printk(KERN_DEBUG, dev, format, ##arg); \ 1114 0; \ 1115 }) 1116 #endif 1117 1118 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \ 1119 do { \ 1120 static DEFINE_RATELIMIT_STATE(_rs, \ 1121 DEFAULT_RATELIMIT_INTERVAL, \ 1122 DEFAULT_RATELIMIT_BURST); \ 1123 if (__ratelimit(&_rs)) \ 1124 dev_level(dev, fmt, ##__VA_ARGS__); \ 1125 } while (0) 1126 1127 #define dev_emerg_ratelimited(dev, fmt, ...) \ 1128 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__) 1129 #define dev_alert_ratelimited(dev, fmt, ...) \ 1130 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__) 1131 #define dev_crit_ratelimited(dev, fmt, ...) \ 1132 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__) 1133 #define dev_err_ratelimited(dev, fmt, ...) \ 1134 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__) 1135 #define dev_warn_ratelimited(dev, fmt, ...) \ 1136 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__) 1137 #define dev_notice_ratelimited(dev, fmt, ...) \ 1138 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__) 1139 #define dev_info_ratelimited(dev, fmt, ...) \ 1140 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__) 1141 #if defined(CONFIG_DYNAMIC_DEBUG) 1142 /* descriptor check is first to prevent flooding with "callbacks suppressed" */ 1143 #define dev_dbg_ratelimited(dev, fmt, ...) \ 1144 do { \ 1145 static DEFINE_RATELIMIT_STATE(_rs, \ 1146 DEFAULT_RATELIMIT_INTERVAL, \ 1147 DEFAULT_RATELIMIT_BURST); \ 1148 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ 1149 if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \ 1150 __ratelimit(&_rs)) \ 1151 __dynamic_dev_dbg(&descriptor, dev, fmt, \ 1152 ##__VA_ARGS__); \ 1153 } while (0) 1154 #elif defined(DEBUG) 1155 #define dev_dbg_ratelimited(dev, fmt, ...) \ 1156 do { \ 1157 static DEFINE_RATELIMIT_STATE(_rs, \ 1158 DEFAULT_RATELIMIT_INTERVAL, \ 1159 DEFAULT_RATELIMIT_BURST); \ 1160 if (__ratelimit(&_rs)) \ 1161 dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); \ 1162 } while (0) 1163 #else 1164 #define dev_dbg_ratelimited(dev, fmt, ...) \ 1165 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 1166 #endif 1167 1168 #ifdef VERBOSE_DEBUG 1169 #define dev_vdbg dev_dbg 1170 #else 1171 #define dev_vdbg(dev, format, arg...) \ 1172 ({ \ 1173 if (0) \ 1174 dev_printk(KERN_DEBUG, dev, format, ##arg); \ 1175 0; \ 1176 }) 1177 #endif 1178 1179 /* 1180 * dev_WARN*() acts like dev_printk(), but with the key difference of 1181 * using WARN/WARN_ONCE to include file/line information and a backtrace. 1182 */ 1183 #define dev_WARN(dev, format, arg...) \ 1184 WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg); 1185 1186 #define dev_WARN_ONCE(dev, condition, format, arg...) \ 1187 WARN_ONCE(condition, "%s %s: " format, \ 1188 dev_driver_string(dev), dev_name(dev), ## arg) 1189 1190 /* Create alias, so I can be autoloaded. */ 1191 #define MODULE_ALIAS_CHARDEV(major,minor) \ 1192 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor)) 1193 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \ 1194 MODULE_ALIAS("char-major-" __stringify(major) "-*") 1195 1196 #ifdef CONFIG_SYSFS_DEPRECATED 1197 extern long sysfs_deprecated; 1198 #else 1199 #define sysfs_deprecated 0 1200 #endif 1201 1202 /** 1203 * module_driver() - Helper macro for drivers that don't do anything 1204 * special in module init/exit. This eliminates a lot of boilerplate. 1205 * Each module may only use this macro once, and calling it replaces 1206 * module_init() and module_exit(). 1207 * 1208 * @__driver: driver name 1209 * @__register: register function for this driver type 1210 * @__unregister: unregister function for this driver type 1211 * @...: Additional arguments to be passed to __register and __unregister. 1212 * 1213 * Use this macro to construct bus specific macros for registering 1214 * drivers, and do not use it on its own. 1215 */ 1216 #define module_driver(__driver, __register, __unregister, ...) \ 1217 static int __init __driver##_init(void) \ 1218 { \ 1219 return __register(&(__driver) , ##__VA_ARGS__); \ 1220 } \ 1221 module_init(__driver##_init); \ 1222 static void __exit __driver##_exit(void) \ 1223 { \ 1224 __unregister(&(__driver) , ##__VA_ARGS__); \ 1225 } \ 1226 module_exit(__driver##_exit); 1227 1228 #endif /* _DEVICE_H_ */
1 #ifndef _LINUX_LIST_H 2 #define _LINUX_LIST_H 3 4 #include <linux/types.h> 5 #include <linux/stddef.h> 6 #include <linux/poison.h> 7 #include <linux/const.h> 8 9 /* 10 * Simple doubly linked list implementation. 11 * 12 * Some of the internal functions ("__xxx") are useful when 13 * manipulating whole lists rather than single entries, as 14 * sometimes we already know the next/prev entries and we can 15 * generate better code by using them directly rather than 16 * using the generic single-entry routines. 17 */ 18 19 #define LIST_HEAD_INIT(name) { &(name), &(name) } 20 21 #define LIST_HEAD(name) \ 22 struct list_head name = LIST_HEAD_INIT(name) 23 24 static inline void INIT_LIST_HEAD(struct list_head *list) 25 { 26 list->next = list; 27 list->prev = list; 28 } 29 30 /* 31 * Insert a new entry between two known consecutive entries. 32 * 33 * This is only for internal list manipulation where we know 34 * the prev/next entries already! 35 */ 36 #ifndef CONFIG_DEBUG_LIST 37 static inline void __list_add(struct list_head *new, 38 struct list_head *prev, 39 struct list_head *next) 40 { 41 next->prev = new; 42 new->next = next; 43 new->prev = prev; 44 prev->next = new; 45 } 46 #else 47 extern void __list_add(struct list_head *new, 48 struct list_head *prev, 49 struct list_head *next); 50 #endif 51 52 /** 53 * list_add - add a new entry 54 * @new: new entry to be added 55 * @head: list head to add it after 56 * 57 * Insert a new entry after the specified head. 58 * This is good for implementing stacks. 59 */ 60 static inline void list_add(struct list_head *new, struct list_head *head) 61 { 62 __list_add(new, head, head->next); 63 } 64 65 66 /** 67 * list_add_tail - add a new entry 68 * @new: new entry to be added 69 * @head: list head to add it before 70 * 71 * Insert a new entry before the specified head. 72 * This is useful for implementing queues. 73 */ 74 static inline void list_add_tail(struct list_head *new, struct list_head *head) 75 { 76 __list_add(new, head->prev, head); 77 } 78 79 /* 80 * Delete a list entry by making the prev/next entries 81 * point to each other. 82 * 83 * This is only for internal list manipulation where we know 84 * the prev/next entries already! 85 */ 86 static inline void __list_del(struct list_head * prev, struct list_head * next) 87 { 88 next->prev = prev; 89 prev->next = next; 90 } 91 92 /** 93 * list_del - deletes entry from list. 94 * @entry: the element to delete from the list. 95 * Note: list_empty() on entry does not return true after this, the entry is 96 * in an undefined state. 97 */ 98 #ifndef CONFIG_DEBUG_LIST 99 static inline void __list_del_entry(struct list_head *entry) 100 { 101 __list_del(entry->prev, entry->next); 102 } 103 104 static inline void list_del(struct list_head *entry) 105 { 106 __list_del(entry->prev, entry->next); 107 entry->next = LIST_POISON1; 108 entry->prev = LIST_POISON2; 109 } 110 #else 111 extern void __list_del_entry(struct list_head *entry); 112 extern void list_del(struct list_head *entry); 113 #endif 114 115 /** 116 * list_replace - replace old entry by new one 117 * @old : the element to be replaced 118 * @new : the new element to insert 119 * 120 * If @old was empty, it will be overwritten. 121 */ 122 static inline void list_replace(struct list_head *old, 123 struct list_head *new) 124 { 125 new->next = old->next; 126 new->next->prev = new; 127 new->prev = old->prev; 128 new->prev->next = new; 129 } 130 131 static inline void list_replace_init(struct list_head *old, 132 struct list_head *new) 133 { 134 list_replace(old, new); 135 INIT_LIST_HEAD(old); 136 } 137 138 /** 139 * list_del_init - deletes entry from list and reinitialize it. 140 * @entry: the element to delete from the list. 141 */ 142 static inline void list_del_init(struct list_head *entry) 143 { 144 __list_del_entry(entry); 145 INIT_LIST_HEAD(entry); 146 } 147 148 /** 149 * list_move - delete from one list and add as another's head 150 * @list: the entry to move 151 * @head: the head that will precede our entry 152 */ 153 static inline void list_move(struct list_head *list, struct list_head *head) 154 { 155 __list_del_entry(list); 156 list_add(list, head); 157 } 158 159 /** 160 * list_move_tail - delete from one list and add as another's tail 161 * @list: the entry to move 162 * @head: the head that will follow our entry 163 */ 164 static inline void list_move_tail(struct list_head *list, 165 struct list_head *head) 166 { 167 __list_del_entry(list); 168 list_add_tail(list, head); 169 } 170 171 /** 172 * list_is_last - tests whether @list is the last entry in list @head 173 * @list: the entry to test 174 * @head: the head of the list 175 */ 176 static inline int list_is_last(const struct list_head *list, 177 const struct list_head *head) 178 { 179 return list->next == head; 180 } 181 182 /** 183 * list_empty - tests whether a list is empty 184 * @head: the list to test. 185 */ 186 static inline int list_empty(const struct list_head *head) 187 { 188 return head->next == head; 189 } 190 191 /** 192 * list_empty_careful - tests whether a list is empty and not being modified 193 * @head: the list to test 194 * 195 * Description: 196 * tests whether a list is empty _and_ checks that no other CPU might be 197 * in the process of modifying either member (next or prev) 198 * 199 * NOTE: using list_empty_careful() without synchronization 200 * can only be safe if the only activity that can happen 201 * to the list entry is list_del_init(). Eg. it cannot be used 202 * if another CPU could re-list_add() it. 203 */ 204 static inline int list_empty_careful(const struct list_head *head) 205 { 206 struct list_head *next = head->next; 207 return (next == head) && (next == head->prev); 208 } 209 210 /** 211 * list_rotate_left - rotate the list to the left 212 * @head: the head of the list 213 */ 214 static inline void list_rotate_left(struct list_head *head) 215 { 216 struct list_head *first; 217 218 if (!list_empty(head)) { 219 first = head->next; 220 list_move_tail(first, head); 221 } 222 } 223 224 /** 225 * list_is_singular - tests whether a list has just one entry. 226 * @head: the list to test. 227 */ 228 static inline int list_is_singular(const struct list_head *head) 229 { 230 return !list_empty(head) && (head->next == head->prev); 231 } 232 233 static inline void __list_cut_position(struct list_head *list, 234 struct list_head *head, struct list_head *entry) 235 { 236 struct list_head *new_first = entry->next; 237 list->next = head->next; 238 list->next->prev = list; 239 list->prev = entry; 240 entry->next = list; 241 head->next = new_first; 242 new_first->prev = head; 243 } 244 245 /** 246 * list_cut_position - cut a list into two 247 * @list: a new list to add all removed entries 248 * @head: a list with entries 249 * @entry: an entry within head, could be the head itself 250 * and if so we won't cut the list 251 * 252 * This helper moves the initial part of @head, up to and 253 * including @entry, from @head to @list. You should 254 * pass on @entry an element you know is on @head. @list 255 * should be an empty list or a list you do not care about 256 * losing its data. 257 * 258 */ 259 static inline void list_cut_position(struct list_head *list, 260 struct list_head *head, struct list_head *entry) 261 { 262 if (list_empty(head)) 263 return; 264 if (list_is_singular(head) && 265 (head->next != entry && head != entry)) 266 return; 267 if (entry == head) 268 INIT_LIST_HEAD(list); 269 else 270 __list_cut_position(list, head, entry); 271 } 272 273 static inline void __list_splice(const struct list_head *list, 274 struct list_head *prev, 275 struct list_head *next) 276 { 277 struct list_head *first = list->next; 278 struct list_head *last = list->prev; 279 280 first->prev = prev; 281 prev->next = first; 282 283 last->next = next; 284 next->prev = last; 285 } 286 287 /** 288 * list_splice - join two lists, this is designed for stacks 289 * @list: the new list to add. 290 * @head: the place to add it in the first list. 291 */ 292 static inline void list_splice(const struct list_head *list, 293 struct list_head *head) 294 { 295 if (!list_empty(list)) 296 __list_splice(list, head, head->next); 297 } 298 299 /** 300 * list_splice_tail - join two lists, each list being a queue 301 * @list: the new list to add. 302 * @head: the place to add it in the first list. 303 */ 304 static inline void list_splice_tail(struct list_head *list, 305 struct list_head *head) 306 { 307 if (!list_empty(list)) 308 __list_splice(list, head->prev, head); 309 } 310 311 /** 312 * list_splice_init - join two lists and reinitialise the emptied list. 313 * @list: the new list to add. 314 * @head: the place to add it in the first list. 315 * 316 * The list at @list is reinitialised 317 */ 318 static inline void list_splice_init(struct list_head *list, 319 struct list_head *head) 320 { 321 if (!list_empty(list)) { 322 __list_splice(list, head, head->next); 323 INIT_LIST_HEAD(list); 324 } 325 } 326 327 /** 328 * list_splice_tail_init - join two lists and reinitialise the emptied list 329 * @list: the new list to add. 330 * @head: the place to add it in the first list. 331 * 332 * Each of the lists is a queue. 333 * The list at @list is reinitialised 334 */ 335 static inline void list_splice_tail_init(struct list_head *list, 336 struct list_head *head) 337 { 338 if (!list_empty(list)) { 339 __list_splice(list, head->prev, head); 340 INIT_LIST_HEAD(list); 341 } 342 } 343 344 /** 345 * list_entry - get the struct for this entry 346 * @ptr: the &struct list_head pointer. 347 * @type: the type of the struct this is embedded in. 348 * @member: the name of the list_struct within the struct. 349 */ 350 #define list_entry(ptr, type, member) \ 351 container_of(ptr, type, member) 352 353 /** 354 * list_first_entry - get the first element from a list 355 * @ptr: the list head to take the element from. 356 * @type: the type of the struct this is embedded in. 357 * @member: the name of the list_struct within the struct. 358 * 359 * Note, that list is expected to be not empty. 360 */ 361 #define list_first_entry(ptr, type, member) \ 362 list_entry((ptr)->next, type, member) 363 364 /** 365 * list_last_entry - get the last element from a list 366 * @ptr: the list head to take the element from. 367 * @type: the type of the struct this is embedded in. 368 * @member: the name of the list_struct within the struct. 369 * 370 * Note, that list is expected to be not empty. 371 */ 372 #define list_last_entry(ptr, type, member) \ 373 list_entry((ptr)->prev, type, member) 374 375 /** 376 * list_first_entry_or_null - get the first element from a list 377 * @ptr: the list head to take the element from. 378 * @type: the type of the struct this is embedded in. 379 * @member: the name of the list_struct within the struct. 380 * 381 * Note that if the list is empty, it returns NULL. 382 */ 383 #define list_first_entry_or_null(ptr, type, member) \ 384 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) 385 386 /** 387 * list_next_entry - get the next element in list 388 * @pos: the type * to cursor 389 * @member: the name of the list_struct within the struct. 390 */ 391 #define list_next_entry(pos, member) \ 392 list_entry((pos)->member.next, typeof(*(pos)), member) 393 394 /** 395 * list_prev_entry - get the prev element in list 396 * @pos: the type * to cursor 397 * @member: the name of the list_struct within the struct. 398 */ 399 #define list_prev_entry(pos, member) \ 400 list_entry((pos)->member.prev, typeof(*(pos)), member) 401 402 /** 403 * list_for_each - iterate over a list 404 * @pos: the &struct list_head to use as a loop cursor. 405 * @head: the head for your list. 406 */ 407 #define list_for_each(pos, head) \ 408 for (pos = (head)->next; pos != (head); pos = pos->next) 409 410 /** 411 * list_for_each_prev - iterate over a list backwards 412 * @pos: the &struct list_head to use as a loop cursor. 413 * @head: the head for your list. 414 */ 415 #define list_for_each_prev(pos, head) \ 416 for (pos = (head)->prev; pos != (head); pos = pos->prev) 417 418 /** 419 * list_for_each_safe - iterate over a list safe against removal of list entry 420 * @pos: the &struct list_head to use as a loop cursor. 421 * @n: another &struct list_head to use as temporary storage 422 * @head: the head for your list. 423 */ 424 #define list_for_each_safe(pos, n, head) \ 425 for (pos = (head)->next, n = pos->next; pos != (head); \ 426 pos = n, n = pos->next) 427 428 /** 429 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry 430 * @pos: the &struct list_head to use as a loop cursor. 431 * @n: another &struct list_head to use as temporary storage 432 * @head: the head for your list. 433 */ 434 #define list_for_each_prev_safe(pos, n, head) \ 435 for (pos = (head)->prev, n = pos->prev; \ 436 pos != (head); \ 437 pos = n, n = pos->prev) 438 439 /** 440 * list_for_each_entry - iterate over list of given type 441 * @pos: the type * to use as a loop cursor. 442 * @head: the head for your list. 443 * @member: the name of the list_struct within the struct. 444 */ 445 #define list_for_each_entry(pos, head, member) \ 446 for (pos = list_first_entry(head, typeof(*pos), member); \ 447 &pos->member != (head); \ 448 pos = list_next_entry(pos, member)) 449 450 /** 451 * list_for_each_entry_reverse - iterate backwards over list of given type. 452 * @pos: the type * to use as a loop cursor. 453 * @head: the head for your list. 454 * @member: the name of the list_struct within the struct. 455 */ 456 #define list_for_each_entry_reverse(pos, head, member) \ 457 for (pos = list_last_entry(head, typeof(*pos), member); \ 458 &pos->member != (head); \ 459 pos = list_prev_entry(pos, member)) 460 461 /** 462 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() 463 * @pos: the type * to use as a start point 464 * @head: the head of the list 465 * @member: the name of the list_struct within the struct. 466 * 467 * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). 468 */ 469 #define list_prepare_entry(pos, head, member) \ 470 ((pos) ? : list_entry(head, typeof(*pos), member)) 471 472 /** 473 * list_for_each_entry_continue - continue iteration over list of given type 474 * @pos: the type * to use as a loop cursor. 475 * @head: the head for your list. 476 * @member: the name of the list_struct within the struct. 477 * 478 * Continue to iterate over list of given type, continuing after 479 * the current position. 480 */ 481 #define list_for_each_entry_continue(pos, head, member) \ 482 for (pos = list_next_entry(pos, member); \ 483 &pos->member != (head); \ 484 pos = list_next_entry(pos, member)) 485 486 /** 487 * list_for_each_entry_continue_reverse - iterate backwards from the given point 488 * @pos: the type * to use as a loop cursor. 489 * @head: the head for your list. 490 * @member: the name of the list_struct within the struct. 491 * 492 * Start to iterate over list of given type backwards, continuing after 493 * the current position. 494 */ 495 #define list_for_each_entry_continue_reverse(pos, head, member) \ 496 for (pos = list_prev_entry(pos, member); \ 497 &pos->member != (head); \ 498 pos = list_prev_entry(pos, member)) 499 500 /** 501 * list_for_each_entry_from - iterate over list of given type from the current point 502 * @pos: the type * to use as a loop cursor. 503 * @head: the head for your list. 504 * @member: the name of the list_struct within the struct. 505 * 506 * Iterate over list of given type, continuing from current position. 507 */ 508 #define list_for_each_entry_from(pos, head, member) \ 509 for (; &pos->member != (head); \ 510 pos = list_next_entry(pos, member)) 511 512 /** 513 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 514 * @pos: the type * to use as a loop cursor. 515 * @n: another type * to use as temporary storage 516 * @head: the head for your list. 517 * @member: the name of the list_struct within the struct. 518 */ 519 #define list_for_each_entry_safe(pos, n, head, member) \ 520 for (pos = list_first_entry(head, typeof(*pos), member), \ 521 n = list_next_entry(pos, member); \ 522 &pos->member != (head); \ 523 pos = n, n = list_next_entry(n, member)) 524 525 /** 526 * list_for_each_entry_safe_continue - continue list iteration safe against removal 527 * @pos: the type * to use as a loop cursor. 528 * @n: another type * to use as temporary storage 529 * @head: the head for your list. 530 * @member: the name of the list_struct within the struct. 531 * 532 * Iterate over list of given type, continuing after current point, 533 * safe against removal of list entry. 534 */ 535 #define list_for_each_entry_safe_continue(pos, n, head, member) \ 536 for (pos = list_next_entry(pos, member), \ 537 n = list_next_entry(pos, member); \ 538 &pos->member != (head); \ 539 pos = n, n = list_next_entry(n, member)) 540 541 /** 542 * list_for_each_entry_safe_from - iterate over list from current point safe against removal 543 * @pos: the type * to use as a loop cursor. 544 * @n: another type * to use as temporary storage 545 * @head: the head for your list. 546 * @member: the name of the list_struct within the struct. 547 * 548 * Iterate over list of given type from current point, safe against 549 * removal of list entry. 550 */ 551 #define list_for_each_entry_safe_from(pos, n, head, member) \ 552 for (n = list_next_entry(pos, member); \ 553 &pos->member != (head); \ 554 pos = n, n = list_next_entry(n, member)) 555 556 /** 557 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal 558 * @pos: the type * to use as a loop cursor. 559 * @n: another type * to use as temporary storage 560 * @head: the head for your list. 561 * @member: the name of the list_struct within the struct. 562 * 563 * Iterate backwards over list of given type, safe against removal 564 * of list entry. 565 */ 566 #define list_for_each_entry_safe_reverse(pos, n, head, member) \ 567 for (pos = list_last_entry(head, typeof(*pos), member), \ 568 n = list_prev_entry(pos, member); \ 569 &pos->member != (head); \ 570 pos = n, n = list_prev_entry(n, member)) 571 572 /** 573 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop 574 * @pos: the loop cursor used in the list_for_each_entry_safe loop 575 * @n: temporary storage used in list_for_each_entry_safe 576 * @member: the name of the list_struct within the struct. 577 * 578 * list_safe_reset_next is not safe to use in general if the list may be 579 * modified concurrently (eg. the lock is dropped in the loop body). An 580 * exception to this is if the cursor element (pos) is pinned in the list, 581 * and list_safe_reset_next is called after re-taking the lock and before 582 * completing the current iteration of the loop body. 583 */ 584 #define list_safe_reset_next(pos, n, member) \ 585 n = list_next_entry(pos, member) 586 587 /* 588 * Double linked lists with a single pointer list head. 589 * Mostly useful for hash tables where the two pointer list head is 590 * too wasteful. 591 * You lose the ability to access the tail in O(1). 592 */ 593 594 #define HLIST_HEAD_INIT { .first = NULL } 595 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } 596 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) 597 static inline void INIT_HLIST_NODE(struct hlist_node *h) 598 { 599 h->next = NULL; 600 h->pprev = NULL; 601 } 602 603 static inline int hlist_unhashed(const struct hlist_node *h) 604 { 605 return !h->pprev; 606 } 607 608 static inline int hlist_empty(const struct hlist_head *h) 609 { 610 return !h->first; 611 } 612 613 static inline void __hlist_del(struct hlist_node *n) 614 { 615 struct hlist_node *next = n->next; 616 struct hlist_node **pprev = n->pprev; 617 *pprev = next; 618 if (next) 619 next->pprev = pprev; 620 } 621 622 static inline void hlist_del(struct hlist_node *n) 623 { 624 __hlist_del(n); 625 n->next = LIST_POISON1; 626 n->pprev = LIST_POISON2; 627 } 628 629 static inline void hlist_del_init(struct hlist_node *n) 630 { 631 if (!hlist_unhashed(n)) { 632 __hlist_del(n); 633 INIT_HLIST_NODE(n); 634 } 635 } 636 637 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) 638 { 639 struct hlist_node *first = h->first; 640 n->next = first; 641 if (first) 642 first->pprev = &n->next; 643 h->first = n; 644 n->pprev = &h->first; 645 } 646 647 /* next must be != NULL */ 648 static inline void hlist_add_before(struct hlist_node *n, 649 struct hlist_node *next) 650 { 651 n->pprev = next->pprev; 652 n->next = next; 653 next->pprev = &n->next; 654 *(n->pprev) = n; 655 } 656 657 static inline void hlist_add_after(struct hlist_node *n, 658 struct hlist_node *next) 659 { 660 next->next = n->next; 661 n->next = next; 662 next->pprev = &n->next; 663 664 if(next->next) 665 next->next->pprev = &next->next; 666 } 667 668 /* after that we'll appear to be on some hlist and hlist_del will work */ 669 static inline void hlist_add_fake(struct hlist_node *n) 670 { 671 n->pprev = &n->next; 672 } 673 674 /* 675 * Move a list from one list head to another. Fixup the pprev 676 * reference of the first entry if it exists. 677 */ 678 static inline void hlist_move_list(struct hlist_head *old, 679 struct hlist_head *new) 680 { 681 new->first = old->first; 682 if (new->first) 683 new->first->pprev = &new->first; 684 old->first = NULL; 685 } 686 687 #define hlist_entry(ptr, type, member) container_of(ptr,type,member) 688 689 #define hlist_for_each(pos, head) \ 690 for (pos = (head)->first; pos ; pos = pos->next) 691 692 #define hlist_for_each_safe(pos, n, head) \ 693 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ 694 pos = n) 695 696 #define hlist_entry_safe(ptr, type, member) \ 697 ({ typeof(ptr) ____ptr = (ptr); \ 698 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \ 699 }) 700 701 /** 702 * hlist_for_each_entry - iterate over list of given type 703 * @pos: the type * to use as a loop cursor. 704 * @head: the head for your list. 705 * @member: the name of the hlist_node within the struct. 706 */ 707 #define hlist_for_each_entry(pos, head, member) \ 708 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 709 pos; \ 710 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 711 712 /** 713 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point 714 * @pos: the type * to use as a loop cursor. 715 * @member: the name of the hlist_node within the struct. 716 */ 717 #define hlist_for_each_entry_continue(pos, member) \ 718 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\ 719 pos; \ 720 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 721 722 /** 723 * hlist_for_each_entry_from - iterate over a hlist continuing from current point 724 * @pos: the type * to use as a loop cursor. 725 * @member: the name of the hlist_node within the struct. 726 */ 727 #define hlist_for_each_entry_from(pos, member) \ 728 for (; pos; \ 729 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 730 731 /** 732 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry 733 * @pos: the type * to use as a loop cursor. 734 * @n: another &struct hlist_node to use as temporary storage 735 * @head: the head for your list. 736 * @member: the name of the hlist_node within the struct. 737 */ 738 #define hlist_for_each_entry_safe(pos, n, head, member) \ 739 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\ 740 pos && ({ n = pos->member.next; 1; }); \ 741 pos = hlist_entry_safe(n, typeof(*pos), member)) 742 743 #endif
1 /* 2 * Written by Mark Hemment, 1996 (markhe@nextd.demon.co.uk). 3 * 4 * (C) SGI 2006, Christoph Lameter 5 * Cleaned up and restructured to ease the addition of alternative 6 * implementations of SLAB allocators. 7 * (C) Linux Foundation 2008-2013 8 * Unified interface for all slab allocators 9 */ 10 11 #ifndef _LINUX_SLAB_H 12 #define _LINUX_SLAB_H 13 14 #include <linux/gfp.h> 15 #include <linux/types.h> 16 #include <linux/workqueue.h> 17 18 19 /* 20 * Flags to pass to kmem_cache_create(). 21 * The ones marked DEBUG are only valid if CONFIG_SLAB_DEBUG is set. 22 */ 23 #define SLAB_DEBUG_FREE 0x00000100UL /* DEBUG: Perform (expensive) checks on free */ 24 #define SLAB_RED_ZONE 0x00000400UL /* DEBUG: Red zone objs in a cache */ 25 #define SLAB_POISON 0x00000800UL /* DEBUG: Poison objects */ 26 #define SLAB_HWCACHE_ALIGN 0x00002000UL /* Align objs on cache lines */ 27 #define SLAB_CACHE_DMA 0x00004000UL /* Use GFP_DMA memory */ 28 #define SLAB_STORE_USER 0x00010000UL /* DEBUG: Store the last owner for bug hunting */ 29 #define SLAB_PANIC 0x00040000UL /* Panic if kmem_cache_create() fails */ 30 /* 31 * SLAB_DESTROY_BY_RCU - **WARNING** READ THIS! 32 * 33 * This delays freeing the SLAB page by a grace period, it does _NOT_ 34 * delay object freeing. This means that if you do kmem_cache_free() 35 * that memory location is free to be reused at any time. Thus it may 36 * be possible to see another object there in the same RCU grace period. 37 * 38 * This feature only ensures the memory location backing the object 39 * stays valid, the trick to using this is relying on an independent 40 * object validation pass. Something like: 41 * 42 * rcu_read_lock() 43 * again: 44 * obj = lockless_lookup(key); 45 * if (obj) { 46 * if (!try_get_ref(obj)) // might fail for free objects 47 * goto again; 48 * 49 * if (obj->key != key) { // not the object we expected 50 * put_ref(obj); 51 * goto again; 52 * } 53 * } 54 * rcu_read_unlock(); 55 * 56 * This is useful if we need to approach a kernel structure obliquely, 57 * from its address obtained without the usual locking. We can lock 58 * the structure to stabilize it and check it's still at the given address, 59 * only if we can be sure that the memory has not been meanwhile reused 60 * for some other kind of object (which our subsystem's lock might corrupt). 61 * 62 * rcu_read_lock before reading the address, then rcu_read_unlock after 63 * taking the spinlock within the structure expected at that address. 64 */ 65 #define SLAB_DESTROY_BY_RCU 0x00080000UL /* Defer freeing slabs to RCU */ 66 #define SLAB_MEM_SPREAD 0x00100000UL /* Spread some memory over cpuset */ 67 #define SLAB_TRACE 0x00200000UL /* Trace allocations and frees */ 68 69 /* Flag to prevent checks on free */ 70 #ifdef CONFIG_DEBUG_OBJECTS 71 # define SLAB_DEBUG_OBJECTS 0x00400000UL 72 #else 73 # define SLAB_DEBUG_OBJECTS 0x00000000UL 74 #endif 75 76 #define SLAB_NOLEAKTRACE 0x00800000UL /* Avoid kmemleak tracing */ 77 78 /* Don't track use of uninitialized memory */ 79 #ifdef CONFIG_KMEMCHECK 80 # define SLAB_NOTRACK 0x01000000UL 81 #else 82 # define SLAB_NOTRACK 0x00000000UL 83 #endif 84 #ifdef CONFIG_FAILSLAB 85 # define SLAB_FAILSLAB 0x02000000UL /* Fault injection mark */ 86 #else 87 # define SLAB_FAILSLAB 0x00000000UL 88 #endif 89 90 /* The following flags affect the page allocator grouping pages by mobility */ 91 #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */ 92 #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT /* Objects are short-lived */ 93 /* 94 * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. 95 * 96 * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault. 97 * 98 * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can. 99 * Both make kfree a no-op. 100 */ 101 #define ZERO_SIZE_PTR ((void *)16) 102 103 #define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \ 104 (unsigned long)ZERO_SIZE_PTR) 105 106 #include <linux/kmemleak.h> 107 108 struct mem_cgroup; 109 /* 110 * struct kmem_cache related prototypes 111 */ 112 void __init kmem_cache_init(void); 113 int slab_is_available(void); 114 115 struct kmem_cache *kmem_cache_create(const char *, size_t, size_t, 116 unsigned long, 117 void (*)(void *)); 118 #ifdef CONFIG_MEMCG_KMEM 119 struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *, 120 struct kmem_cache *, 121 const char *); 122 #endif 123 void kmem_cache_destroy(struct kmem_cache *); 124 int kmem_cache_shrink(struct kmem_cache *); 125 void kmem_cache_free(struct kmem_cache *, void *); 126 127 /* 128 * Please use this macro to create slab caches. Simply specify the 129 * name of the structure and maybe some flags that are listed above. 130 * 131 * The alignment of the struct determines object alignment. If you 132 * f.e. add ____cacheline_aligned_in_smp to the struct declaration 133 * then the objects will be properly aligned in SMP configurations. 134 */ 135 #define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\ 136 sizeof(struct __struct), __alignof__(struct __struct),\ 137 (__flags), NULL) 138 139 /* 140 * Common kmalloc functions provided by all allocators 141 */ 142 void * __must_check __krealloc(const void *, size_t, gfp_t); 143 void * __must_check krealloc(const void *, size_t, gfp_t); 144 void kfree(const void *); 145 void kzfree(const void *); 146 size_t ksize(const void *); 147 148 /* 149 * Some archs want to perform DMA into kmalloc caches and need a guaranteed 150 * alignment larger than the alignment of a 64-bit integer. 151 * Setting ARCH_KMALLOC_MINALIGN in arch headers allows that. 152 */ 153 #if defined(ARCH_DMA_MINALIGN) && ARCH_DMA_MINALIGN > 8 154 #define ARCH_KMALLOC_MINALIGN ARCH_DMA_MINALIGN 155 #define KMALLOC_MIN_SIZE ARCH_DMA_MINALIGN 156 #define KMALLOC_SHIFT_LOW ilog2(ARCH_DMA_MINALIGN) 157 #else 158 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long) 159 #endif 160 161 #ifdef CONFIG_SLOB 162 /* 163 * Common fields provided in kmem_cache by all slab allocators 164 * This struct is either used directly by the allocator (SLOB) 165 * or the allocator must include definitions for all fields 166 * provided in kmem_cache_common in their definition of kmem_cache. 167 * 168 * Once we can do anonymous structs (C11 standard) we could put a 169 * anonymous struct definition in these allocators so that the 170 * separate allocations in the kmem_cache structure of SLAB and 171 * SLUB is no longer needed. 172 */ 173 struct kmem_cache { 174 unsigned int object_size;/* The original size of the object */ 175 unsigned int size; /* The aligned/padded/added on size */ 176 unsigned int align; /* Alignment as calculated */ 177 unsigned long flags; /* Active flags on the slab */ 178 const char *name; /* Slab name for sysfs */ 179 int refcount; /* Use counter */ 180 void (*ctor)(void *); /* Called on object slot creation */ 181 struct list_head list; /* List of all slab caches on the system */ 182 }; 183 184 #endif /* CONFIG_SLOB */ 185 186 /* 187 * Kmalloc array related definitions 188 */ 189 190 #ifdef CONFIG_SLAB 191 /* 192 * The largest kmalloc size supported by the SLAB allocators is 193 * 32 megabyte (2^25) or the maximum allocatable page order if that is 194 * less than 32 MB. 195 * 196 * WARNING: Its not easy to increase this value since the allocators have 197 * to do various tricks to work around compiler limitations in order to 198 * ensure proper constant folding. 199 */ 200 #define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \ 201 (MAX_ORDER + PAGE_SHIFT - 1) : 25) 202 #define KMALLOC_SHIFT_MAX KMALLOC_SHIFT_HIGH 203 #ifndef KMALLOC_SHIFT_LOW 204 #define KMALLOC_SHIFT_LOW 5 205 #endif 206 #endif 207 208 #ifdef CONFIG_SLUB 209 /* 210 * SLUB directly allocates requests fitting in to an order-1 page 211 * (PAGE_SIZE*2). Larger requests are passed to the page allocator. 212 */ 213 #define KMALLOC_SHIFT_HIGH (PAGE_SHIFT + 1) 214 #define KMALLOC_SHIFT_MAX (MAX_ORDER + PAGE_SHIFT) 215 #ifndef KMALLOC_SHIFT_LOW 216 #define KMALLOC_SHIFT_LOW 3 217 #endif 218 #endif 219 220 #ifdef CONFIG_SLOB 221 /* 222 * SLOB passes all requests larger than one page to the page allocator. 223 * No kmalloc array is necessary since objects of different sizes can 224 * be allocated from the same page. 225 */ 226 #define KMALLOC_SHIFT_HIGH PAGE_SHIFT 227 #define KMALLOC_SHIFT_MAX 30 228 #ifndef KMALLOC_SHIFT_LOW 229 #define KMALLOC_SHIFT_LOW 3 230 #endif 231 #endif 232 233 /* Maximum allocatable size */ 234 #define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_MAX) 235 /* Maximum size for which we actually use a slab cache */ 236 #define KMALLOC_MAX_CACHE_SIZE (1UL << KMALLOC_SHIFT_HIGH) 237 /* Maximum order allocatable via the slab allocagtor */ 238 #define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_MAX - PAGE_SHIFT) 239 240 /* 241 * Kmalloc subsystem. 242 */ 243 #ifndef KMALLOC_MIN_SIZE 244 #define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW) 245 #endif 246 247 /* 248 * This restriction comes from byte sized index implementation. 249 * Page size is normally 2^12 bytes and, in this case, if we want to use 250 * byte sized index which can represent 2^8 entries, the size of the object 251 * should be equal or greater to 2^12 / 2^8 = 2^4 = 16. 252 * If minimum size of kmalloc is less than 16, we use it as minimum object 253 * size and give up to use byte sized index. 254 */ 255 #define SLAB_OBJ_MIN_SIZE (KMALLOC_MIN_SIZE < 16 ? \ 256 (KMALLOC_MIN_SIZE) : 16) 257 258 #ifndef CONFIG_SLOB 259 extern struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1]; 260 #ifdef CONFIG_ZONE_DMA 261 extern struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1]; 262 #endif 263 264 /* 265 * Figure out which kmalloc slab an allocation of a certain size 266 * belongs to. 267 * 0 = zero alloc 268 * 1 = 65 .. 96 bytes 269 * 2 = 120 .. 192 bytes 270 * n = 2^(n-1) .. 2^n -1 271 */ 272 static __always_inline int kmalloc_index(size_t size) 273 { 274 if (!size) 275 return 0; 276 277 if (size <= KMALLOC_MIN_SIZE) 278 return KMALLOC_SHIFT_LOW; 279 280 if (KMALLOC_MIN_SIZE <= 32 && size > 64 && size <= 96) 281 return 1; 282 if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192) 283 return 2; 284 if (size <= 8) return 3; 285 if (size <= 16) return 4; 286 if (size <= 32) return 5; 287 if (size <= 64) return 6; 288 if (size <= 128) return 7; 289 if (size <= 256) return 8; 290 if (size <= 512) return 9; 291 if (size <= 1024) return 10; 292 if (size <= 2 * 1024) return 11; 293 if (size <= 4 * 1024) return 12; 294 if (size <= 8 * 1024) return 13; 295 if (size <= 16 * 1024) return 14; 296 if (size <= 32 * 1024) return 15; 297 if (size <= 64 * 1024) return 16; 298 if (size <= 128 * 1024) return 17; 299 if (size <= 256 * 1024) return 18; 300 if (size <= 512 * 1024) return 19; 301 if (size <= 1024 * 1024) return 20; 302 if (size <= 2 * 1024 * 1024) return 21; 303 if (size <= 4 * 1024 * 1024) return 22; 304 if (size <= 8 * 1024 * 1024) return 23; 305 if (size <= 16 * 1024 * 1024) return 24; 306 if (size <= 32 * 1024 * 1024) return 25; 307 if (size <= 64 * 1024 * 1024) return 26; 308 BUG(); 309 310 /* Will never be reached. Needed because the compiler may complain */ 311 return -1; 312 } 313 #endif /* !CONFIG_SLOB */ 314 315 void *__kmalloc(size_t size, gfp_t flags); 316 void *kmem_cache_alloc(struct kmem_cache *, gfp_t flags); 317 318 #ifdef CONFIG_NUMA 319 void *__kmalloc_node(size_t size, gfp_t flags, int node); 320 void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node); 321 #else 322 static __always_inline void *__kmalloc_node(size_t size, gfp_t flags, int node) 323 { 324 return __kmalloc(size, flags); 325 } 326 327 static __always_inline void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node) 328 { 329 return kmem_cache_alloc(s, flags); 330 } 331 #endif 332 333 #ifdef CONFIG_TRACING 334 extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t); 335 336 #ifdef CONFIG_NUMA 337 extern void *kmem_cache_alloc_node_trace(struct kmem_cache *s, 338 gfp_t gfpflags, 339 int node, size_t size); 340 #else 341 static __always_inline void * 342 kmem_cache_alloc_node_trace(struct kmem_cache *s, 343 gfp_t gfpflags, 344 int node, size_t size) 345 { 346 return kmem_cache_alloc_trace(s, gfpflags, size); 347 } 348 #endif /* CONFIG_NUMA */ 349 350 #else /* CONFIG_TRACING */ 351 static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s, 352 gfp_t flags, size_t size) 353 { 354 return kmem_cache_alloc(s, flags); 355 } 356 357 static __always_inline void * 358 kmem_cache_alloc_node_trace(struct kmem_cache *s, 359 gfp_t gfpflags, 360 int node, size_t size) 361 { 362 return kmem_cache_alloc_node(s, gfpflags, node); 363 } 364 #endif /* CONFIG_TRACING */ 365 366 #ifdef CONFIG_SLAB 367 #include <linux/slab_def.h> 368 #endif 369 370 #ifdef CONFIG_SLUB 371 #include <linux/slub_def.h> 372 #endif 373 374 extern void *kmalloc_order(size_t size, gfp_t flags, unsigned int order); 375 376 #ifdef CONFIG_TRACING 377 extern void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order); 378 #else 379 static __always_inline void * 380 kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order) 381 { 382 return kmalloc_order(size, flags, order); 383 } 384 #endif 385 386 static __always_inline void *kmalloc_large(size_t size, gfp_t flags) 387 { 388 unsigned int order = get_order(size); 389 return kmalloc_order_trace(size, flags, order); 390 } 391 392 /** 393 * kmalloc - allocate memory 394 * @size: how many bytes of memory are required. 395 * @flags: the type of memory to allocate. 396 * 397 * kmalloc is the normal method of allocating memory 398 * for objects smaller than page size in the kernel. 399 * 400 * The @flags argument may be one of: 401 * 402 * %GFP_USER - Allocate memory on behalf of user. May sleep. 403 * 404 * %GFP_KERNEL - Allocate normal kernel ram. May sleep. 405 * 406 * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools. 407 * For example, use this inside interrupt handlers. 408 * 409 * %GFP_HIGHUSER - Allocate pages from high memory. 410 * 411 * %GFP_NOIO - Do not do any I/O at all while trying to get memory. 412 * 413 * %GFP_NOFS - Do not make any fs calls while trying to get memory. 414 * 415 * %GFP_NOWAIT - Allocation will not sleep. 416 * 417 * %__GFP_THISNODE - Allocate node-local memory only. 418 * 419 * %GFP_DMA - Allocation suitable for DMA. 420 * Should only be used for kmalloc() caches. Otherwise, use a 421 * slab created with SLAB_DMA. 422 * 423 * Also it is possible to set different flags by OR'ing 424 * in one or more of the following additional @flags: 425 * 426 * %__GFP_COLD - Request cache-cold pages instead of 427 * trying to return cache-warm pages. 428 * 429 * %__GFP_HIGH - This allocation has high priority and may use emergency pools. 430 * 431 * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail 432 * (think twice before using). 433 * 434 * %__GFP_NORETRY - If memory is not immediately available, 435 * then give up at once. 436 * 437 * %__GFP_NOWARN - If allocation fails, don't issue any warnings. 438 * 439 * %__GFP_REPEAT - If allocation fails initially, try once more before failing. 440 * 441 * There are other flags available as well, but these are not intended 442 * for general use, and so are not documented here. For a full list of 443 * potential flags, always refer to linux/gfp.h. 444 */ 445 static __always_inline void *kmalloc(size_t size, gfp_t flags) 446 { 447 if (__builtin_constant_p(size)) { 448 if (size > KMALLOC_MAX_CACHE_SIZE) 449 return kmalloc_large(size, flags); 450 #ifndef CONFIG_SLOB 451 if (!(flags & GFP_DMA)) { 452 int index = kmalloc_index(size); 453 454 if (!index) 455 return ZERO_SIZE_PTR; 456 457 return kmem_cache_alloc_trace(kmalloc_caches[index], 458 flags, size); 459 } 460 #endif 461 } 462 return __kmalloc(size, flags); 463 } 464 465 /* 466 * Determine size used for the nth kmalloc cache. 467 * return size or 0 if a kmalloc cache for that 468 * size does not exist 469 */ 470 static __always_inline int kmalloc_size(int n) 471 { 472 #ifndef CONFIG_SLOB 473 if (n > 2) 474 return 1 << n; 475 476 if (n == 1 && KMALLOC_MIN_SIZE <= 32) 477 return 96; 478 479 if (n == 2 && KMALLOC_MIN_SIZE <= 64) 480 return 192; 481 #endif 482 return 0; 483 } 484 485 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) 486 { 487 #ifndef CONFIG_SLOB 488 if (__builtin_constant_p(size) && 489 size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) { 490 int i = kmalloc_index(size); 491 492 if (!i) 493 return ZERO_SIZE_PTR; 494 495 return kmem_cache_alloc_node_trace(kmalloc_caches[i], 496 flags, node, size); 497 } 498 #endif 499 return __kmalloc_node(size, flags, node); 500 } 501 502 /* 503 * Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment. 504 * Intended for arches that get misalignment faults even for 64 bit integer 505 * aligned buffers. 506 */ 507 #ifndef ARCH_SLAB_MINALIGN 508 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) 509 #endif 510 /* 511 * This is the main placeholder for memcg-related information in kmem caches. 512 * struct kmem_cache will hold a pointer to it, so the memory cost while 513 * disabled is 1 pointer. The runtime cost while enabled, gets bigger than it 514 * would otherwise be if that would be bundled in kmem_cache: we'll need an 515 * extra pointer chase. But the trade off clearly lays in favor of not 516 * penalizing non-users. 517 * 518 * Both the root cache and the child caches will have it. For the root cache, 519 * this will hold a dynamically allocated array large enough to hold 520 * information about the currently limited memcgs in the system. To allow the 521 * array to be accessed without taking any locks, on relocation we free the old 522 * version only after a grace period. 523 * 524 * Child caches will hold extra metadata needed for its operation. Fields are: 525 * 526 * @memcg: pointer to the memcg this cache belongs to 527 * @list: list_head for the list of all caches in this memcg 528 * @root_cache: pointer to the global, root cache, this cache was derived from 529 * @nr_pages: number of pages that belongs to this cache. 530 */ 531 struct memcg_cache_params { 532 bool is_root_cache; 533 union { 534 struct { 535 struct rcu_head rcu_head; 536 struct kmem_cache *memcg_caches[0]; 537 }; 538 struct { 539 struct mem_cgroup *memcg; 540 struct list_head list; 541 struct kmem_cache *root_cache; 542 atomic_t nr_pages; 543 }; 544 }; 545 }; 546 547 int memcg_update_all_caches(int num_memcgs); 548 549 struct seq_file; 550 int cache_show(struct kmem_cache *s, struct seq_file *m); 551 void print_slabinfo_header(struct seq_file *m); 552 553 /** 554 * kmalloc_array - allocate memory for an array. 555 * @n: number of elements. 556 * @size: element size. 557 * @flags: the type of memory to allocate (see kmalloc). 558 */ 559 static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) 560 { 561 if (size != 0 && n > SIZE_MAX / size) 562 return NULL; 563 return __kmalloc(n * size, flags); 564 } 565 566 /** 567 * kcalloc - allocate memory for an array. The memory is set to zero. 568 * @n: number of elements. 569 * @size: element size. 570 * @flags: the type of memory to allocate (see kmalloc). 571 */ 572 static inline void *kcalloc(size_t n, size_t size, gfp_t flags) 573 { 574 return kmalloc_array(n, size, flags | __GFP_ZERO); 575 } 576 577 /* 578 * kmalloc_track_caller is a special version of kmalloc that records the 579 * calling function of the routine calling it for slab leak tracking instead 580 * of just the calling function (confusing, eh?). 581 * It's useful when the call to kmalloc comes from a widely-used standard 582 * allocator where we care about the real place the memory allocation 583 * request comes from. 584 */ 585 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \ 586 (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \ 587 (defined(CONFIG_SLOB) && defined(CONFIG_TRACING)) 588 extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long); 589 #define kmalloc_track_caller(size, flags) \ 590 __kmalloc_track_caller(size, flags, _RET_IP_) 591 #else 592 #define kmalloc_track_caller(size, flags) \ 593 __kmalloc(size, flags) 594 #endif /* DEBUG_SLAB */ 595 596 #ifdef CONFIG_NUMA 597 /* 598 * kmalloc_node_track_caller is a special version of kmalloc_node that 599 * records the calling function of the routine calling it for slab leak 600 * tracking instead of just the calling function (confusing, eh?). 601 * It's useful when the call to kmalloc_node comes from a widely-used 602 * standard allocator where we care about the real place the memory 603 * allocation request comes from. 604 */ 605 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB) || \ 606 (defined(CONFIG_SLAB) && defined(CONFIG_TRACING)) || \ 607 (defined(CONFIG_SLOB) && defined(CONFIG_TRACING)) 608 extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, unsigned long); 609 #define kmalloc_node_track_caller(size, flags, node) \ 610 __kmalloc_node_track_caller(size, flags, node, \ 611 _RET_IP_) 612 #else 613 #define kmalloc_node_track_caller(size, flags, node) \ 614 __kmalloc_node(size, flags, node) 615 #endif 616 617 #else /* CONFIG_NUMA */ 618 619 #define kmalloc_node_track_caller(size, flags, node) \ 620 kmalloc_track_caller(size, flags) 621 622 #endif /* CONFIG_NUMA */ 623 624 /* 625 * Shortcuts 626 */ 627 static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags) 628 { 629 return kmem_cache_alloc(k, flags | __GFP_ZERO); 630 } 631 632 /** 633 * kzalloc - allocate memory. The memory is set to zero. 634 * @size: how many bytes of memory are required. 635 * @flags: the type of memory to allocate (see kmalloc). 636 */ 637 static inline void *kzalloc(size_t size, gfp_t flags) 638 { 639 return kmalloc(size, flags | __GFP_ZERO); 640 } 641 642 /** 643 * kzalloc_node - allocate zeroed memory from a particular memory node. 644 * @size: how many bytes of memory are required. 645 * @flags: the type of memory to allocate (see kmalloc). 646 * @node: memory node from which to allocate 647 */ 648 static inline void *kzalloc_node(size_t size, gfp_t flags, int node) 649 { 650 return kmalloc_node(size, flags | __GFP_ZERO, node); 651 } 652 653 /* 654 * Determine the size of a slab object 655 */ 656 static inline unsigned int kmem_cache_size(struct kmem_cache *s) 657 { 658 return s->object_size; 659 } 660 661 void __init kmem_cache_init_late(void); 662 663 #endif /* _LINUX_SLAB_H */
1 #ifndef __LINUX_SPINLOCK_H 2 #define __LINUX_SPINLOCK_H 3 4 /* 5 * include/linux/spinlock.h - generic spinlock/rwlock declarations 6 * 7 * here's the role of the various spinlock/rwlock related include files: 8 * 9 * on SMP builds: 10 * 11 * asm/spinlock_types.h: contains the arch_spinlock_t/arch_rwlock_t and the 12 * initializers 13 * 14 * linux/spinlock_types.h: 15 * defines the generic type and initializers 16 * 17 * asm/spinlock.h: contains the arch_spin_*()/etc. lowlevel 18 * implementations, mostly inline assembly code 19 * 20 * (also included on UP-debug builds:) 21 * 22 * linux/spinlock_api_smp.h: 23 * contains the prototypes for the _spin_*() APIs. 24 * 25 * linux/spinlock.h: builds the final spin_*() APIs. 26 * 27 * on UP builds: 28 * 29 * linux/spinlock_type_up.h: 30 * contains the generic, simplified UP spinlock type. 31 * (which is an empty structure on non-debug builds) 32 * 33 * linux/spinlock_types.h: 34 * defines the generic type and initializers 35 * 36 * linux/spinlock_up.h: 37 * contains the arch_spin_*()/etc. version of UP 38 * builds. (which are NOPs on non-debug, non-preempt 39 * builds) 40 * 41 * (included on UP-non-debug builds:) 42 * 43 * linux/spinlock_api_up.h: 44 * builds the _spin_*() APIs. 45 * 46 * linux/spinlock.h: builds the final spin_*() APIs. 47 */ 48 49 #include <linux/typecheck.h> 50 #include <linux/preempt.h> 51 #include <linux/linkage.h> 52 #include <linux/compiler.h> 53 #include <linux/irqflags.h> 54 #include <linux/thread_info.h> 55 #include <linux/kernel.h> 56 #include <linux/stringify.h> 57 #include <linux/bottom_half.h> 58 #include <asm/barrier.h> 59 60 61 /* 62 * Must define these before including other files, inline functions need them 63 */ 64 #define LOCK_SECTION_NAME ".text..lock."KBUILD_BASENAME 65 66 #define LOCK_SECTION_START(extra) \ 67 ".subsection 1\n\t" \ 68 extra \ 69 ".ifndef " LOCK_SECTION_NAME "\n\t" \ 70 LOCK_SECTION_NAME ":\n\t" \ 71 ".endif\n" 72 73 #define LOCK_SECTION_END \ 74 ".previous\n\t" 75 76 #define __lockfunc __attribute__((section(".spinlock.text"))) 77 78 /* 79 * Pull the arch_spinlock_t and arch_rwlock_t definitions: 80 */ 81 #include <linux/spinlock_types.h> 82 83 /* 84 * Pull the arch_spin*() functions/declarations (UP-nondebug doesn't need them): 85 */ 86 #ifdef CONFIG_SMP 87 # include <asm/spinlock.h> 88 #else 89 # include <linux/spinlock_up.h> 90 #endif 91 92 #ifdef CONFIG_DEBUG_SPINLOCK 93 extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name, 94 struct lock_class_key *key); 95 # define raw_spin_lock_init(lock) \ 96 do { \ 97 static struct lock_class_key __key; \ 98 \ 99 __raw_spin_lock_init((lock), #lock, &__key); \ 100 } while (0) 101 102 #else 103 # define raw_spin_lock_init(lock) \ 104 do { *(lock) = __RAW_SPIN_LOCK_UNLOCKED(lock); } while (0) 105 #endif 106 107 #define raw_spin_is_locked(lock) arch_spin_is_locked(&(lock)->raw_lock) 108 109 #ifdef CONFIG_GENERIC_LOCKBREAK 110 #define raw_spin_is_contended(lock) ((lock)->break_lock) 111 #else 112 113 #ifdef arch_spin_is_contended 114 #define raw_spin_is_contended(lock) arch_spin_is_contended(&(lock)->raw_lock) 115 #else 116 #define raw_spin_is_contended(lock) (((void)(lock), 0)) 117 #endif /*arch_spin_is_contended*/ 118 #endif 119 120 /* 121 * Despite its name it doesn't necessarily has to be a full barrier. 122 * It should only guarantee that a STORE before the critical section 123 * can not be reordered with a LOAD inside this section. 124 * spin_lock() is the one-way barrier, this LOAD can not escape out 125 * of the region. So the default implementation simply ensures that 126 * a STORE can not move into the critical section, smp_wmb() should 127 * serialize it with another STORE done by spin_lock(). 128 */ 129 #ifndef smp_mb__before_spinlock 130 #define smp_mb__before_spinlock() smp_wmb() 131 #endif 132 133 /* 134 * Place this after a lock-acquisition primitive to guarantee that 135 * an UNLOCK+LOCK pair act as a full barrier. This guarantee applies 136 * if the UNLOCK and LOCK are executed by the same CPU or if the 137 * UNLOCK and LOCK operate on the same lock variable. 138 */ 139 #ifndef smp_mb__after_unlock_lock 140 #define smp_mb__after_unlock_lock() do { } while (0) 141 #endif 142 143 /** 144 * raw_spin_unlock_wait - wait until the spinlock gets unlocked 145 * @lock: the spinlock in question. 146 */ 147 #define raw_spin_unlock_wait(lock) arch_spin_unlock_wait(&(lock)->raw_lock) 148 149 #ifdef CONFIG_DEBUG_SPINLOCK 150 extern void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock); 151 #define do_raw_spin_lock_flags(lock, flags) do_raw_spin_lock(lock) 152 extern int do_raw_spin_trylock(raw_spinlock_t *lock); 153 extern void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock); 154 #else 155 static inline void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock) 156 { 157 __acquire(lock); 158 arch_spin_lock(&lock->raw_lock); 159 } 160 161 static inline void 162 do_raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long *flags) __acquires(lock) 163 { 164 __acquire(lock); 165 arch_spin_lock_flags(&lock->raw_lock, *flags); 166 } 167 168 static inline int do_raw_spin_trylock(raw_spinlock_t *lock) 169 { 170 return arch_spin_trylock(&(lock)->raw_lock); 171 } 172 173 static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock) 174 { 175 arch_spin_unlock(&lock->raw_lock); 176 __release(lock); 177 } 178 #endif 179 180 /* 181 * Define the various spin_lock methods. Note we define these 182 * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The 183 * various methods are defined as nops in the case they are not 184 * required. 185 */ 186 #define raw_spin_trylock(lock) __cond_lock(lock, _raw_spin_trylock(lock)) 187 188 #define raw_spin_lock(lock) _raw_spin_lock(lock) 189 190 #ifdef CONFIG_DEBUG_LOCK_ALLOC 191 # define raw_spin_lock_nested(lock, subclass) \ 192 _raw_spin_lock_nested(lock, subclass) 193 194 # define raw_spin_lock_nest_lock(lock, nest_lock) \ 195 do { \ 196 typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\ 197 _raw_spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \ 198 } while (0) 199 #else 200 # define raw_spin_lock_nested(lock, subclass) _raw_spin_lock(lock) 201 # define raw_spin_lock_nest_lock(lock, nest_lock) _raw_spin_lock(lock) 202 #endif 203 204 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 205 206 #define raw_spin_lock_irqsave(lock, flags) \ 207 do { \ 208 typecheck(unsigned long, flags); \ 209 flags = _raw_spin_lock_irqsave(lock); \ 210 } while (0) 211 212 #ifdef CONFIG_DEBUG_LOCK_ALLOC 213 #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \ 214 do { \ 215 typecheck(unsigned long, flags); \ 216 flags = _raw_spin_lock_irqsave_nested(lock, subclass); \ 217 } while (0) 218 #else 219 #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \ 220 do { \ 221 typecheck(unsigned long, flags); \ 222 flags = _raw_spin_lock_irqsave(lock); \ 223 } while (0) 224 #endif 225 226 #else 227 228 #define raw_spin_lock_irqsave(lock, flags) \ 229 do { \ 230 typecheck(unsigned long, flags); \ 231 _raw_spin_lock_irqsave(lock, flags); \ 232 } while (0) 233 234 #define raw_spin_lock_irqsave_nested(lock, flags, subclass) \ 235 raw_spin_lock_irqsave(lock, flags) 236 237 #endif 238 239 #define raw_spin_lock_irq(lock) _raw_spin_lock_irq(lock) 240 #define raw_spin_lock_bh(lock) _raw_spin_lock_bh(lock) 241 #define raw_spin_unlock(lock) _raw_spin_unlock(lock) 242 #define raw_spin_unlock_irq(lock) _raw_spin_unlock_irq(lock) 243 244 #define raw_spin_unlock_irqrestore(lock, flags) \ 245 do { \ 246 typecheck(unsigned long, flags); \ 247 _raw_spin_unlock_irqrestore(lock, flags); \ 248 } while (0) 249 #define raw_spin_unlock_bh(lock) _raw_spin_unlock_bh(lock) 250 251 #define raw_spin_trylock_bh(lock) \ 252 __cond_lock(lock, _raw_spin_trylock_bh(lock)) 253 254 #define raw_spin_trylock_irq(lock) \ 255 ({ \ 256 local_irq_disable(); \ 257 raw_spin_trylock(lock) ? \ 258 1 : ({ local_irq_enable(); 0; }); \ 259 }) 260 261 #define raw_spin_trylock_irqsave(lock, flags) \ 262 ({ \ 263 local_irq_save(flags); \ 264 raw_spin_trylock(lock) ? \ 265 1 : ({ local_irq_restore(flags); 0; }); \ 266 }) 267 268 /** 269 * raw_spin_can_lock - would raw_spin_trylock() succeed? 270 * @lock: the spinlock in question. 271 */ 272 #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock)) 273 274 /* Include rwlock functions */ 275 #include <linux/rwlock.h> 276 277 /* 278 * Pull the _spin_*()/_read_*()/_write_*() functions/declarations: 279 */ 280 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 281 # include <linux/spinlock_api_smp.h> 282 #else 283 # include <linux/spinlock_api_up.h> 284 #endif 285 286 /* 287 * Map the spin_lock functions to the raw variants for PREEMPT_RT=n 288 */ 289 290 static inline raw_spinlock_t *spinlock_check(spinlock_t *lock) 291 { 292 return &lock->rlock; 293 } 294 295 #define spin_lock_init(_lock) \ 296 do { \ 297 spinlock_check(_lock); \ 298 raw_spin_lock_init(&(_lock)->rlock); \ 299 } while (0) 300 301 static inline void spin_lock(spinlock_t *lock) 302 { 303 raw_spin_lock(&lock->rlock); 304 } 305 306 static inline void spin_lock_bh(spinlock_t *lock) 307 { 308 raw_spin_lock_bh(&lock->rlock); 309 } 310 311 static inline int spin_trylock(spinlock_t *lock) 312 { 313 return raw_spin_trylock(&lock->rlock); 314 } 315 316 #define spin_lock_nested(lock, subclass) \ 317 do { \ 318 raw_spin_lock_nested(spinlock_check(lock), subclass); \ 319 } while (0) 320 321 #define spin_lock_nest_lock(lock, nest_lock) \ 322 do { \ 323 raw_spin_lock_nest_lock(spinlock_check(lock), nest_lock); \ 324 } while (0) 325 326 static inline void spin_lock_irq(spinlock_t *lock) 327 { 328 raw_spin_lock_irq(&lock->rlock); 329 } 330 331 #define spin_lock_irqsave(lock, flags) \ 332 do { \ 333 raw_spin_lock_irqsave(spinlock_check(lock), flags); \ 334 } while (0) 335 336 #define spin_lock_irqsave_nested(lock, flags, subclass) \ 337 do { \ 338 raw_spin_lock_irqsave_nested(spinlock_check(lock), flags, subclass); \ 339 } while (0) 340 341 static inline void spin_unlock(spinlock_t *lock) 342 { 343 raw_spin_unlock(&lock->rlock); 344 } 345 346 static inline void spin_unlock_bh(spinlock_t *lock) 347 { 348 raw_spin_unlock_bh(&lock->rlock); 349 } 350 351 static inline void spin_unlock_irq(spinlock_t *lock) 352 { 353 raw_spin_unlock_irq(&lock->rlock); 354 } 355 356 static inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags) 357 { 358 raw_spin_unlock_irqrestore(&lock->rlock, flags); 359 } 360 361 static inline int spin_trylock_bh(spinlock_t *lock) 362 { 363 return raw_spin_trylock_bh(&lock->rlock); 364 } 365 366 static inline int spin_trylock_irq(spinlock_t *lock) 367 { 368 return raw_spin_trylock_irq(&lock->rlock); 369 } 370 371 #define spin_trylock_irqsave(lock, flags) \ 372 ({ \ 373 raw_spin_trylock_irqsave(spinlock_check(lock), flags); \ 374 }) 375 376 static inline void spin_unlock_wait(spinlock_t *lock) 377 { 378 raw_spin_unlock_wait(&lock->rlock); 379 } 380 381 static inline int spin_is_locked(spinlock_t *lock) 382 { 383 return raw_spin_is_locked(&lock->rlock); 384 } 385 386 static inline int spin_is_contended(spinlock_t *lock) 387 { 388 return raw_spin_is_contended(&lock->rlock); 389 } 390 391 static inline int spin_can_lock(spinlock_t *lock) 392 { 393 return raw_spin_can_lock(&lock->rlock); 394 } 395 396 #define assert_spin_locked(lock) assert_raw_spin_locked(&(lock)->rlock) 397 398 /* 399 * Pull the atomic_t declaration: 400 * (asm-mips/atomic.h needs above definitions) 401 */ 402 #include <linux/atomic.h> 403 /** 404 * atomic_dec_and_lock - lock on reaching reference count zero 405 * @atomic: the atomic counter 406 * @lock: the spinlock in question 407 * 408 * Decrements @atomic by 1. If the result is 0, returns true and locks 409 * @lock. Returns false for all other cases. 410 */ 411 extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock); 412 #define atomic_dec_and_lock(atomic, lock) \ 413 __cond_lock(lock, _atomic_dec_and_lock(atomic, lock)) 414 415 #endif /* __LINUX_SPINLOCK_H */
1 #ifndef __LINUX_USB_H 2 #define __LINUX_USB_H 3 4 #include <linux/mod_devicetable.h> 5 #include <linux/usb/ch9.h> 6 7 #define USB_MAJOR 180 8 #define USB_DEVICE_MAJOR 189 9 10 11 #ifdef __KERNEL__ 12 13 #include <linux/errno.h> /* for -ENODEV */ 14 #include <linux/delay.h> /* for mdelay() */ 15 #include <linux/interrupt.h> /* for in_interrupt() */ 16 #include <linux/list.h> /* for struct list_head */ 17 #include <linux/kref.h> /* for struct kref */ 18 #include <linux/device.h> /* for struct device */ 19 #include <linux/fs.h> /* for struct file_operations */ 20 #include <linux/completion.h> /* for struct completion */ 21 #include <linux/sched.h> /* for current && schedule_timeout */ 22 #include <linux/mutex.h> /* for struct mutex */ 23 #include <linux/pm_runtime.h> /* for runtime PM */ 24 25 struct usb_device; 26 struct usb_driver; 27 struct wusb_dev; 28 29 /*-------------------------------------------------------------------------*/ 30 31 /* 32 * Host-side wrappers for standard USB descriptors ... these are parsed 33 * from the data provided by devices. Parsing turns them from a flat 34 * sequence of descriptors into a hierarchy: 35 * 36 * - devices have one (usually) or more configs; 37 * - configs have one (often) or more interfaces; 38 * - interfaces have one (usually) or more settings; 39 * - each interface setting has zero or (usually) more endpoints. 40 * - a SuperSpeed endpoint has a companion descriptor 41 * 42 * And there might be other descriptors mixed in with those. 43 * 44 * Devices may also have class-specific or vendor-specific descriptors. 45 */ 46 47 struct ep_device; 48 49 /** 50 * struct usb_host_endpoint - host-side endpoint descriptor and queue 51 * @desc: descriptor for this endpoint, wMaxPacketSize in native byteorder 52 * @ss_ep_comp: SuperSpeed companion descriptor for this endpoint 53 * @urb_list: urbs queued to this endpoint; maintained by usbcore 54 * @hcpriv: for use by HCD; typically holds hardware dma queue head (QH) 55 * with one or more transfer descriptors (TDs) per urb 56 * @ep_dev: ep_device for sysfs info 57 * @extra: descriptors following this endpoint in the configuration 58 * @extralen: how many bytes of "extra" are valid 59 * @enabled: URBs may be submitted to this endpoint 60 * @streams: number of USB-3 streams allocated on the endpoint 61 * 62 * USB requests are always queued to a given endpoint, identified by a 63 * descriptor within an active interface in a given USB configuration. 64 */ 65 struct usb_host_endpoint { 66 struct usb_endpoint_descriptor desc; 67 struct usb_ss_ep_comp_descriptor ss_ep_comp; 68 struct list_head urb_list; 69 void *hcpriv; 70 struct ep_device *ep_dev; /* For sysfs info */ 71 72 unsigned char *extra; /* Extra descriptors */ 73 int extralen; 74 int enabled; 75 int streams; 76 }; 77 78 /* host-side wrapper for one interface setting's parsed descriptors */ 79 struct usb_host_interface { 80 struct usb_interface_descriptor desc; 81 82 int extralen; 83 unsigned char *extra; /* Extra descriptors */ 84 85 /* array of desc.bNumEndpoint endpoints associated with this 86 * interface setting. these will be in no particular order. 87 */ 88 struct usb_host_endpoint *endpoint; 89 90 char *string; /* iInterface string, if present */ 91 }; 92 93 enum usb_interface_condition { 94 USB_INTERFACE_UNBOUND = 0, 95 USB_INTERFACE_BINDING, 96 USB_INTERFACE_BOUND, 97 USB_INTERFACE_UNBINDING, 98 }; 99 100 /** 101 * struct usb_interface - what usb device drivers talk to 102 * @altsetting: array of interface structures, one for each alternate 103 * setting that may be selected. Each one includes a set of 104 * endpoint configurations. They will be in no particular order. 105 * @cur_altsetting: the current altsetting. 106 * @num_altsetting: number of altsettings defined. 107 * @intf_assoc: interface association descriptor 108 * @minor: the minor number assigned to this interface, if this 109 * interface is bound to a driver that uses the USB major number. 110 * If this interface does not use the USB major, this field should 111 * be unused. The driver should set this value in the probe() 112 * function of the driver, after it has been assigned a minor 113 * number from the USB core by calling usb_register_dev(). 114 * @condition: binding state of the interface: not bound, binding 115 * (in probe()), bound to a driver, or unbinding (in disconnect()) 116 * @sysfs_files_created: sysfs attributes exist 117 * @ep_devs_created: endpoint child pseudo-devices exist 118 * @unregistering: flag set when the interface is being unregistered 119 * @needs_remote_wakeup: flag set when the driver requires remote-wakeup 120 * capability during autosuspend. 121 * @needs_altsetting0: flag set when a set-interface request for altsetting 0 122 * has been deferred. 123 * @needs_binding: flag set when the driver should be re-probed or unbound 124 * following a reset or suspend operation it doesn't support. 125 * @dev: driver model's view of this device 126 * @usb_dev: if an interface is bound to the USB major, this will point 127 * to the sysfs representation for that device. 128 * @pm_usage_cnt: PM usage counter for this interface 129 * @reset_ws: Used for scheduling resets from atomic context. 130 * @reset_running: set to 1 if the interface is currently running a 131 * queued reset so that usb_cancel_queued_reset() doesn't try to 132 * remove from the workqueue when running inside the worker 133 * thread. See __usb_queue_reset_device(). 134 * @resetting_device: USB core reset the device, so use alt setting 0 as 135 * current; needs bandwidth alloc after reset. 136 * 137 * USB device drivers attach to interfaces on a physical device. Each 138 * interface encapsulates a single high level function, such as feeding 139 * an audio stream to a speaker or reporting a change in a volume control. 140 * Many USB devices only have one interface. The protocol used to talk to 141 * an interface's endpoints can be defined in a usb "class" specification, 142 * or by a product's vendor. The (default) control endpoint is part of 143 * every interface, but is never listed among the interface's descriptors. 144 * 145 * The driver that is bound to the interface can use standard driver model 146 * calls such as dev_get_drvdata() on the dev member of this structure. 147 * 148 * Each interface may have alternate settings. The initial configuration 149 * of a device sets altsetting 0, but the device driver can change 150 * that setting using usb_set_interface(). Alternate settings are often 151 * used to control the use of periodic endpoints, such as by having 152 * different endpoints use different amounts of reserved USB bandwidth. 153 * All standards-conformant USB devices that use isochronous endpoints 154 * will use them in non-default settings. 155 * 156 * The USB specification says that alternate setting numbers must run from 157 * 0 to one less than the total number of alternate settings. But some 158 * devices manage to mess this up, and the structures aren't necessarily 159 * stored in numerical order anyhow. Use usb_altnum_to_altsetting() to 160 * look up an alternate setting in the altsetting array based on its number. 161 */ 162 struct usb_interface { 163 /* array of alternate settings for this interface, 164 * stored in no particular order */ 165 struct usb_host_interface *altsetting; 166 167 struct usb_host_interface *cur_altsetting; /* the currently 168 * active alternate setting */ 169 unsigned num_altsetting; /* number of alternate settings */ 170 171 /* If there is an interface association descriptor then it will list 172 * the associated interfaces */ 173 struct usb_interface_assoc_descriptor *intf_assoc; 174 175 int minor; /* minor number this interface is 176 * bound to */ 177 enum usb_interface_condition condition; /* state of binding */ 178 unsigned sysfs_files_created:1; /* the sysfs attributes exist */ 179 unsigned ep_devs_created:1; /* endpoint "devices" exist */ 180 unsigned unregistering:1; /* unregistration is in progress */ 181 unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */ 182 unsigned needs_altsetting0:1; /* switch to altsetting 0 is pending */ 183 unsigned needs_binding:1; /* needs delayed unbind/rebind */ 184 unsigned reset_running:1; 185 unsigned resetting_device:1; /* true: bandwidth alloc after reset */ 186 187 struct device dev; /* interface specific device info */ 188 struct device *usb_dev; 189 atomic_t pm_usage_cnt; /* usage counter for autosuspend */ 190 struct work_struct reset_ws; /* for resets in atomic context */ 191 }; 192 #define to_usb_interface(d) container_of(d, struct usb_interface, dev) 193 194 static inline void *usb_get_intfdata(struct usb_interface *intf) 195 { 196 return dev_get_drvdata(&intf->dev); 197 } 198 199 static inline void usb_set_intfdata(struct usb_interface *intf, void *data) 200 { 201 dev_set_drvdata(&intf->dev, data); 202 } 203 204 struct usb_interface *usb_get_intf(struct usb_interface *intf); 205 void usb_put_intf(struct usb_interface *intf); 206 207 /* Hard limit */ 208 #define USB_MAXENDPOINTS 30 209 /* this maximum is arbitrary */ 210 #define USB_MAXINTERFACES 32 211 #define USB_MAXIADS (USB_MAXINTERFACES/2) 212 213 /** 214 * struct usb_interface_cache - long-term representation of a device interface 215 * @num_altsetting: number of altsettings defined. 216 * @ref: reference counter. 217 * @altsetting: variable-length array of interface structures, one for 218 * each alternate setting that may be selected. Each one includes a 219 * set of endpoint configurations. They will be in no particular order. 220 * 221 * These structures persist for the lifetime of a usb_device, unlike 222 * struct usb_interface (which persists only as long as its configuration 223 * is installed). The altsetting arrays can be accessed through these 224 * structures at any time, permitting comparison of configurations and 225 * providing support for the /proc/bus/usb/devices pseudo-file. 226 */ 227 struct usb_interface_cache { 228 unsigned num_altsetting; /* number of alternate settings */ 229 struct kref ref; /* reference counter */ 230 231 /* variable-length array of alternate settings for this interface, 232 * stored in no particular order */ 233 struct usb_host_interface altsetting[0]; 234 }; 235 #define ref_to_usb_interface_cache(r) \ 236 container_of(r, struct usb_interface_cache, ref) 237 #define altsetting_to_usb_interface_cache(a) \ 238 container_of(a, struct usb_interface_cache, altsetting[0]) 239 240 /** 241 * struct usb_host_config - representation of a device's configuration 242 * @desc: the device's configuration descriptor. 243 * @string: pointer to the cached version of the iConfiguration string, if 244 * present for this configuration. 245 * @intf_assoc: list of any interface association descriptors in this config 246 * @interface: array of pointers to usb_interface structures, one for each 247 * interface in the configuration. The number of interfaces is stored 248 * in desc.bNumInterfaces. These pointers are valid only while the 249 * the configuration is active. 250 * @intf_cache: array of pointers to usb_interface_cache structures, one 251 * for each interface in the configuration. These structures exist 252 * for the entire life of the device. 253 * @extra: pointer to buffer containing all extra descriptors associated 254 * with this configuration (those preceding the first interface 255 * descriptor). 256 * @extralen: length of the extra descriptors buffer. 257 * 258 * USB devices may have multiple configurations, but only one can be active 259 * at any time. Each encapsulates a different operational environment; 260 * for example, a dual-speed device would have separate configurations for 261 * full-speed and high-speed operation. The number of configurations 262 * available is stored in the device descriptor as bNumConfigurations. 263 * 264 * A configuration can contain multiple interfaces. Each corresponds to 265 * a different function of the USB device, and all are available whenever 266 * the configuration is active. The USB standard says that interfaces 267 * are supposed to be numbered from 0 to desc.bNumInterfaces-1, but a lot 268 * of devices get this wrong. In addition, the interface array is not 269 * guaranteed to be sorted in numerical order. Use usb_ifnum_to_if() to 270 * look up an interface entry based on its number. 271 * 272 * Device drivers should not attempt to activate configurations. The choice 273 * of which configuration to install is a policy decision based on such 274 * considerations as available power, functionality provided, and the user's 275 * desires (expressed through userspace tools). However, drivers can call 276 * usb_reset_configuration() to reinitialize the current configuration and 277 * all its interfaces. 278 */ 279 struct usb_host_config { 280 struct usb_config_descriptor desc; 281 282 char *string; /* iConfiguration string, if present */ 283 284 /* List of any Interface Association Descriptors in this 285 * configuration. */ 286 struct usb_interface_assoc_descriptor *intf_assoc[USB_MAXIADS]; 287 288 /* the interfaces associated with this configuration, 289 * stored in no particular order */ 290 struct usb_interface *interface[USB_MAXINTERFACES]; 291 292 /* Interface information available even when this is not the 293 * active configuration */ 294 struct usb_interface_cache *intf_cache[USB_MAXINTERFACES]; 295 296 unsigned char *extra; /* Extra descriptors */ 297 int extralen; 298 }; 299 300 /* USB2.0 and USB3.0 device BOS descriptor set */ 301 struct usb_host_bos { 302 struct usb_bos_descriptor *desc; 303 304 /* wireless cap descriptor is handled by wusb */ 305 struct usb_ext_cap_descriptor *ext_cap; 306 struct usb_ss_cap_descriptor *ss_cap; 307 struct usb_ss_container_id_descriptor *ss_id; 308 }; 309 310 int __usb_get_extra_descriptor(char *buffer, unsigned size, 311 unsigned char type, void **ptr); 312 #define usb_get_extra_descriptor(ifpoint, type, ptr) \ 313 __usb_get_extra_descriptor((ifpoint)->extra, \ 314 (ifpoint)->extralen, \ 315 type, (void **)ptr) 316 317 /* ----------------------------------------------------------------------- */ 318 319 /* USB device number allocation bitmap */ 320 struct usb_devmap { 321 unsigned long devicemap[128 / (8*sizeof(unsigned long))]; 322 }; 323 324 /* 325 * Allocated per bus (tree of devices) we have: 326 */ 327 struct usb_bus { 328 struct device *controller; /* host/master side hardware */ 329 int busnum; /* Bus number (in order of reg) */ 330 const char *bus_name; /* stable id (PCI slot_name etc) */ 331 u8 uses_dma; /* Does the host controller use DMA? */ 332 u8 uses_pio_for_control; /* 333 * Does the host controller use PIO 334 * for control transfers? 335 */ 336 u8 otg_port; /* 0, or number of OTG/HNP port */ 337 unsigned is_b_host:1; /* true during some HNP roleswitches */ 338 unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */ 339 unsigned no_stop_on_short:1; /* 340 * Quirk: some controllers don't stop 341 * the ep queue on a short transfer 342 * with the URB_SHORT_NOT_OK flag set. 343 */ 344 unsigned no_sg_constraint:1; /* no sg constraint */ 345 unsigned sg_tablesize; /* 0 or largest number of sg list entries */ 346 347 int devnum_next; /* Next open device number in 348 * round-robin allocation */ 349 350 struct usb_devmap devmap; /* device address allocation map */ 351 struct usb_device *root_hub; /* Root hub */ 352 struct usb_bus *hs_companion; /* Companion EHCI bus, if any */ 353 struct list_head bus_list; /* list of busses */ 354 355 struct mutex usb_address0_mutex; /* unaddressed device mutex */ 356 357 int bandwidth_allocated; /* on this bus: how much of the time 358 * reserved for periodic (intr/iso) 359 * requests is used, on average? 360 * Units: microseconds/frame. 361 * Limits: Full/low speed reserve 90%, 362 * while high speed reserves 80%. 363 */ 364 int bandwidth_int_reqs; /* number of Interrupt requests */ 365 int bandwidth_isoc_reqs; /* number of Isoc. requests */ 366 367 unsigned resuming_ports; /* bit array: resuming root-hub ports */ 368 369 #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE) 370 struct mon_bus *mon_bus; /* non-null when associated */ 371 int monitored; /* non-zero when monitored */ 372 #endif 373 }; 374 375 struct usb_dev_state; 376 377 /* ----------------------------------------------------------------------- */ 378 379 struct usb_tt; 380 381 enum usb_device_removable { 382 USB_DEVICE_REMOVABLE_UNKNOWN = 0, 383 USB_DEVICE_REMOVABLE, 384 USB_DEVICE_FIXED, 385 }; 386 387 enum usb_port_connect_type { 388 USB_PORT_CONNECT_TYPE_UNKNOWN = 0, 389 USB_PORT_CONNECT_TYPE_HOT_PLUG, 390 USB_PORT_CONNECT_TYPE_HARD_WIRED, 391 USB_PORT_NOT_USED, 392 }; 393 394 /* 395 * USB 2.0 Link Power Management (LPM) parameters. 396 */ 397 struct usb2_lpm_parameters { 398 /* Best effort service latency indicate how long the host will drive 399 * resume on an exit from L1. 400 */ 401 unsigned int besl; 402 403 /* Timeout value in microseconds for the L1 inactivity (LPM) timer. 404 * When the timer counts to zero, the parent hub will initiate a LPM 405 * transition to L1. 406 */ 407 int timeout; 408 }; 409 410 /* 411 * USB 3.0 Link Power Management (LPM) parameters. 412 * 413 * PEL and SEL are USB 3.0 Link PM latencies for device-initiated LPM exit. 414 * MEL is the USB 3.0 Link PM latency for host-initiated LPM exit. 415 * All three are stored in nanoseconds. 416 */ 417 struct usb3_lpm_parameters { 418 /* 419 * Maximum exit latency (MEL) for the host to send a packet to the 420 * device (either a Ping for isoc endpoints, or a data packet for 421 * interrupt endpoints), the hubs to decode the packet, and for all hubs 422 * in the path to transition the links to U0. 423 */ 424 unsigned int mel; 425 /* 426 * Maximum exit latency for a device-initiated LPM transition to bring 427 * all links into U0. Abbreviated as "PEL" in section 9.4.12 of the USB 428 * 3.0 spec, with no explanation of what "P" stands for. "Path"? 429 */ 430 unsigned int pel; 431 432 /* 433 * The System Exit Latency (SEL) includes PEL, and three other 434 * latencies. After a device initiates a U0 transition, it will take 435 * some time from when the device sends the ERDY to when it will finally 436 * receive the data packet. Basically, SEL should be the worse-case 437 * latency from when a device starts initiating a U0 transition to when 438 * it will get data. 439 */ 440 unsigned int sel; 441 /* 442 * The idle timeout value that is currently programmed into the parent 443 * hub for this device. When the timer counts to zero, the parent hub 444 * will initiate an LPM transition to either U1 or U2. 445 */ 446 int timeout; 447 }; 448 449 /** 450 * struct usb_device - kernel's representation of a USB device 451 * @devnum: device number; address on a USB bus 452 * @devpath: device ID string for use in messages (e.g., /port/...) 453 * @route: tree topology hex string for use with xHCI 454 * @state: device state: configured, not attached, etc. 455 * @speed: device speed: high/full/low (or error) 456 * @tt: Transaction Translator info; used with low/full speed dev, highspeed hub 457 * @ttport: device port on that tt hub 458 * @toggle: one bit for each endpoint, with ([0] = IN, [1] = OUT) endpoints 459 * @parent: our hub, unless we're the root 460 * @bus: bus we're part of 461 * @ep0: endpoint 0 data (default control pipe) 462 * @dev: generic device interface 463 * @descriptor: USB device descriptor 464 * @bos: USB device BOS descriptor set 465 * @config: all of the device's configs 466 * @actconfig: the active configuration 467 * @ep_in: array of IN endpoints 468 * @ep_out: array of OUT endpoints 469 * @rawdescriptors: raw descriptors for each config 470 * @bus_mA: Current available from the bus 471 * @portnum: parent port number (origin 1) 472 * @level: number of USB hub ancestors 473 * @can_submit: URBs may be submitted 474 * @persist_enabled: USB_PERSIST enabled for this device 475 * @have_langid: whether string_langid is valid 476 * @authorized: policy has said we can use it; 477 * (user space) policy determines if we authorize this device to be 478 * used or not. By default, wired USB devices are authorized. 479 * WUSB devices are not, until we authorize them from user space. 480 * FIXME -- complete doc 481 * @authenticated: Crypto authentication passed 482 * @wusb: device is Wireless USB 483 * @lpm_capable: device supports LPM 484 * @usb2_hw_lpm_capable: device can perform USB2 hardware LPM 485 * @usb2_hw_lpm_besl_capable: device can perform USB2 hardware BESL LPM 486 * @usb2_hw_lpm_enabled: USB2 hardware LPM is enabled 487 * @usb2_hw_lpm_allowed: Userspace allows USB 2.0 LPM to be enabled 488 * @usb3_lpm_enabled: USB3 hardware LPM enabled 489 * @string_langid: language ID for strings 490 * @product: iProduct string, if present (static) 491 * @manufacturer: iManufacturer string, if present (static) 492 * @serial: iSerialNumber string, if present (static) 493 * @filelist: usbfs files that are open to this device 494 * @maxchild: number of ports if hub 495 * @quirks: quirks of the whole device 496 * @urbnum: number of URBs submitted for the whole device 497 * @active_duration: total time device is not suspended 498 * @connect_time: time device was first connected 499 * @do_remote_wakeup: remote wakeup should be enabled 500 * @reset_resume: needs reset instead of resume 501 * @port_is_suspended: the upstream port is suspended (L2 or U3) 502 * @wusb_dev: if this is a Wireless USB device, link to the WUSB 503 * specific data for the device. 504 * @slot_id: Slot ID assigned by xHCI 505 * @removable: Device can be physically removed from this port 506 * @l1_params: best effor service latency for USB2 L1 LPM state, and L1 timeout. 507 * @u1_params: exit latencies for USB3 U1 LPM state, and hub-initiated timeout. 508 * @u2_params: exit latencies for USB3 U2 LPM state, and hub-initiated timeout. 509 * @lpm_disable_count: Ref count used by usb_disable_lpm() and usb_enable_lpm() 510 * to keep track of the number of functions that require USB 3.0 Link Power 511 * Management to be disabled for this usb_device. This count should only 512 * be manipulated by those functions, with the bandwidth_mutex is held. 513 * 514 * Notes: 515 * Usbcore drivers should not set usbdev->state directly. Instead use 516 * usb_set_device_state(). 517 */ 518 struct usb_device { 519 int devnum; 520 char devpath[16]; 521 u32 route; 522 enum usb_device_state state; 523 enum usb_device_speed speed; 524 525 struct usb_tt *tt; 526 int ttport; 527 528 unsigned int toggle[2]; 529 530 struct usb_device *parent; 531 struct usb_bus *bus; 532 struct usb_host_endpoint ep0; 533 534 struct device dev; 535 536 struct usb_device_descriptor descriptor; 537 struct usb_host_bos *bos; 538 struct usb_host_config *config; 539 540 struct usb_host_config *actconfig; 541 struct usb_host_endpoint *ep_in[16]; 542 struct usb_host_endpoint *ep_out[16]; 543 544 char **rawdescriptors; 545 546 unsigned short bus_mA; 547 u8 portnum; 548 u8 level; 549 550 unsigned can_submit:1; 551 unsigned persist_enabled:1; 552 unsigned have_langid:1; 553 unsigned authorized:1; 554 unsigned authenticated:1; 555 unsigned wusb:1; 556 unsigned lpm_capable:1; 557 unsigned usb2_hw_lpm_capable:1; 558 unsigned usb2_hw_lpm_besl_capable:1; 559 unsigned usb2_hw_lpm_enabled:1; 560 unsigned usb2_hw_lpm_allowed:1; 561 unsigned usb3_lpm_enabled:1; 562 int string_langid; 563 564 /* static strings from the device */ 565 char *product; 566 char *manufacturer; 567 char *serial; 568 569 struct list_head filelist; 570 571 int maxchild; 572 573 u32 quirks; 574 atomic_t urbnum; 575 576 unsigned long active_duration; 577 578 #ifdef CONFIG_PM 579 unsigned long connect_time; 580 581 unsigned do_remote_wakeup:1; 582 unsigned reset_resume:1; 583 unsigned port_is_suspended:1; 584 #endif 585 struct wusb_dev *wusb_dev; 586 int slot_id; 587 enum usb_device_removable removable; 588 struct usb2_lpm_parameters l1_params; 589 struct usb3_lpm_parameters u1_params; 590 struct usb3_lpm_parameters u2_params; 591 unsigned lpm_disable_count; 592 }; 593 #define to_usb_device(d) container_of(d, struct usb_device, dev) 594 595 static inline struct usb_device *interface_to_usbdev(struct usb_interface *intf) 596 { 597 return to_usb_device(intf->dev.parent); 598 } 599 600 extern struct usb_device *usb_get_dev(struct usb_device *dev); 601 extern void usb_put_dev(struct usb_device *dev); 602 extern struct usb_device *usb_hub_find_child(struct usb_device *hdev, 603 int port1); 604 605 /** 606 * usb_hub_for_each_child - iterate over all child devices on the hub 607 * @hdev: USB device belonging to the usb hub 608 * @port1: portnum associated with child device 609 * @child: child device pointer 610 */ 611 #define usb_hub_for_each_child(hdev, port1, child) \ 612 for (port1 = 1, child = usb_hub_find_child(hdev, port1); \ 613 port1 <= hdev->maxchild; \ 614 child = usb_hub_find_child(hdev, ++port1)) \ 615 if (!child) continue; else 616 617 /* USB device locking */ 618 #define usb_lock_device(udev) device_lock(&(udev)->dev) 619 #define usb_unlock_device(udev) device_unlock(&(udev)->dev) 620 #define usb_trylock_device(udev) device_trylock(&(udev)->dev) 621 extern int usb_lock_device_for_reset(struct usb_device *udev, 622 const struct usb_interface *iface); 623 624 /* USB port reset for device reinitialization */ 625 extern int usb_reset_device(struct usb_device *dev); 626 extern void usb_queue_reset_device(struct usb_interface *dev); 627 628 #ifdef CONFIG_ACPI 629 extern int usb_acpi_set_power_state(struct usb_device *hdev, int index, 630 bool enable); 631 extern bool usb_acpi_power_manageable(struct usb_device *hdev, int index); 632 #else 633 static inline int usb_acpi_set_power_state(struct usb_device *hdev, int index, 634 bool enable) { return 0; } 635 static inline bool usb_acpi_power_manageable(struct usb_device *hdev, int index) 636 { return true; } 637 #endif 638 639 /* USB autosuspend and autoresume */ 640 #ifdef CONFIG_PM_RUNTIME 641 extern void usb_enable_autosuspend(struct usb_device *udev); 642 extern void usb_disable_autosuspend(struct usb_device *udev); 643 644 extern int usb_autopm_get_interface(struct usb_interface *intf); 645 extern void usb_autopm_put_interface(struct usb_interface *intf); 646 extern int usb_autopm_get_interface_async(struct usb_interface *intf); 647 extern void usb_autopm_put_interface_async(struct usb_interface *intf); 648 extern void usb_autopm_get_interface_no_resume(struct usb_interface *intf); 649 extern void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); 650 651 static inline void usb_mark_last_busy(struct usb_device *udev) 652 { 653 pm_runtime_mark_last_busy(&udev->dev); 654 } 655 656 #else 657 658 static inline int usb_enable_autosuspend(struct usb_device *udev) 659 { return 0; } 660 static inline int usb_disable_autosuspend(struct usb_device *udev) 661 { return 0; } 662 663 static inline int usb_autopm_get_interface(struct usb_interface *intf) 664 { return 0; } 665 static inline int usb_autopm_get_interface_async(struct usb_interface *intf) 666 { return 0; } 667 668 static inline void usb_autopm_put_interface(struct usb_interface *intf) 669 { } 670 static inline void usb_autopm_put_interface_async(struct usb_interface *intf) 671 { } 672 static inline void usb_autopm_get_interface_no_resume( 673 struct usb_interface *intf) 674 { } 675 static inline void usb_autopm_put_interface_no_suspend( 676 struct usb_interface *intf) 677 { } 678 static inline void usb_mark_last_busy(struct usb_device *udev) 679 { } 680 #endif 681 682 extern int usb_disable_lpm(struct usb_device *udev); 683 extern void usb_enable_lpm(struct usb_device *udev); 684 /* Same as above, but these functions lock/unlock the bandwidth_mutex. */ 685 extern int usb_unlocked_disable_lpm(struct usb_device *udev); 686 extern void usb_unlocked_enable_lpm(struct usb_device *udev); 687 688 extern int usb_disable_ltm(struct usb_device *udev); 689 extern void usb_enable_ltm(struct usb_device *udev); 690 691 static inline bool usb_device_supports_ltm(struct usb_device *udev) 692 { 693 if (udev->speed != USB_SPEED_SUPER || !udev->bos || !udev->bos->ss_cap) 694 return false; 695 return udev->bos->ss_cap->bmAttributes & USB_LTM_SUPPORT; 696 } 697 698 static inline bool usb_device_no_sg_constraint(struct usb_device *udev) 699 { 700 return udev && udev->bus && udev->bus->no_sg_constraint; 701 } 702 703 704 /*-------------------------------------------------------------------------*/ 705 706 /* for drivers using iso endpoints */ 707 extern int usb_get_current_frame_number(struct usb_device *usb_dev); 708 709 /* Sets up a group of bulk endpoints to support multiple stream IDs. */ 710 extern int usb_alloc_streams(struct usb_interface *interface, 711 struct usb_host_endpoint **eps, unsigned int num_eps, 712 unsigned int num_streams, gfp_t mem_flags); 713 714 /* Reverts a group of bulk endpoints back to not using stream IDs. */ 715 extern int usb_free_streams(struct usb_interface *interface, 716 struct usb_host_endpoint **eps, unsigned int num_eps, 717 gfp_t mem_flags); 718 719 /* used these for multi-interface device registration */ 720 extern int usb_driver_claim_interface(struct usb_driver *driver, 721 struct usb_interface *iface, void *priv); 722 723 /** 724 * usb_interface_claimed - returns true iff an interface is claimed 725 * @iface: the interface being checked 726 * 727 * Return: %true (nonzero) iff the interface is claimed, else %false 728 * (zero). 729 * 730 * Note: 731 * Callers must own the driver model's usb bus readlock. So driver 732 * probe() entries don't need extra locking, but other call contexts 733 * may need to explicitly claim that lock. 734 * 735 */ 736 static inline int usb_interface_claimed(struct usb_interface *iface) 737 { 738 return (iface->dev.driver != NULL); 739 } 740 741 extern void usb_driver_release_interface(struct usb_driver *driver, 742 struct usb_interface *iface); 743 const struct usb_device_id *usb_match_id(struct usb_interface *interface, 744 const struct usb_device_id *id); 745 extern int usb_match_one_id(struct usb_interface *interface, 746 const struct usb_device_id *id); 747 748 extern int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *)); 749 extern struct usb_interface *usb_find_interface(struct usb_driver *drv, 750 int minor); 751 extern struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev, 752 unsigned ifnum); 753 extern struct usb_host_interface *usb_altnum_to_altsetting( 754 const struct usb_interface *intf, unsigned int altnum); 755 extern struct usb_host_interface *usb_find_alt_setting( 756 struct usb_host_config *config, 757 unsigned int iface_num, 758 unsigned int alt_num); 759 760 /* port claiming functions */ 761 int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, 762 struct usb_dev_state *owner); 763 int usb_hub_release_port(struct usb_device *hdev, unsigned port1, 764 struct usb_dev_state *owner); 765 766 /** 767 * usb_make_path - returns stable device path in the usb tree 768 * @dev: the device whose path is being constructed 769 * @buf: where to put the string 770 * @size: how big is "buf"? 771 * 772 * Return: Length of the string (> 0) or negative if size was too small. 773 * 774 * Note: 775 * This identifier is intended to be "stable", reflecting physical paths in 776 * hardware such as physical bus addresses for host controllers or ports on 777 * USB hubs. That makes it stay the same until systems are physically 778 * reconfigured, by re-cabling a tree of USB devices or by moving USB host 779 * controllers. Adding and removing devices, including virtual root hubs 780 * in host controller driver modules, does not change these path identifiers; 781 * neither does rebooting or re-enumerating. These are more useful identifiers 782 * than changeable ("unstable") ones like bus numbers or device addresses. 783 * 784 * With a partial exception for devices connected to USB 2.0 root hubs, these 785 * identifiers are also predictable. So long as the device tree isn't changed, 786 * plugging any USB device into a given hub port always gives it the same path. 787 * Because of the use of "companion" controllers, devices connected to ports on 788 * USB 2.0 root hubs (EHCI host controllers) will get one path ID if they are 789 * high speed, and a different one if they are full or low speed. 790 */ 791 static inline int usb_make_path(struct usb_device *dev, char *buf, size_t size) 792 { 793 int actual; 794 actual = snprintf(buf, size, "usb-%s-%s", dev->bus->bus_name, 795 dev->devpath); 796 return (actual >= (int)size) ? -1 : actual; 797 } 798 799 /*-------------------------------------------------------------------------*/ 800 801 #define USB_DEVICE_ID_MATCH_DEVICE \ 802 (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT) 803 #define USB_DEVICE_ID_MATCH_DEV_RANGE \ 804 (USB_DEVICE_ID_MATCH_DEV_LO | USB_DEVICE_ID_MATCH_DEV_HI) 805 #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \ 806 (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_RANGE) 807 #define USB_DEVICE_ID_MATCH_DEV_INFO \ 808 (USB_DEVICE_ID_MATCH_DEV_CLASS | \ 809 USB_DEVICE_ID_MATCH_DEV_SUBCLASS | \ 810 USB_DEVICE_ID_MATCH_DEV_PROTOCOL) 811 #define USB_DEVICE_ID_MATCH_INT_INFO \ 812 (USB_DEVICE_ID_MATCH_INT_CLASS | \ 813 USB_DEVICE_ID_MATCH_INT_SUBCLASS | \ 814 USB_DEVICE_ID_MATCH_INT_PROTOCOL) 815 816 /** 817 * USB_DEVICE - macro used to describe a specific usb device 818 * @vend: the 16 bit USB Vendor ID 819 * @prod: the 16 bit USB Product ID 820 * 821 * This macro is used to create a struct usb_device_id that matches a 822 * specific device. 823 */ 824 #define USB_DEVICE(vend, prod) \ 825 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, \ 826 .idVendor = (vend), \ 827 .idProduct = (prod) 828 /** 829 * USB_DEVICE_VER - describe a specific usb device with a version range 830 * @vend: the 16 bit USB Vendor ID 831 * @prod: the 16 bit USB Product ID 832 * @lo: the bcdDevice_lo value 833 * @hi: the bcdDevice_hi value 834 * 835 * This macro is used to create a struct usb_device_id that matches a 836 * specific device, with a version range. 837 */ 838 #define USB_DEVICE_VER(vend, prod, lo, hi) \ 839 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, \ 840 .idVendor = (vend), \ 841 .idProduct = (prod), \ 842 .bcdDevice_lo = (lo), \ 843 .bcdDevice_hi = (hi) 844 845 /** 846 * USB_DEVICE_INTERFACE_CLASS - describe a usb device with a specific interface class 847 * @vend: the 16 bit USB Vendor ID 848 * @prod: the 16 bit USB Product ID 849 * @cl: bInterfaceClass value 850 * 851 * This macro is used to create a struct usb_device_id that matches a 852 * specific interface class of devices. 853 */ 854 #define USB_DEVICE_INTERFACE_CLASS(vend, prod, cl) \ 855 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 856 USB_DEVICE_ID_MATCH_INT_CLASS, \ 857 .idVendor = (vend), \ 858 .idProduct = (prod), \ 859 .bInterfaceClass = (cl) 860 861 /** 862 * USB_DEVICE_INTERFACE_PROTOCOL - describe a usb device with a specific interface protocol 863 * @vend: the 16 bit USB Vendor ID 864 * @prod: the 16 bit USB Product ID 865 * @pr: bInterfaceProtocol value 866 * 867 * This macro is used to create a struct usb_device_id that matches a 868 * specific interface protocol of devices. 869 */ 870 #define USB_DEVICE_INTERFACE_PROTOCOL(vend, prod, pr) \ 871 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 872 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \ 873 .idVendor = (vend), \ 874 .idProduct = (prod), \ 875 .bInterfaceProtocol = (pr) 876 877 /** 878 * USB_DEVICE_INTERFACE_NUMBER - describe a usb device with a specific interface number 879 * @vend: the 16 bit USB Vendor ID 880 * @prod: the 16 bit USB Product ID 881 * @num: bInterfaceNumber value 882 * 883 * This macro is used to create a struct usb_device_id that matches a 884 * specific interface number of devices. 885 */ 886 #define USB_DEVICE_INTERFACE_NUMBER(vend, prod, num) \ 887 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 888 USB_DEVICE_ID_MATCH_INT_NUMBER, \ 889 .idVendor = (vend), \ 890 .idProduct = (prod), \ 891 .bInterfaceNumber = (num) 892 893 /** 894 * USB_DEVICE_INFO - macro used to describe a class of usb devices 895 * @cl: bDeviceClass value 896 * @sc: bDeviceSubClass value 897 * @pr: bDeviceProtocol value 898 * 899 * This macro is used to create a struct usb_device_id that matches a 900 * specific class of devices. 901 */ 902 #define USB_DEVICE_INFO(cl, sc, pr) \ 903 .match_flags = USB_DEVICE_ID_MATCH_DEV_INFO, \ 904 .bDeviceClass = (cl), \ 905 .bDeviceSubClass = (sc), \ 906 .bDeviceProtocol = (pr) 907 908 /** 909 * USB_INTERFACE_INFO - macro used to describe a class of usb interfaces 910 * @cl: bInterfaceClass value 911 * @sc: bInterfaceSubClass value 912 * @pr: bInterfaceProtocol value 913 * 914 * This macro is used to create a struct usb_device_id that matches a 915 * specific class of interfaces. 916 */ 917 #define USB_INTERFACE_INFO(cl, sc, pr) \ 918 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, \ 919 .bInterfaceClass = (cl), \ 920 .bInterfaceSubClass = (sc), \ 921 .bInterfaceProtocol = (pr) 922 923 /** 924 * USB_DEVICE_AND_INTERFACE_INFO - describe a specific usb device with a class of usb interfaces 925 * @vend: the 16 bit USB Vendor ID 926 * @prod: the 16 bit USB Product ID 927 * @cl: bInterfaceClass value 928 * @sc: bInterfaceSubClass value 929 * @pr: bInterfaceProtocol value 930 * 931 * This macro is used to create a struct usb_device_id that matches a 932 * specific device with a specific class of interfaces. 933 * 934 * This is especially useful when explicitly matching devices that have 935 * vendor specific bDeviceClass values, but standards-compliant interfaces. 936 */ 937 #define USB_DEVICE_AND_INTERFACE_INFO(vend, prod, cl, sc, pr) \ 938 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \ 939 | USB_DEVICE_ID_MATCH_DEVICE, \ 940 .idVendor = (vend), \ 941 .idProduct = (prod), \ 942 .bInterfaceClass = (cl), \ 943 .bInterfaceSubClass = (sc), \ 944 .bInterfaceProtocol = (pr) 945 946 /** 947 * USB_VENDOR_AND_INTERFACE_INFO - describe a specific usb vendor with a class of usb interfaces 948 * @vend: the 16 bit USB Vendor ID 949 * @cl: bInterfaceClass value 950 * @sc: bInterfaceSubClass value 951 * @pr: bInterfaceProtocol value 952 * 953 * This macro is used to create a struct usb_device_id that matches a 954 * specific vendor with a specific class of interfaces. 955 * 956 * This is especially useful when explicitly matching devices that have 957 * vendor specific bDeviceClass values, but standards-compliant interfaces. 958 */ 959 #define USB_VENDOR_AND_INTERFACE_INFO(vend, cl, sc, pr) \ 960 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \ 961 | USB_DEVICE_ID_MATCH_VENDOR, \ 962 .idVendor = (vend), \ 963 .bInterfaceClass = (cl), \ 964 .bInterfaceSubClass = (sc), \ 965 .bInterfaceProtocol = (pr) 966 967 /* ----------------------------------------------------------------------- */ 968 969 /* Stuff for dynamic usb ids */ 970 struct usb_dynids { 971 spinlock_t lock; 972 struct list_head list; 973 }; 974 975 struct usb_dynid { 976 struct list_head node; 977 struct usb_device_id id; 978 }; 979 980 extern ssize_t usb_store_new_id(struct usb_dynids *dynids, 981 const struct usb_device_id *id_table, 982 struct device_driver *driver, 983 const char *buf, size_t count); 984 985 extern ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf); 986 987 /** 988 * struct usbdrv_wrap - wrapper for driver-model structure 989 * @driver: The driver-model core driver structure. 990 * @for_devices: Non-zero for device drivers, 0 for interface drivers. 991 */ 992 struct usbdrv_wrap { 993 struct device_driver driver; 994 int for_devices; 995 }; 996 997 /** 998 * struct usb_driver - identifies USB interface driver to usbcore 999 * @name: The driver name should be unique among USB drivers, 1000 * and should normally be the same as the module name. 1001 * @probe: Called to see if the driver is willing to manage a particular 1002 * interface on a device. If it is, probe returns zero and uses 1003 * usb_set_intfdata() to associate driver-specific data with the 1004 * interface. It may also use usb_set_interface() to specify the 1005 * appropriate altsetting. If unwilling to manage the interface, 1006 * return -ENODEV, if genuine IO errors occurred, an appropriate 1007 * negative errno value. 1008 * @disconnect: Called when the interface is no longer accessible, usually 1009 * because its device has been (or is being) disconnected or the 1010 * driver module is being unloaded. 1011 * @unlocked_ioctl: Used for drivers that want to talk to userspace through 1012 * the "usbfs" filesystem. This lets devices provide ways to 1013 * expose information to user space regardless of where they 1014 * do (or don't) show up otherwise in the filesystem. 1015 * @suspend: Called when the device is going to be suspended by the 1016 * system either from system sleep or runtime suspend context. The 1017 * return value will be ignored in system sleep context, so do NOT 1018 * try to continue using the device if suspend fails in this case. 1019 * Instead, let the resume or reset-resume routine recover from 1020 * the failure. 1021 * @resume: Called when the device is being resumed by the system. 1022 * @reset_resume: Called when the suspended device has been reset instead 1023 * of being resumed. 1024 * @pre_reset: Called by usb_reset_device() when the device is about to be 1025 * reset. This routine must not return until the driver has no active 1026 * URBs for the device, and no more URBs may be submitted until the 1027 * post_reset method is called. 1028 * @post_reset: Called by usb_reset_device() after the device 1029 * has been reset 1030 * @id_table: USB drivers use ID table to support hotplugging. 1031 * Export this with MODULE_DEVICE_TABLE(usb,...). This must be set 1032 * or your driver's probe function will never get called. 1033 * @dynids: used internally to hold the list of dynamically added device 1034 * ids for this driver. 1035 * @drvwrap: Driver-model core structure wrapper. 1036 * @no_dynamic_id: if set to 1, the USB core will not allow dynamic ids to be 1037 * added to this driver by preventing the sysfs file from being created. 1038 * @supports_autosuspend: if set to 0, the USB core will not allow autosuspend 1039 * for interfaces bound to this driver. 1040 * @soft_unbind: if set to 1, the USB core will not kill URBs and disable 1041 * endpoints before calling the driver's disconnect method. 1042 * @disable_hub_initiated_lpm: if set to 0, the USB core will not allow hubs 1043 * to initiate lower power link state transitions when an idle timeout 1044 * occurs. Device-initiated USB 3.0 link PM will still be allowed. 1045 * 1046 * USB interface drivers must provide a name, probe() and disconnect() 1047 * methods, and an id_table. Other driver fields are optional. 1048 * 1049 * The id_table is used in hotplugging. It holds a set of descriptors, 1050 * and specialized data may be associated with each entry. That table 1051 * is used by both user and kernel mode hotplugging support. 1052 * 1053 * The probe() and disconnect() methods are called in a context where 1054 * they can sleep, but they should avoid abusing the privilege. Most 1055 * work to connect to a device should be done when the device is opened, 1056 * and undone at the last close. The disconnect code needs to address 1057 * concurrency issues with respect to open() and close() methods, as 1058 * well as forcing all pending I/O requests to complete (by unlinking 1059 * them as necessary, and blocking until the unlinks complete). 1060 */ 1061 struct usb_driver { 1062 const char *name; 1063 1064 int (*probe) (struct usb_interface *intf, 1065 const struct usb_device_id *id); 1066 1067 void (*disconnect) (struct usb_interface *intf); 1068 1069 int (*unlocked_ioctl) (struct usb_interface *intf, unsigned int code, 1070 void *buf); 1071 1072 int (*suspend) (struct usb_interface *intf, pm_message_t message); 1073 int (*resume) (struct usb_interface *intf); 1074 int (*reset_resume)(struct usb_interface *intf); 1075 1076 int (*pre_reset)(struct usb_interface *intf); 1077 int (*post_reset)(struct usb_interface *intf); 1078 1079 const struct usb_device_id *id_table; 1080 1081 struct usb_dynids dynids; 1082 struct usbdrv_wrap drvwrap; 1083 unsigned int no_dynamic_id:1; 1084 unsigned int supports_autosuspend:1; 1085 unsigned int disable_hub_initiated_lpm:1; 1086 unsigned int soft_unbind:1; 1087 }; 1088 #define to_usb_driver(d) container_of(d, struct usb_driver, drvwrap.driver) 1089 1090 /** 1091 * struct usb_device_driver - identifies USB device driver to usbcore 1092 * @name: The driver name should be unique among USB drivers, 1093 * and should normally be the same as the module name. 1094 * @probe: Called to see if the driver is willing to manage a particular 1095 * device. If it is, probe returns zero and uses dev_set_drvdata() 1096 * to associate driver-specific data with the device. If unwilling 1097 * to manage the device, return a negative errno value. 1098 * @disconnect: Called when the device is no longer accessible, usually 1099 * because it has been (or is being) disconnected or the driver's 1100 * module is being unloaded. 1101 * @suspend: Called when the device is going to be suspended by the system. 1102 * @resume: Called when the device is being resumed by the system. 1103 * @drvwrap: Driver-model core structure wrapper. 1104 * @supports_autosuspend: if set to 0, the USB core will not allow autosuspend 1105 * for devices bound to this driver. 1106 * 1107 * USB drivers must provide all the fields listed above except drvwrap. 1108 */ 1109 struct usb_device_driver { 1110 const char *name; 1111 1112 int (*probe) (struct usb_device *udev); 1113 void (*disconnect) (struct usb_device *udev); 1114 1115 int (*suspend) (struct usb_device *udev, pm_message_t message); 1116 int (*resume) (struct usb_device *udev, pm_message_t message); 1117 struct usbdrv_wrap drvwrap; 1118 unsigned int supports_autosuspend:1; 1119 }; 1120 #define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \ 1121 drvwrap.driver) 1122 1123 extern struct bus_type usb_bus_type; 1124 1125 /** 1126 * struct usb_class_driver - identifies a USB driver that wants to use the USB major number 1127 * @name: the usb class device name for this driver. Will show up in sysfs. 1128 * @devnode: Callback to provide a naming hint for a possible 1129 * device node to create. 1130 * @fops: pointer to the struct file_operations of this driver. 1131 * @minor_base: the start of the minor range for this driver. 1132 * 1133 * This structure is used for the usb_register_dev() and 1134 * usb_unregister_dev() functions, to consolidate a number of the 1135 * parameters used for them. 1136 */ 1137 struct usb_class_driver { 1138 char *name; 1139 char *(*devnode)(struct device *dev, umode_t *mode); 1140 const struct file_operations *fops; 1141 int minor_base; 1142 }; 1143 1144 /* 1145 * use these in module_init()/module_exit() 1146 * and don't forget MODULE_DEVICE_TABLE(usb, ...) 1147 */ 1148 extern int usb_register_driver(struct usb_driver *, struct module *, 1149 const char *); 1150 1151 /* use a define to avoid include chaining to get THIS_MODULE & friends */ 1152 #define usb_register(driver) \ 1153 usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME) 1154 1155 extern void usb_deregister(struct usb_driver *); 1156 1157 /** 1158 * module_usb_driver() - Helper macro for registering a USB driver 1159 * @__usb_driver: usb_driver struct 1160 * 1161 * Helper macro for USB drivers which do not do anything special in module 1162 * init/exit. This eliminates a lot of boilerplate. Each module may only 1163 * use this macro once, and calling it replaces module_init() and module_exit() 1164 */ 1165 #define module_usb_driver(__usb_driver) \ 1166 module_driver(__usb_driver, usb_register, \ 1167 usb_deregister) 1168 1169 extern int usb_register_device_driver(struct usb_device_driver *, 1170 struct module *); 1171 extern void usb_deregister_device_driver(struct usb_device_driver *); 1172 1173 extern int usb_register_dev(struct usb_interface *intf, 1174 struct usb_class_driver *class_driver); 1175 extern void usb_deregister_dev(struct usb_interface *intf, 1176 struct usb_class_driver *class_driver); 1177 1178 extern int usb_disabled(void); 1179 1180 /* ----------------------------------------------------------------------- */ 1181 1182 /* 1183 * URB support, for asynchronous request completions 1184 */ 1185 1186 /* 1187 * urb->transfer_flags: 1188 * 1189 * Note: URB_DIR_IN/OUT is automatically set in usb_submit_urb(). 1190 */ 1191 #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */ 1192 #define URB_ISO_ASAP 0x0002 /* iso-only; use the first unexpired 1193 * slot in the schedule */ 1194 #define URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */ 1195 #define URB_NO_FSBR 0x0020 /* UHCI-specific */ 1196 #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */ 1197 #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt 1198 * needed */ 1199 #define URB_FREE_BUFFER 0x0100 /* Free transfer buffer with the URB */ 1200 1201 /* The following flags are used internally by usbcore and HCDs */ 1202 #define URB_DIR_IN 0x0200 /* Transfer from device to host */ 1203 #define URB_DIR_OUT 0 1204 #define URB_DIR_MASK URB_DIR_IN 1205 1206 #define URB_DMA_MAP_SINGLE 0x00010000 /* Non-scatter-gather mapping */ 1207 #define URB_DMA_MAP_PAGE 0x00020000 /* HCD-unsupported S-G */ 1208 #define URB_DMA_MAP_SG 0x00040000 /* HCD-supported S-G */ 1209 #define URB_MAP_LOCAL 0x00080000 /* HCD-local-memory mapping */ 1210 #define URB_SETUP_MAP_SINGLE 0x00100000 /* Setup packet DMA mapped */ 1211 #define URB_SETUP_MAP_LOCAL 0x00200000 /* HCD-local setup packet */ 1212 #define URB_DMA_SG_COMBINED 0x00400000 /* S-G entries were combined */ 1213 #define URB_ALIGNED_TEMP_BUFFER 0x00800000 /* Temp buffer was alloc'd */ 1214 1215 struct usb_iso_packet_descriptor { 1216 unsigned int offset; 1217 unsigned int length; /* expected length */ 1218 unsigned int actual_length; 1219 int status; 1220 }; 1221 1222 struct urb; 1223 1224 struct usb_anchor { 1225 struct list_head urb_list; 1226 wait_queue_head_t wait; 1227 spinlock_t lock; 1228 atomic_t suspend_wakeups; 1229 unsigned int poisoned:1; 1230 }; 1231 1232 static inline void init_usb_anchor(struct usb_anchor *anchor) 1233 { 1234 memset(anchor, 0, sizeof(*anchor)); 1235 INIT_LIST_HEAD(&anchor->urb_list); 1236 init_waitqueue_head(&anchor->wait); 1237 spin_lock_init(&anchor->lock); 1238 } 1239 1240 typedef void (*usb_complete_t)(struct urb *); 1241 1242 /** 1243 * struct urb - USB Request Block 1244 * @urb_list: For use by current owner of the URB. 1245 * @anchor_list: membership in the list of an anchor 1246 * @anchor: to anchor URBs to a common mooring 1247 * @ep: Points to the endpoint's data structure. Will eventually 1248 * replace @pipe. 1249 * @pipe: Holds endpoint number, direction, type, and more. 1250 * Create these values with the eight macros available; 1251 * usb_{snd,rcv}TYPEpipe(dev,endpoint), where the TYPE is "ctrl" 1252 * (control), "bulk", "int" (interrupt), or "iso" (isochronous). 1253 * For example usb_sndbulkpipe() or usb_rcvintpipe(). Endpoint 1254 * numbers range from zero to fifteen. Note that "in" endpoint two 1255 * is a different endpoint (and pipe) from "out" endpoint two. 1256 * The current configuration controls the existence, type, and 1257 * maximum packet size of any given endpoint. 1258 * @stream_id: the endpoint's stream ID for bulk streams 1259 * @dev: Identifies the USB device to perform the request. 1260 * @status: This is read in non-iso completion functions to get the 1261 * status of the particular request. ISO requests only use it 1262 * to tell whether the URB was unlinked; detailed status for 1263 * each frame is in the fields of the iso_frame-desc. 1264 * @transfer_flags: A variety of flags may be used to affect how URB 1265 * submission, unlinking, or operation are handled. Different 1266 * kinds of URB can use different flags. 1267 * @transfer_buffer: This identifies the buffer to (or from) which the I/O 1268 * request will be performed unless URB_NO_TRANSFER_DMA_MAP is set 1269 * (however, do not leave garbage in transfer_buffer even then). 1270 * This buffer must be suitable for DMA; allocate it with 1271 * kmalloc() or equivalent. For transfers to "in" endpoints, contents 1272 * of this buffer will be modified. This buffer is used for the data 1273 * stage of control transfers. 1274 * @transfer_dma: When transfer_flags includes URB_NO_TRANSFER_DMA_MAP, 1275 * the device driver is saying that it provided this DMA address, 1276 * which the host controller driver should use in preference to the 1277 * transfer_buffer. 1278 * @sg: scatter gather buffer list, the buffer size of each element in 1279 * the list (except the last) must be divisible by the endpoint's 1280 * max packet size if no_sg_constraint isn't set in 'struct usb_bus' 1281 * @num_mapped_sgs: (internal) number of mapped sg entries 1282 * @num_sgs: number of entries in the sg list 1283 * @transfer_buffer_length: How big is transfer_buffer. The transfer may 1284 * be broken up into chunks according to the current maximum packet 1285 * size for the endpoint, which is a function of the configuration 1286 * and is encoded in the pipe. When the length is zero, neither 1287 * transfer_buffer nor transfer_dma is used. 1288 * @actual_length: This is read in non-iso completion functions, and 1289 * it tells how many bytes (out of transfer_buffer_length) were 1290 * transferred. It will normally be the same as requested, unless 1291 * either an error was reported or a short read was performed. 1292 * The URB_SHORT_NOT_OK transfer flag may be used to make such 1293 * short reads be reported as errors. 1294 * @setup_packet: Only used for control transfers, this points to eight bytes 1295 * of setup data. Control transfers always start by sending this data 1296 * to the device. Then transfer_buffer is read or written, if needed. 1297 * @setup_dma: DMA pointer for the setup packet. The caller must not use 1298 * this field; setup_packet must point to a valid buffer. 1299 * @start_frame: Returns the initial frame for isochronous transfers. 1300 * @number_of_packets: Lists the number of ISO transfer buffers. 1301 * @interval: Specifies the polling interval for interrupt or isochronous 1302 * transfers. The units are frames (milliseconds) for full and low 1303 * speed devices, and microframes (1/8 millisecond) for highspeed 1304 * and SuperSpeed devices. 1305 * @error_count: Returns the number of ISO transfers that reported errors. 1306 * @context: For use in completion functions. This normally points to 1307 * request-specific driver context. 1308 * @complete: Completion handler. This URB is passed as the parameter to the 1309 * completion function. The completion function may then do what 1310 * it likes with the URB, including resubmitting or freeing it. 1311 * @iso_frame_desc: Used to provide arrays of ISO transfer buffers and to 1312 * collect the transfer status for each buffer. 1313 * 1314 * This structure identifies USB transfer requests. URBs must be allocated by 1315 * calling usb_alloc_urb() and freed with a call to usb_free_urb(). 1316 * Initialization may be done using various usb_fill_*_urb() functions. URBs 1317 * are submitted using usb_submit_urb(), and pending requests may be canceled 1318 * using usb_unlink_urb() or usb_kill_urb(). 1319 * 1320 * Data Transfer Buffers: 1321 * 1322 * Normally drivers provide I/O buffers allocated with kmalloc() or otherwise 1323 * taken from the general page pool. That is provided by transfer_buffer 1324 * (control requests also use setup_packet), and host controller drivers 1325 * perform a dma mapping (and unmapping) for each buffer transferred. Those 1326 * mapping operations can be expensive on some platforms (perhaps using a dma 1327 * bounce buffer or talking to an IOMMU), 1328 * although they're cheap on commodity x86 and ppc hardware. 1329 * 1330 * Alternatively, drivers may pass the URB_NO_TRANSFER_DMA_MAP transfer flag, 1331 * which tells the host controller driver that no such mapping is needed for 1332 * the transfer_buffer since 1333 * the device driver is DMA-aware. For example, a device driver might 1334 * allocate a DMA buffer with usb_alloc_coherent() or call usb_buffer_map(). 1335 * When this transfer flag is provided, host controller drivers will 1336 * attempt to use the dma address found in the transfer_dma 1337 * field rather than determining a dma address themselves. 1338 * 1339 * Note that transfer_buffer must still be set if the controller 1340 * does not support DMA (as indicated by bus.uses_dma) and when talking 1341 * to root hub. If you have to trasfer between highmem zone and the device 1342 * on such controller, create a bounce buffer or bail out with an error. 1343 * If transfer_buffer cannot be set (is in highmem) and the controller is DMA 1344 * capable, assign NULL to it, so that usbmon knows not to use the value. 1345 * The setup_packet must always be set, so it cannot be located in highmem. 1346 * 1347 * Initialization: 1348 * 1349 * All URBs submitted must initialize the dev, pipe, transfer_flags (may be 1350 * zero), and complete fields. All URBs must also initialize 1351 * transfer_buffer and transfer_buffer_length. They may provide the 1352 * URB_SHORT_NOT_OK transfer flag, indicating that short reads are 1353 * to be treated as errors; that flag is invalid for write requests. 1354 * 1355 * Bulk URBs may 1356 * use the URB_ZERO_PACKET transfer flag, indicating that bulk OUT transfers 1357 * should always terminate with a short packet, even if it means adding an 1358 * extra zero length packet. 1359 * 1360 * Control URBs must provide a valid pointer in the setup_packet field. 1361 * Unlike the transfer_buffer, the setup_packet may not be mapped for DMA 1362 * beforehand. 1363 * 1364 * Interrupt URBs must provide an interval, saying how often (in milliseconds 1365 * or, for highspeed devices, 125 microsecond units) 1366 * to poll for transfers. After the URB has been submitted, the interval 1367 * field reflects how the transfer was actually scheduled. 1368 * The polling interval may be more frequent than requested. 1369 * For example, some controllers have a maximum interval of 32 milliseconds, 1370 * while others support intervals of up to 1024 milliseconds. 1371 * Isochronous URBs also have transfer intervals. (Note that for isochronous 1372 * endpoints, as well as high speed interrupt endpoints, the encoding of 1373 * the transfer interval in the endpoint descriptor is logarithmic. 1374 * Device drivers must convert that value to linear units themselves.) 1375 * 1376 * If an isochronous endpoint queue isn't already running, the host 1377 * controller will schedule a new URB to start as soon as bandwidth 1378 * utilization allows. If the queue is running then a new URB will be 1379 * scheduled to start in the first transfer slot following the end of the 1380 * preceding URB, if that slot has not already expired. If the slot has 1381 * expired (which can happen when IRQ delivery is delayed for a long time), 1382 * the scheduling behavior depends on the URB_ISO_ASAP flag. If the flag 1383 * is clear then the URB will be scheduled to start in the expired slot, 1384 * implying that some of its packets will not be transferred; if the flag 1385 * is set then the URB will be scheduled in the first unexpired slot, 1386 * breaking the queue's synchronization. Upon URB completion, the 1387 * start_frame field will be set to the (micro)frame number in which the 1388 * transfer was scheduled. Ranges for frame counter values are HC-specific 1389 * and can go from as low as 256 to as high as 65536 frames. 1390 * 1391 * Isochronous URBs have a different data transfer model, in part because 1392 * the quality of service is only "best effort". Callers provide specially 1393 * allocated URBs, with number_of_packets worth of iso_frame_desc structures 1394 * at the end. Each such packet is an individual ISO transfer. Isochronous 1395 * URBs are normally queued, submitted by drivers to arrange that 1396 * transfers are at least double buffered, and then explicitly resubmitted 1397 * in completion handlers, so 1398 * that data (such as audio or video) streams at as constant a rate as the 1399 * host controller scheduler can support. 1400 * 1401 * Completion Callbacks: 1402 * 1403 * The completion callback is made in_interrupt(), and one of the first 1404 * things that a completion handler should do is check the status field. 1405 * The status field is provided for all URBs. It is used to report 1406 * unlinked URBs, and status for all non-ISO transfers. It should not 1407 * be examined before the URB is returned to the completion handler. 1408 * 1409 * The context field is normally used to link URBs back to the relevant 1410 * driver or request state. 1411 * 1412 * When the completion callback is invoked for non-isochronous URBs, the 1413 * actual_length field tells how many bytes were transferred. This field 1414 * is updated even when the URB terminated with an error or was unlinked. 1415 * 1416 * ISO transfer status is reported in the status and actual_length fields 1417 * of the iso_frame_desc array, and the number of errors is reported in 1418 * error_count. Completion callbacks for ISO transfers will normally 1419 * (re)submit URBs to ensure a constant transfer rate. 1420 * 1421 * Note that even fields marked "public" should not be touched by the driver 1422 * when the urb is owned by the hcd, that is, since the call to 1423 * usb_submit_urb() till the entry into the completion routine. 1424 */ 1425 struct urb { 1426 /* private: usb core and host controller only fields in the urb */ 1427 struct kref kref; /* reference count of the URB */ 1428 void *hcpriv; /* private data for host controller */ 1429 atomic_t use_count; /* concurrent submissions counter */ 1430 atomic_t reject; /* submissions will fail */ 1431 int unlinked; /* unlink error code */ 1432 1433 /* public: documented fields in the urb that can be used by drivers */ 1434 struct list_head urb_list; /* list head for use by the urb's 1435 * current owner */ 1436 struct list_head anchor_list; /* the URB may be anchored */ 1437 struct usb_anchor *anchor; 1438 struct usb_device *dev; /* (in) pointer to associated device */ 1439 struct usb_host_endpoint *ep; /* (internal) pointer to endpoint */ 1440 unsigned int pipe; /* (in) pipe information */ 1441 unsigned int stream_id; /* (in) stream ID */ 1442 int status; /* (return) non-ISO status */ 1443 unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/ 1444 void *transfer_buffer; /* (in) associated data buffer */ 1445 dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */ 1446 struct scatterlist *sg; /* (in) scatter gather buffer list */ 1447 int num_mapped_sgs; /* (internal) mapped sg entries */ 1448 int num_sgs; /* (in) number of entries in the sg list */ 1449 u32 transfer_buffer_length; /* (in) data buffer length */ 1450 u32 actual_length; /* (return) actual transfer length */ 1451 unsigned char *setup_packet; /* (in) setup packet (control only) */ 1452 dma_addr_t setup_dma; /* (in) dma addr for setup_packet */ 1453 int start_frame; /* (modify) start frame (ISO) */ 1454 int number_of_packets; /* (in) number of ISO packets */ 1455 int interval; /* (modify) transfer interval 1456 * (INT/ISO) */ 1457 int error_count; /* (return) number of ISO errors */ 1458 void *context; /* (in) context for completion */ 1459 usb_complete_t complete; /* (in) completion routine */ 1460 struct usb_iso_packet_descriptor iso_frame_desc[0]; 1461 /* (in) ISO ONLY */ 1462 }; 1463 1464 /* ----------------------------------------------------------------------- */ 1465 1466 /** 1467 * usb_fill_control_urb - initializes a control urb 1468 * @urb: pointer to the urb to initialize. 1469 * @dev: pointer to the struct usb_device for this urb. 1470 * @pipe: the endpoint pipe 1471 * @setup_packet: pointer to the setup_packet buffer 1472 * @transfer_buffer: pointer to the transfer buffer 1473 * @buffer_length: length of the transfer buffer 1474 * @complete_fn: pointer to the usb_complete_t function 1475 * @context: what to set the urb context to. 1476 * 1477 * Initializes a control urb with the proper information needed to submit 1478 * it to a device. 1479 */ 1480 static inline void usb_fill_control_urb(struct urb *urb, 1481 struct usb_device *dev, 1482 unsigned int pipe, 1483 unsigned char *setup_packet, 1484 void *transfer_buffer, 1485 int buffer_length, 1486 usb_complete_t complete_fn, 1487 void *context) 1488 { 1489 urb->dev = dev; 1490 urb->pipe = pipe; 1491 urb->setup_packet = setup_packet; 1492 urb->transfer_buffer = transfer_buffer; 1493 urb->transfer_buffer_length = buffer_length; 1494 urb->complete = complete_fn; 1495 urb->context = context; 1496 } 1497 1498 /** 1499 * usb_fill_bulk_urb - macro to help initialize a bulk urb 1500 * @urb: pointer to the urb to initialize. 1501 * @dev: pointer to the struct usb_device for this urb. 1502 * @pipe: the endpoint pipe 1503 * @transfer_buffer: pointer to the transfer buffer 1504 * @buffer_length: length of the transfer buffer 1505 * @complete_fn: pointer to the usb_complete_t function 1506 * @context: what to set the urb context to. 1507 * 1508 * Initializes a bulk urb with the proper information needed to submit it 1509 * to a device. 1510 */ 1511 static inline void usb_fill_bulk_urb(struct urb *urb, 1512 struct usb_device *dev, 1513 unsigned int pipe, 1514 void *transfer_buffer, 1515 int buffer_length, 1516 usb_complete_t complete_fn, 1517 void *context) 1518 { 1519 urb->dev = dev; 1520 urb->pipe = pipe; 1521 urb->transfer_buffer = transfer_buffer; 1522 urb->transfer_buffer_length = buffer_length; 1523 urb->complete = complete_fn; 1524 urb->context = context; 1525 } 1526 1527 /** 1528 * usb_fill_int_urb - macro to help initialize a interrupt urb 1529 * @urb: pointer to the urb to initialize. 1530 * @dev: pointer to the struct usb_device for this urb. 1531 * @pipe: the endpoint pipe 1532 * @transfer_buffer: pointer to the transfer buffer 1533 * @buffer_length: length of the transfer buffer 1534 * @complete_fn: pointer to the usb_complete_t function 1535 * @context: what to set the urb context to. 1536 * @interval: what to set the urb interval to, encoded like 1537 * the endpoint descriptor's bInterval value. 1538 * 1539 * Initializes a interrupt urb with the proper information needed to submit 1540 * it to a device. 1541 * 1542 * Note that High Speed and SuperSpeed interrupt endpoints use a logarithmic 1543 * encoding of the endpoint interval, and express polling intervals in 1544 * microframes (eight per millisecond) rather than in frames (one per 1545 * millisecond). 1546 * 1547 * Wireless USB also uses the logarithmic encoding, but specifies it in units of 1548 * 128us instead of 125us. For Wireless USB devices, the interval is passed 1549 * through to the host controller, rather than being translated into microframe 1550 * units. 1551 */ 1552 static inline void usb_fill_int_urb(struct urb *urb, 1553 struct usb_device *dev, 1554 unsigned int pipe, 1555 void *transfer_buffer, 1556 int buffer_length, 1557 usb_complete_t complete_fn, 1558 void *context, 1559 int interval) 1560 { 1561 urb->dev = dev; 1562 urb->pipe = pipe; 1563 urb->transfer_buffer = transfer_buffer; 1564 urb->transfer_buffer_length = buffer_length; 1565 urb->complete = complete_fn; 1566 urb->context = context; 1567 1568 if (dev->speed == USB_SPEED_HIGH || dev->speed == USB_SPEED_SUPER) { 1569 /* make sure interval is within allowed range */ 1570 interval = clamp(interval, 1, 16); 1571 1572 urb->interval = 1 << (interval - 1); 1573 } else { 1574 urb->interval = interval; 1575 } 1576 1577 urb->start_frame = -1; 1578 } 1579 1580 extern void usb_init_urb(struct urb *urb); 1581 extern struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags); 1582 extern void usb_free_urb(struct urb *urb); 1583 #define usb_put_urb usb_free_urb 1584 extern struct urb *usb_get_urb(struct urb *urb); 1585 extern int usb_submit_urb(struct urb *urb, gfp_t mem_flags); 1586 extern int usb_unlink_urb(struct urb *urb); 1587 extern void usb_kill_urb(struct urb *urb); 1588 extern void usb_poison_urb(struct urb *urb); 1589 extern void usb_unpoison_urb(struct urb *urb); 1590 extern void usb_block_urb(struct urb *urb); 1591 extern void usb_kill_anchored_urbs(struct usb_anchor *anchor); 1592 extern void usb_poison_anchored_urbs(struct usb_anchor *anchor); 1593 extern void usb_unpoison_anchored_urbs(struct usb_anchor *anchor); 1594 extern void usb_unlink_anchored_urbs(struct usb_anchor *anchor); 1595 extern void usb_anchor_suspend_wakeups(struct usb_anchor *anchor); 1596 extern void usb_anchor_resume_wakeups(struct usb_anchor *anchor); 1597 extern void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor); 1598 extern void usb_unanchor_urb(struct urb *urb); 1599 extern int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor, 1600 unsigned int timeout); 1601 extern struct urb *usb_get_from_anchor(struct usb_anchor *anchor); 1602 extern void usb_scuttle_anchored_urbs(struct usb_anchor *anchor); 1603 extern int usb_anchor_empty(struct usb_anchor *anchor); 1604 1605 #define usb_unblock_urb usb_unpoison_urb 1606 1607 /** 1608 * usb_urb_dir_in - check if an URB describes an IN transfer 1609 * @urb: URB to be checked 1610 * 1611 * Return: 1 if @urb describes an IN transfer (device-to-host), 1612 * otherwise 0. 1613 */ 1614 static inline int usb_urb_dir_in(struct urb *urb) 1615 { 1616 return (urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN; 1617 } 1618 1619 /** 1620 * usb_urb_dir_out - check if an URB describes an OUT transfer 1621 * @urb: URB to be checked 1622 * 1623 * Return: 1 if @urb describes an OUT transfer (host-to-device), 1624 * otherwise 0. 1625 */ 1626 static inline int usb_urb_dir_out(struct urb *urb) 1627 { 1628 return (urb->transfer_flags & URB_DIR_MASK) == URB_DIR_OUT; 1629 } 1630 1631 void *usb_alloc_coherent(struct usb_device *dev, size_t size, 1632 gfp_t mem_flags, dma_addr_t *dma); 1633 void usb_free_coherent(struct usb_device *dev, size_t size, 1634 void *addr, dma_addr_t dma); 1635 1636 #if 0 1637 struct urb *usb_buffer_map(struct urb *urb); 1638 void usb_buffer_dmasync(struct urb *urb); 1639 void usb_buffer_unmap(struct urb *urb); 1640 #endif 1641 1642 struct scatterlist; 1643 int usb_buffer_map_sg(const struct usb_device *dev, int is_in, 1644 struct scatterlist *sg, int nents); 1645 #if 0 1646 void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in, 1647 struct scatterlist *sg, int n_hw_ents); 1648 #endif 1649 void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in, 1650 struct scatterlist *sg, int n_hw_ents); 1651 1652 /*-------------------------------------------------------------------* 1653 * SYNCHRONOUS CALL SUPPORT * 1654 *-------------------------------------------------------------------*/ 1655 1656 extern int usb_control_msg(struct usb_device *dev, unsigned int pipe, 1657 __u8 request, __u8 requesttype, __u16 value, __u16 index, 1658 void *data, __u16 size, int timeout); 1659 extern int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe, 1660 void *data, int len, int *actual_length, int timeout); 1661 extern int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, 1662 void *data, int len, int *actual_length, 1663 int timeout); 1664 1665 /* wrappers around usb_control_msg() for the most common standard requests */ 1666 extern int usb_get_descriptor(struct usb_device *dev, unsigned char desctype, 1667 unsigned char descindex, void *buf, int size); 1668 extern int usb_get_status(struct usb_device *dev, 1669 int type, int target, void *data); 1670 extern int usb_string(struct usb_device *dev, int index, 1671 char *buf, size_t size); 1672 1673 /* wrappers that also update important state inside usbcore */ 1674 extern int usb_clear_halt(struct usb_device *dev, int pipe); 1675 extern int usb_reset_configuration(struct usb_device *dev); 1676 extern int usb_set_interface(struct usb_device *dev, int ifnum, int alternate); 1677 extern void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr); 1678 1679 /* this request isn't really synchronous, but it belongs with the others */ 1680 extern int usb_driver_set_configuration(struct usb_device *udev, int config); 1681 1682 /* choose and set configuration for device */ 1683 extern int usb_choose_configuration(struct usb_device *udev); 1684 extern int usb_set_configuration(struct usb_device *dev, int configuration); 1685 1686 /* 1687 * timeouts, in milliseconds, used for sending/receiving control messages 1688 * they typically complete within a few frames (msec) after they're issued 1689 * USB identifies 5 second timeouts, maybe more in a few cases, and a few 1690 * slow devices (like some MGE Ellipse UPSes) actually push that limit. 1691 */ 1692 #define USB_CTRL_GET_TIMEOUT 5000 1693 #define USB_CTRL_SET_TIMEOUT 5000 1694 1695 1696 /** 1697 * struct usb_sg_request - support for scatter/gather I/O 1698 * @status: zero indicates success, else negative errno 1699 * @bytes: counts bytes transferred. 1700 * 1701 * These requests are initialized using usb_sg_init(), and then are used 1702 * as request handles passed to usb_sg_wait() or usb_sg_cancel(). Most 1703 * members of the request object aren't for driver access. 1704 * 1705 * The status and bytecount values are valid only after usb_sg_wait() 1706 * returns. If the status is zero, then the bytecount matches the total 1707 * from the request. 1708 * 1709 * After an error completion, drivers may need to clear a halt condition 1710 * on the endpoint. 1711 */ 1712 struct usb_sg_request { 1713 int status; 1714 size_t bytes; 1715 1716 /* private: 1717 * members below are private to usbcore, 1718 * and are not provided for driver access! 1719 */ 1720 spinlock_t lock; 1721 1722 struct usb_device *dev; 1723 int pipe; 1724 1725 int entries; 1726 struct urb **urbs; 1727 1728 int count; 1729 struct completion complete; 1730 }; 1731 1732 int usb_sg_init( 1733 struct usb_sg_request *io, 1734 struct usb_device *dev, 1735 unsigned pipe, 1736 unsigned period, 1737 struct scatterlist *sg, 1738 int nents, 1739 size_t length, 1740 gfp_t mem_flags 1741 ); 1742 void usb_sg_cancel(struct usb_sg_request *io); 1743 void usb_sg_wait(struct usb_sg_request *io); 1744 1745 1746 /* ----------------------------------------------------------------------- */ 1747 1748 /* 1749 * For various legacy reasons, Linux has a small cookie that's paired with 1750 * a struct usb_device to identify an endpoint queue. Queue characteristics 1751 * are defined by the endpoint's descriptor. This cookie is called a "pipe", 1752 * an unsigned int encoded as: 1753 * 1754 * - direction: bit 7 (0 = Host-to-Device [Out], 1755 * 1 = Device-to-Host [In] ... 1756 * like endpoint bEndpointAddress) 1757 * - device address: bits 8-14 ... bit positions known to uhci-hcd 1758 * - endpoint: bits 15-18 ... bit positions known to uhci-hcd 1759 * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt, 1760 * 10 = control, 11 = bulk) 1761 * 1762 * Given the device address and endpoint descriptor, pipes are redundant. 1763 */ 1764 1765 /* NOTE: these are not the standard USB_ENDPOINT_XFER_* values!! */ 1766 /* (yet ... they're the values used by usbfs) */ 1767 #define PIPE_ISOCHRONOUS 0 1768 #define PIPE_INTERRUPT 1 1769 #define PIPE_CONTROL 2 1770 #define PIPE_BULK 3 1771 1772 #define usb_pipein(pipe) ((pipe) & USB_DIR_IN) 1773 #define usb_pipeout(pipe) (!usb_pipein(pipe)) 1774 1775 #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f) 1776 #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf) 1777 1778 #define usb_pipetype(pipe) (((pipe) >> 30) & 3) 1779 #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS) 1780 #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT) 1781 #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL) 1782 #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK) 1783 1784 static inline unsigned int __create_pipe(struct usb_device *dev, 1785 unsigned int endpoint) 1786 { 1787 return (dev->devnum << 8) | (endpoint << 15); 1788 } 1789 1790 /* Create various pipes... */ 1791 #define usb_sndctrlpipe(dev, endpoint) \ 1792 ((PIPE_CONTROL << 30) | __create_pipe(dev, endpoint)) 1793 #define usb_rcvctrlpipe(dev, endpoint) \ 1794 ((PIPE_CONTROL << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN) 1795 #define usb_sndisocpipe(dev, endpoint) \ 1796 ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev, endpoint)) 1797 #define usb_rcvisocpipe(dev, endpoint) \ 1798 ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN) 1799 #define usb_sndbulkpipe(dev, endpoint) \ 1800 ((PIPE_BULK << 30) | __create_pipe(dev, endpoint)) 1801 #define usb_rcvbulkpipe(dev, endpoint) \ 1802 ((PIPE_BULK << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN) 1803 #define usb_sndintpipe(dev, endpoint) \ 1804 ((PIPE_INTERRUPT << 30) | __create_pipe(dev, endpoint)) 1805 #define usb_rcvintpipe(dev, endpoint) \ 1806 ((PIPE_INTERRUPT << 30) | __create_pipe(dev, endpoint) | USB_DIR_IN) 1807 1808 static inline struct usb_host_endpoint * 1809 usb_pipe_endpoint(struct usb_device *dev, unsigned int pipe) 1810 { 1811 struct usb_host_endpoint **eps; 1812 eps = usb_pipein(pipe) ? dev->ep_in : dev->ep_out; 1813 return eps[usb_pipeendpoint(pipe)]; 1814 } 1815 1816 /*-------------------------------------------------------------------------*/ 1817 1818 static inline __u16 1819 usb_maxpacket(struct usb_device *udev, int pipe, int is_out) 1820 { 1821 struct usb_host_endpoint *ep; 1822 unsigned epnum = usb_pipeendpoint(pipe); 1823 1824 if (is_out) { 1825 WARN_ON(usb_pipein(pipe)); 1826 ep = udev->ep_out[epnum]; 1827 } else { 1828 WARN_ON(usb_pipeout(pipe)); 1829 ep = udev->ep_in[epnum]; 1830 } 1831 if (!ep) 1832 return 0; 1833 1834 /* NOTE: only 0x07ff bits are for packet size... */ 1835 return usb_endpoint_maxp(&ep->desc); 1836 } 1837 1838 /* ----------------------------------------------------------------------- */ 1839 1840 /* translate USB error codes to codes user space understands */ 1841 static inline int usb_translate_errors(int error_code) 1842 { 1843 switch (error_code) { 1844 case 0: 1845 case -ENOMEM: 1846 case -ENODEV: 1847 case -EOPNOTSUPP: 1848 return error_code; 1849 default: 1850 return -EIO; 1851 } 1852 } 1853 1854 /* Events from the usb core */ 1855 #define USB_DEVICE_ADD 0x0001 1856 #define USB_DEVICE_REMOVE 0x0002 1857 #define USB_BUS_ADD 0x0003 1858 #define USB_BUS_REMOVE 0x0004 1859 extern void usb_register_notify(struct notifier_block *nb); 1860 extern void usb_unregister_notify(struct notifier_block *nb); 1861 1862 /* debugfs stuff */ 1863 extern struct dentry *usb_debug_root; 1864 1865 #endif /* __KERNEL__ */ 1866 1867 #endif
1 /* 2 * This file holds USB constants and structures that are needed for 3 * USB device APIs. These are used by the USB device model, which is 4 * defined in chapter 9 of the USB 2.0 specification and in the 5 * Wireless USB 1.0 (spread around). Linux has several APIs in C that 6 * need these: 7 * 8 * - the master/host side Linux-USB kernel driver API; 9 * - the "usbfs" user space API; and 10 * - the Linux "gadget" slave/device/peripheral side driver API. 11 * 12 * USB 2.0 adds an additional "On The Go" (OTG) mode, which lets systems 13 * act either as a USB master/host or as a USB slave/device. That means 14 * the master and slave side APIs benefit from working well together. 15 * 16 * There's also "Wireless USB", using low power short range radios for 17 * peripheral interconnection but otherwise building on the USB framework. 18 * 19 * Note all descriptors are declared '__attribute__((packed))' so that: 20 * 21 * [a] they never get padded, either internally (USB spec writers 22 * probably handled that) or externally; 23 * 24 * [b] so that accessing bigger-than-a-bytes fields will never 25 * generate bus errors on any platform, even when the location of 26 * its descriptor inside a bundle isn't "naturally aligned", and 27 * 28 * [c] for consistency, removing all doubt even when it appears to 29 * someone that the two other points are non-issues for that 30 * particular descriptor type. 31 */ 32 33 #ifndef _UAPI__LINUX_USB_CH9_H 34 #define _UAPI__LINUX_USB_CH9_H 35 36 #include <linux/types.h> /* __u8 etc */ 37 #include <asm/byteorder.h> /* le16_to_cpu */ 38 39 /*-------------------------------------------------------------------------*/ 40 41 /* CONTROL REQUEST SUPPORT */ 42 43 /* 44 * USB directions 45 * 46 * This bit flag is used in endpoint descriptors' bEndpointAddress field. 47 * It's also one of three fields in control requests bRequestType. 48 */ 49 #define USB_DIR_OUT 0 /* to device */ 50 #define USB_DIR_IN 0x80 /* to host */ 51 52 /* 53 * USB types, the second of three bRequestType fields 54 */ 55 #define USB_TYPE_MASK (0x03 << 5) 56 #define USB_TYPE_STANDARD (0x00 << 5) 57 #define USB_TYPE_CLASS (0x01 << 5) 58 #define USB_TYPE_VENDOR (0x02 << 5) 59 #define USB_TYPE_RESERVED (0x03 << 5) 60 61 /* 62 * USB recipients, the third of three bRequestType fields 63 */ 64 #define USB_RECIP_MASK 0x1f 65 #define USB_RECIP_DEVICE 0x00 66 #define USB_RECIP_INTERFACE 0x01 67 #define USB_RECIP_ENDPOINT 0x02 68 #define USB_RECIP_OTHER 0x03 69 /* From Wireless USB 1.0 */ 70 #define USB_RECIP_PORT 0x04 71 #define USB_RECIP_RPIPE 0x05 72 73 /* 74 * Standard requests, for the bRequest field of a SETUP packet. 75 * 76 * These are qualified by the bRequestType field, so that for example 77 * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved 78 * by a GET_STATUS request. 79 */ 80 #define USB_REQ_GET_STATUS 0x00 81 #define USB_REQ_CLEAR_FEATURE 0x01 82 #define USB_REQ_SET_FEATURE 0x03 83 #define USB_REQ_SET_ADDRESS 0x05 84 #define USB_REQ_GET_DESCRIPTOR 0x06 85 #define USB_REQ_SET_DESCRIPTOR 0x07 86 #define USB_REQ_GET_CONFIGURATION 0x08 87 #define USB_REQ_SET_CONFIGURATION 0x09 88 #define USB_REQ_GET_INTERFACE 0x0A 89 #define USB_REQ_SET_INTERFACE 0x0B 90 #define USB_REQ_SYNCH_FRAME 0x0C 91 #define USB_REQ_SET_SEL 0x30 92 #define USB_REQ_SET_ISOCH_DELAY 0x31 93 94 #define USB_REQ_SET_ENCRYPTION 0x0D /* Wireless USB */ 95 #define USB_REQ_GET_ENCRYPTION 0x0E 96 #define USB_REQ_RPIPE_ABORT 0x0E 97 #define USB_REQ_SET_HANDSHAKE 0x0F 98 #define USB_REQ_RPIPE_RESET 0x0F 99 #define USB_REQ_GET_HANDSHAKE 0x10 100 #define USB_REQ_SET_CONNECTION 0x11 101 #define USB_REQ_SET_SECURITY_DATA 0x12 102 #define USB_REQ_GET_SECURITY_DATA 0x13 103 #define USB_REQ_SET_WUSB_DATA 0x14 104 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 105 #define USB_REQ_LOOPBACK_DATA_READ 0x16 106 #define USB_REQ_SET_INTERFACE_DS 0x17 107 108 /* The Link Power Management (LPM) ECN defines USB_REQ_TEST_AND_SET command, 109 * used by hubs to put ports into a new L1 suspend state, except that it 110 * forgot to define its number ... 111 */ 112 113 /* 114 * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and 115 * are read as a bit array returned by USB_REQ_GET_STATUS. (So there 116 * are at most sixteen features of each type.) Hubs may also support a 117 * new USB_REQ_TEST_AND_SET_FEATURE to put ports into L1 suspend. 118 */ 119 #define USB_DEVICE_SELF_POWERED 0 /* (read only) */ 120 #define USB_DEVICE_REMOTE_WAKEUP 1 /* dev may initiate wakeup */ 121 #define USB_DEVICE_TEST_MODE 2 /* (wired high speed only) */ 122 #define USB_DEVICE_BATTERY 2 /* (wireless) */ 123 #define USB_DEVICE_B_HNP_ENABLE 3 /* (otg) dev may initiate HNP */ 124 #define USB_DEVICE_WUSB_DEVICE 3 /* (wireless)*/ 125 #define USB_DEVICE_A_HNP_SUPPORT 4 /* (otg) RH port supports HNP */ 126 #define USB_DEVICE_A_ALT_HNP_SUPPORT 5 /* (otg) other RH port does */ 127 #define USB_DEVICE_DEBUG_MODE 6 /* (special devices only) */ 128 129 /* 130 * Test Mode Selectors 131 * See USB 2.0 spec Table 9-7 132 */ 133 #define TEST_J 1 134 #define TEST_K 2 135 #define TEST_SE0_NAK 3 136 #define TEST_PACKET 4 137 #define TEST_FORCE_EN 5 138 139 /* 140 * New Feature Selectors as added by USB 3.0 141 * See USB 3.0 spec Table 9-7 142 */ 143 #define USB_DEVICE_U1_ENABLE 48 /* dev may initiate U1 transition */ 144 #define USB_DEVICE_U2_ENABLE 49 /* dev may initiate U2 transition */ 145 #define USB_DEVICE_LTM_ENABLE 50 /* dev may send LTM */ 146 #define USB_INTRF_FUNC_SUSPEND 0 /* function suspend */ 147 148 #define USB_INTR_FUNC_SUSPEND_OPT_MASK 0xFF00 149 /* 150 * Suspend Options, Table 9-8 USB 3.0 spec 151 */ 152 #define USB_INTRF_FUNC_SUSPEND_LP (1 << (8 + 0)) 153 #define USB_INTRF_FUNC_SUSPEND_RW (1 << (8 + 1)) 154 155 /* 156 * Interface status, Figure 9-5 USB 3.0 spec 157 */ 158 #define USB_INTRF_STAT_FUNC_RW_CAP 1 159 #define USB_INTRF_STAT_FUNC_RW 2 160 161 #define USB_ENDPOINT_HALT 0 /* IN/OUT will STALL */ 162 163 /* Bit array elements as returned by the USB_REQ_GET_STATUS request. */ 164 #define USB_DEV_STAT_U1_ENABLED 2 /* transition into U1 state */ 165 #define USB_DEV_STAT_U2_ENABLED 3 /* transition into U2 state */ 166 #define USB_DEV_STAT_LTM_ENABLED 4 /* Latency tolerance messages */ 167 168 /** 169 * struct usb_ctrlrequest - SETUP data for a USB device control request 170 * @bRequestType: matches the USB bmRequestType field 171 * @bRequest: matches the USB bRequest field 172 * @wValue: matches the USB wValue field (le16 byte order) 173 * @wIndex: matches the USB wIndex field (le16 byte order) 174 * @wLength: matches the USB wLength field (le16 byte order) 175 * 176 * This structure is used to send control requests to a USB device. It matches 177 * the different fields of the USB 2.0 Spec section 9.3, table 9-2. See the 178 * USB spec for a fuller description of the different fields, and what they are 179 * used for. 180 * 181 * Note that the driver for any interface can issue control requests. 182 * For most devices, interfaces don't coordinate with each other, so 183 * such requests may be made at any time. 184 */ 185 struct usb_ctrlrequest { 186 __u8 bRequestType; 187 __u8 bRequest; 188 __le16 wValue; 189 __le16 wIndex; 190 __le16 wLength; 191 } __attribute__ ((packed)); 192 193 /*-------------------------------------------------------------------------*/ 194 195 /* 196 * STANDARD DESCRIPTORS ... as returned by GET_DESCRIPTOR, or 197 * (rarely) accepted by SET_DESCRIPTOR. 198 * 199 * Note that all multi-byte values here are encoded in little endian 200 * byte order "on the wire". Within the kernel and when exposed 201 * through the Linux-USB APIs, they are not converted to cpu byte 202 * order; it is the responsibility of the client code to do this. 203 * The single exception is when device and configuration descriptors (but 204 * not other descriptors) are read from usbfs (i.e. /proc/bus/usb/BBB/DDD); 205 * in this case the fields are converted to host endianness by the kernel. 206 */ 207 208 /* 209 * Descriptor types ... USB 2.0 spec table 9.5 210 */ 211 #define USB_DT_DEVICE 0x01 212 #define USB_DT_CONFIG 0x02 213 #define USB_DT_STRING 0x03 214 #define USB_DT_INTERFACE 0x04 215 #define USB_DT_ENDPOINT 0x05 216 #define USB_DT_DEVICE_QUALIFIER 0x06 217 #define USB_DT_OTHER_SPEED_CONFIG 0x07 218 #define USB_DT_INTERFACE_POWER 0x08 219 /* these are from a minor usb 2.0 revision (ECN) */ 220 #define USB_DT_OTG 0x09 221 #define USB_DT_DEBUG 0x0a 222 #define USB_DT_INTERFACE_ASSOCIATION 0x0b 223 /* these are from the Wireless USB spec */ 224 #define USB_DT_SECURITY 0x0c 225 #define USB_DT_KEY 0x0d 226 #define USB_DT_ENCRYPTION_TYPE 0x0e 227 #define USB_DT_BOS 0x0f 228 #define USB_DT_DEVICE_CAPABILITY 0x10 229 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 230 #define USB_DT_WIRE_ADAPTER 0x21 231 #define USB_DT_RPIPE 0x22 232 #define USB_DT_CS_RADIO_CONTROL 0x23 233 /* From the T10 UAS specification */ 234 #define USB_DT_PIPE_USAGE 0x24 235 /* From the USB 3.0 spec */ 236 #define USB_DT_SS_ENDPOINT_COMP 0x30 237 238 /* Conventional codes for class-specific descriptors. The convention is 239 * defined in the USB "Common Class" Spec (3.11). Individual class specs 240 * are authoritative for their usage, not the "common class" writeup. 241 */ 242 #define USB_DT_CS_DEVICE (USB_TYPE_CLASS | USB_DT_DEVICE) 243 #define USB_DT_CS_CONFIG (USB_TYPE_CLASS | USB_DT_CONFIG) 244 #define USB_DT_CS_STRING (USB_TYPE_CLASS | USB_DT_STRING) 245 #define USB_DT_CS_INTERFACE (USB_TYPE_CLASS | USB_DT_INTERFACE) 246 #define USB_DT_CS_ENDPOINT (USB_TYPE_CLASS | USB_DT_ENDPOINT) 247 248 /* All standard descriptors have these 2 fields at the beginning */ 249 struct usb_descriptor_header { 250 __u8 bLength; 251 __u8 bDescriptorType; 252 } __attribute__ ((packed)); 253 254 255 /*-------------------------------------------------------------------------*/ 256 257 /* USB_DT_DEVICE: Device descriptor */ 258 struct usb_device_descriptor { 259 __u8 bLength; 260 __u8 bDescriptorType; 261 262 __le16 bcdUSB; 263 __u8 bDeviceClass; 264 __u8 bDeviceSubClass; 265 __u8 bDeviceProtocol; 266 __u8 bMaxPacketSize0; 267 __le16 idVendor; 268 __le16 idProduct; 269 __le16 bcdDevice; 270 __u8 iManufacturer; 271 __u8 iProduct; 272 __u8 iSerialNumber; 273 __u8 bNumConfigurations; 274 } __attribute__ ((packed)); 275 276 #define USB_DT_DEVICE_SIZE 18 277 278 279 /* 280 * Device and/or Interface Class codes 281 * as found in bDeviceClass or bInterfaceClass 282 * and defined by www.usb.org documents 283 */ 284 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ 285 #define USB_CLASS_AUDIO 1 286 #define USB_CLASS_COMM 2 287 #define USB_CLASS_HID 3 288 #define USB_CLASS_PHYSICAL 5 289 #define USB_CLASS_STILL_IMAGE 6 290 #define USB_CLASS_PRINTER 7 291 #define USB_CLASS_MASS_STORAGE 8 292 #define USB_CLASS_HUB 9 293 #define USB_CLASS_CDC_DATA 0x0a 294 #define USB_CLASS_CSCID 0x0b /* chip+ smart card */ 295 #define USB_CLASS_CONTENT_SEC 0x0d /* content security */ 296 #define USB_CLASS_VIDEO 0x0e 297 #define USB_CLASS_WIRELESS_CONTROLLER 0xe0 298 #define USB_CLASS_MISC 0xef 299 #define USB_CLASS_APP_SPEC 0xfe 300 #define USB_CLASS_VENDOR_SPEC 0xff 301 302 #define USB_SUBCLASS_VENDOR_SPEC 0xff 303 304 /*-------------------------------------------------------------------------*/ 305 306 /* USB_DT_CONFIG: Configuration descriptor information. 307 * 308 * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the 309 * descriptor type is different. Highspeed-capable devices can look 310 * different depending on what speed they're currently running. Only 311 * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG 312 * descriptors. 313 */ 314 struct usb_config_descriptor { 315 __u8 bLength; 316 __u8 bDescriptorType; 317 318 __le16 wTotalLength; 319 __u8 bNumInterfaces; 320 __u8 bConfigurationValue; 321 __u8 iConfiguration; 322 __u8 bmAttributes; 323 __u8 bMaxPower; 324 } __attribute__ ((packed)); 325 326 #define USB_DT_CONFIG_SIZE 9 327 328 /* from config descriptor bmAttributes */ 329 #define USB_CONFIG_ATT_ONE (1 << 7) /* must be set */ 330 #define USB_CONFIG_ATT_SELFPOWER (1 << 6) /* self powered */ 331 #define USB_CONFIG_ATT_WAKEUP (1 << 5) /* can wakeup */ 332 #define USB_CONFIG_ATT_BATTERY (1 << 4) /* battery powered */ 333 334 /*-------------------------------------------------------------------------*/ 335 336 /* USB_DT_STRING: String descriptor */ 337 struct usb_string_descriptor { 338 __u8 bLength; 339 __u8 bDescriptorType; 340 341 __le16 wData[1]; /* UTF-16LE encoded */ 342 } __attribute__ ((packed)); 343 344 /* note that "string" zero is special, it holds language codes that 345 * the device supports, not Unicode characters. 346 */ 347 348 /*-------------------------------------------------------------------------*/ 349 350 /* USB_DT_INTERFACE: Interface descriptor */ 351 struct usb_interface_descriptor { 352 __u8 bLength; 353 __u8 bDescriptorType; 354 355 __u8 bInterfaceNumber; 356 __u8 bAlternateSetting; 357 __u8 bNumEndpoints; 358 __u8 bInterfaceClass; 359 __u8 bInterfaceSubClass; 360 __u8 bInterfaceProtocol; 361 __u8 iInterface; 362 } __attribute__ ((packed)); 363 364 #define USB_DT_INTERFACE_SIZE 9 365 366 /*-------------------------------------------------------------------------*/ 367 368 /* USB_DT_ENDPOINT: Endpoint descriptor */ 369 struct usb_endpoint_descriptor { 370 __u8 bLength; 371 __u8 bDescriptorType; 372 373 __u8 bEndpointAddress; 374 __u8 bmAttributes; 375 __le16 wMaxPacketSize; 376 __u8 bInterval; 377 378 /* NOTE: these two are _only_ in audio endpoints. */ 379 /* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */ 380 __u8 bRefresh; 381 __u8 bSynchAddress; 382 } __attribute__ ((packed)); 383 384 #define USB_DT_ENDPOINT_SIZE 7 385 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 386 387 388 /* 389 * Endpoints 390 */ 391 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */ 392 #define USB_ENDPOINT_DIR_MASK 0x80 393 394 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */ 395 #define USB_ENDPOINT_XFER_CONTROL 0 396 #define USB_ENDPOINT_XFER_ISOC 1 397 #define USB_ENDPOINT_XFER_BULK 2 398 #define USB_ENDPOINT_XFER_INT 3 399 #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80 400 401 /* The USB 3.0 spec redefines bits 5:4 of bmAttributes as interrupt ep type. */ 402 #define USB_ENDPOINT_INTRTYPE 0x30 403 #define USB_ENDPOINT_INTR_PERIODIC (0 << 4) 404 #define USB_ENDPOINT_INTR_NOTIFICATION (1 << 4) 405 406 #define USB_ENDPOINT_SYNCTYPE 0x0c 407 #define USB_ENDPOINT_SYNC_NONE (0 << 2) 408 #define USB_ENDPOINT_SYNC_ASYNC (1 << 2) 409 #define USB_ENDPOINT_SYNC_ADAPTIVE (2 << 2) 410 #define USB_ENDPOINT_SYNC_SYNC (3 << 2) 411 412 #define USB_ENDPOINT_USAGE_MASK 0x30 413 #define USB_ENDPOINT_USAGE_DATA 0x00 414 #define USB_ENDPOINT_USAGE_FEEDBACK 0x10 415 #define USB_ENDPOINT_USAGE_IMPLICIT_FB 0x20 /* Implicit feedback Data endpoint */ 416 417 /*-------------------------------------------------------------------------*/ 418 419 /** 420 * usb_endpoint_num - get the endpoint's number 421 * @epd: endpoint to be checked 422 * 423 * Returns @epd's number: 0 to 15. 424 */ 425 static inline int usb_endpoint_num(const struct usb_endpoint_descriptor *epd) 426 { 427 return epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 428 } 429 430 /** 431 * usb_endpoint_type - get the endpoint's transfer type 432 * @epd: endpoint to be checked 433 * 434 * Returns one of USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT} according 435 * to @epd's transfer type. 436 */ 437 static inline int usb_endpoint_type(const struct usb_endpoint_descriptor *epd) 438 { 439 return epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 440 } 441 442 /** 443 * usb_endpoint_dir_in - check if the endpoint has IN direction 444 * @epd: endpoint to be checked 445 * 446 * Returns true if the endpoint is of type IN, otherwise it returns false. 447 */ 448 static inline int usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd) 449 { 450 return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN); 451 } 452 453 /** 454 * usb_endpoint_dir_out - check if the endpoint has OUT direction 455 * @epd: endpoint to be checked 456 * 457 * Returns true if the endpoint is of type OUT, otherwise it returns false. 458 */ 459 static inline int usb_endpoint_dir_out( 460 const struct usb_endpoint_descriptor *epd) 461 { 462 return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT); 463 } 464 465 /** 466 * usb_endpoint_xfer_bulk - check if the endpoint has bulk transfer type 467 * @epd: endpoint to be checked 468 * 469 * Returns true if the endpoint is of type bulk, otherwise it returns false. 470 */ 471 static inline int usb_endpoint_xfer_bulk( 472 const struct usb_endpoint_descriptor *epd) 473 { 474 return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 475 USB_ENDPOINT_XFER_BULK); 476 } 477 478 /** 479 * usb_endpoint_xfer_control - check if the endpoint has control transfer type 480 * @epd: endpoint to be checked 481 * 482 * Returns true if the endpoint is of type control, otherwise it returns false. 483 */ 484 static inline int usb_endpoint_xfer_control( 485 const struct usb_endpoint_descriptor *epd) 486 { 487 return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 488 USB_ENDPOINT_XFER_CONTROL); 489 } 490 491 /** 492 * usb_endpoint_xfer_int - check if the endpoint has interrupt transfer type 493 * @epd: endpoint to be checked 494 * 495 * Returns true if the endpoint is of type interrupt, otherwise it returns 496 * false. 497 */ 498 static inline int usb_endpoint_xfer_int( 499 const struct usb_endpoint_descriptor *epd) 500 { 501 return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 502 USB_ENDPOINT_XFER_INT); 503 } 504 505 /** 506 * usb_endpoint_xfer_isoc - check if the endpoint has isochronous transfer type 507 * @epd: endpoint to be checked 508 * 509 * Returns true if the endpoint is of type isochronous, otherwise it returns 510 * false. 511 */ 512 static inline int usb_endpoint_xfer_isoc( 513 const struct usb_endpoint_descriptor *epd) 514 { 515 return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 516 USB_ENDPOINT_XFER_ISOC); 517 } 518 519 /** 520 * usb_endpoint_is_bulk_in - check if the endpoint is bulk IN 521 * @epd: endpoint to be checked 522 * 523 * Returns true if the endpoint has bulk transfer type and IN direction, 524 * otherwise it returns false. 525 */ 526 static inline int usb_endpoint_is_bulk_in( 527 const struct usb_endpoint_descriptor *epd) 528 { 529 return usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_in(epd); 530 } 531 532 /** 533 * usb_endpoint_is_bulk_out - check if the endpoint is bulk OUT 534 * @epd: endpoint to be checked 535 * 536 * Returns true if the endpoint has bulk transfer type and OUT direction, 537 * otherwise it returns false. 538 */ 539 static inline int usb_endpoint_is_bulk_out( 540 const struct usb_endpoint_descriptor *epd) 541 { 542 return usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_out(epd); 543 } 544 545 /** 546 * usb_endpoint_is_int_in - check if the endpoint is interrupt IN 547 * @epd: endpoint to be checked 548 * 549 * Returns true if the endpoint has interrupt transfer type and IN direction, 550 * otherwise it returns false. 551 */ 552 static inline int usb_endpoint_is_int_in( 553 const struct usb_endpoint_descriptor *epd) 554 { 555 return usb_endpoint_xfer_int(epd) && usb_endpoint_dir_in(epd); 556 } 557 558 /** 559 * usb_endpoint_is_int_out - check if the endpoint is interrupt OUT 560 * @epd: endpoint to be checked 561 * 562 * Returns true if the endpoint has interrupt transfer type and OUT direction, 563 * otherwise it returns false. 564 */ 565 static inline int usb_endpoint_is_int_out( 566 const struct usb_endpoint_descriptor *epd) 567 { 568 return usb_endpoint_xfer_int(epd) && usb_endpoint_dir_out(epd); 569 } 570 571 /** 572 * usb_endpoint_is_isoc_in - check if the endpoint is isochronous IN 573 * @epd: endpoint to be checked 574 * 575 * Returns true if the endpoint has isochronous transfer type and IN direction, 576 * otherwise it returns false. 577 */ 578 static inline int usb_endpoint_is_isoc_in( 579 const struct usb_endpoint_descriptor *epd) 580 { 581 return usb_endpoint_xfer_isoc(epd) && usb_endpoint_dir_in(epd); 582 } 583 584 /** 585 * usb_endpoint_is_isoc_out - check if the endpoint is isochronous OUT 586 * @epd: endpoint to be checked 587 * 588 * Returns true if the endpoint has isochronous transfer type and OUT direction, 589 * otherwise it returns false. 590 */ 591 static inline int usb_endpoint_is_isoc_out( 592 const struct usb_endpoint_descriptor *epd) 593 { 594 return usb_endpoint_xfer_isoc(epd) && usb_endpoint_dir_out(epd); 595 } 596 597 /** 598 * usb_endpoint_maxp - get endpoint's max packet size 599 * @epd: endpoint to be checked 600 * 601 * Returns @epd's max packet 602 */ 603 static inline int usb_endpoint_maxp(const struct usb_endpoint_descriptor *epd) 604 { 605 return __le16_to_cpu(epd->wMaxPacketSize); 606 } 607 608 static inline int usb_endpoint_interrupt_type( 609 const struct usb_endpoint_descriptor *epd) 610 { 611 return epd->bmAttributes & USB_ENDPOINT_INTRTYPE; 612 } 613 614 /*-------------------------------------------------------------------------*/ 615 616 /* USB_DT_SS_ENDPOINT_COMP: SuperSpeed Endpoint Companion descriptor */ 617 struct usb_ss_ep_comp_descriptor { 618 __u8 bLength; 619 __u8 bDescriptorType; 620 621 __u8 bMaxBurst; 622 __u8 bmAttributes; 623 __le16 wBytesPerInterval; 624 } __attribute__ ((packed)); 625 626 #define USB_DT_SS_EP_COMP_SIZE 6 627 628 /* Bits 4:0 of bmAttributes if this is a bulk endpoint */ 629 static inline int 630 usb_ss_max_streams(const struct usb_ss_ep_comp_descriptor *comp) 631 { 632 int max_streams; 633 634 if (!comp) 635 return 0; 636 637 max_streams = comp->bmAttributes & 0x1f; 638 639 if (!max_streams) 640 return 0; 641 642 max_streams = 1 << max_streams; 643 644 return max_streams; 645 } 646 647 /* Bits 1:0 of bmAttributes if this is an isoc endpoint */ 648 #define USB_SS_MULT(p) (1 + ((p) & 0x3)) 649 650 /*-------------------------------------------------------------------------*/ 651 652 /* USB_DT_DEVICE_QUALIFIER: Device Qualifier descriptor */ 653 struct usb_qualifier_descriptor { 654 __u8 bLength; 655 __u8 bDescriptorType; 656 657 __le16 bcdUSB; 658 __u8 bDeviceClass; 659 __u8 bDeviceSubClass; 660 __u8 bDeviceProtocol; 661 __u8 bMaxPacketSize0; 662 __u8 bNumConfigurations; 663 __u8 bRESERVED; 664 } __attribute__ ((packed)); 665 666 667 /*-------------------------------------------------------------------------*/ 668 669 /* USB_DT_OTG (from OTG 1.0a supplement) */ 670 struct usb_otg_descriptor { 671 __u8 bLength; 672 __u8 bDescriptorType; 673 674 __u8 bmAttributes; /* support for HNP, SRP, etc */ 675 } __attribute__ ((packed)); 676 677 /* from usb_otg_descriptor.bmAttributes */ 678 #define USB_OTG_SRP (1 << 0) 679 #define USB_OTG_HNP (1 << 1) /* swap host/device roles */ 680 681 /*-------------------------------------------------------------------------*/ 682 683 /* USB_DT_DEBUG: for special highspeed devices, replacing serial console */ 684 struct usb_debug_descriptor { 685 __u8 bLength; 686 __u8 bDescriptorType; 687 688 /* bulk endpoints with 8 byte maxpacket */ 689 __u8 bDebugInEndpoint; 690 __u8 bDebugOutEndpoint; 691 } __attribute__((packed)); 692 693 /*-------------------------------------------------------------------------*/ 694 695 /* USB_DT_INTERFACE_ASSOCIATION: groups interfaces */ 696 struct usb_interface_assoc_descriptor { 697 __u8 bLength; 698 __u8 bDescriptorType; 699 700 __u8 bFirstInterface; 701 __u8 bInterfaceCount; 702 __u8 bFunctionClass; 703 __u8 bFunctionSubClass; 704 __u8 bFunctionProtocol; 705 __u8 iFunction; 706 } __attribute__ ((packed)); 707 708 709 /*-------------------------------------------------------------------------*/ 710 711 /* USB_DT_SECURITY: group of wireless security descriptors, including 712 * encryption types available for setting up a CC/association. 713 */ 714 struct usb_security_descriptor { 715 __u8 bLength; 716 __u8 bDescriptorType; 717 718 __le16 wTotalLength; 719 __u8 bNumEncryptionTypes; 720 } __attribute__((packed)); 721 722 /*-------------------------------------------------------------------------*/ 723 724 /* USB_DT_KEY: used with {GET,SET}_SECURITY_DATA; only public keys 725 * may be retrieved. 726 */ 727 struct usb_key_descriptor { 728 __u8 bLength; 729 __u8 bDescriptorType; 730 731 __u8 tTKID[3]; 732 __u8 bReserved; 733 __u8 bKeyData[0]; 734 } __attribute__((packed)); 735 736 /*-------------------------------------------------------------------------*/ 737 738 /* USB_DT_ENCRYPTION_TYPE: bundled in DT_SECURITY groups */ 739 struct usb_encryption_descriptor { 740 __u8 bLength; 741 __u8 bDescriptorType; 742 743 __u8 bEncryptionType; 744 #define USB_ENC_TYPE_UNSECURE 0 745 #define USB_ENC_TYPE_WIRED 1 /* non-wireless mode */ 746 #define USB_ENC_TYPE_CCM_1 2 /* aes128/cbc session */ 747 #define USB_ENC_TYPE_RSA_1 3 /* rsa3072/sha1 auth */ 748 __u8 bEncryptionValue; /* use in SET_ENCRYPTION */ 749 __u8 bAuthKeyIndex; 750 } __attribute__((packed)); 751 752 753 /*-------------------------------------------------------------------------*/ 754 755 /* USB_DT_BOS: group of device-level capabilities */ 756 struct usb_bos_descriptor { 757 __u8 bLength; 758 __u8 bDescriptorType; 759 760 __le16 wTotalLength; 761 __u8 bNumDeviceCaps; 762 } __attribute__((packed)); 763 764 #define USB_DT_BOS_SIZE 5 765 /*-------------------------------------------------------------------------*/ 766 767 /* USB_DT_DEVICE_CAPABILITY: grouped with BOS */ 768 struct usb_dev_cap_header { 769 __u8 bLength; 770 __u8 bDescriptorType; 771 __u8 bDevCapabilityType; 772 } __attribute__((packed)); 773 774 #define USB_CAP_TYPE_WIRELESS_USB 1 775 776 struct usb_wireless_cap_descriptor { /* Ultra Wide Band */ 777 __u8 bLength; 778 __u8 bDescriptorType; 779 __u8 bDevCapabilityType; 780 781 __u8 bmAttributes; 782 #define USB_WIRELESS_P2P_DRD (1 << 1) 783 #define USB_WIRELESS_BEACON_MASK (3 << 2) 784 #define USB_WIRELESS_BEACON_SELF (1 << 2) 785 #define USB_WIRELESS_BEACON_DIRECTED (2 << 2) 786 #define USB_WIRELESS_BEACON_NONE (3 << 2) 787 __le16 wPHYRates; /* bit rates, Mbps */ 788 #define USB_WIRELESS_PHY_53 (1 << 0) /* always set */ 789 #define USB_WIRELESS_PHY_80 (1 << 1) 790 #define USB_WIRELESS_PHY_107 (1 << 2) /* always set */ 791 #define USB_WIRELESS_PHY_160 (1 << 3) 792 #define USB_WIRELESS_PHY_200 (1 << 4) /* always set */ 793 #define USB_WIRELESS_PHY_320 (1 << 5) 794 #define USB_WIRELESS_PHY_400 (1 << 6) 795 #define USB_WIRELESS_PHY_480 (1 << 7) 796 __u8 bmTFITXPowerInfo; /* TFI power levels */ 797 __u8 bmFFITXPowerInfo; /* FFI power levels */ 798 __le16 bmBandGroup; 799 __u8 bReserved; 800 } __attribute__((packed)); 801 802 /* USB 2.0 Extension descriptor */ 803 #define USB_CAP_TYPE_EXT 2 804 805 struct usb_ext_cap_descriptor { /* Link Power Management */ 806 __u8 bLength; 807 __u8 bDescriptorType; 808 __u8 bDevCapabilityType; 809 __le32 bmAttributes; 810 #define USB_LPM_SUPPORT (1 << 1) /* supports LPM */ 811 #define USB_BESL_SUPPORT (1 << 2) /* supports BESL */ 812 #define USB_BESL_BASELINE_VALID (1 << 3) /* Baseline BESL valid*/ 813 #define USB_BESL_DEEP_VALID (1 << 4) /* Deep BESL valid */ 814 #define USB_GET_BESL_BASELINE(p) (((p) & (0xf << 8)) >> 8) 815 #define USB_GET_BESL_DEEP(p) (((p) & (0xf << 12)) >> 12) 816 } __attribute__((packed)); 817 818 #define USB_DT_USB_EXT_CAP_SIZE 7 819 820 /* 821 * SuperSpeed USB Capability descriptor: Defines the set of SuperSpeed USB 822 * specific device level capabilities 823 */ 824 #define USB_SS_CAP_TYPE 3 825 struct usb_ss_cap_descriptor { /* Link Power Management */ 826 __u8 bLength; 827 __u8 bDescriptorType; 828 __u8 bDevCapabilityType; 829 __u8 bmAttributes; 830 #define USB_LTM_SUPPORT (1 << 1) /* supports LTM */ 831 __le16 wSpeedSupported; 832 #define USB_LOW_SPEED_OPERATION (1) /* Low speed operation */ 833 #define USB_FULL_SPEED_OPERATION (1 << 1) /* Full speed operation */ 834 #define USB_HIGH_SPEED_OPERATION (1 << 2) /* High speed operation */ 835 #define USB_5GBPS_OPERATION (1 << 3) /* Operation at 5Gbps */ 836 __u8 bFunctionalitySupport; 837 __u8 bU1devExitLat; 838 __le16 bU2DevExitLat; 839 } __attribute__((packed)); 840 841 #define USB_DT_USB_SS_CAP_SIZE 10 842 843 /* 844 * Container ID Capability descriptor: Defines the instance unique ID used to 845 * identify the instance across all operating modes 846 */ 847 #define CONTAINER_ID_TYPE 4 848 struct usb_ss_container_id_descriptor { 849 __u8 bLength; 850 __u8 bDescriptorType; 851 __u8 bDevCapabilityType; 852 __u8 bReserved; 853 __u8 ContainerID[16]; /* 128-bit number */ 854 } __attribute__((packed)); 855 856 #define USB_DT_USB_SS_CONTN_ID_SIZE 20 857 /*-------------------------------------------------------------------------*/ 858 859 /* USB_DT_WIRELESS_ENDPOINT_COMP: companion descriptor associated with 860 * each endpoint descriptor for a wireless device 861 */ 862 struct usb_wireless_ep_comp_descriptor { 863 __u8 bLength; 864 __u8 bDescriptorType; 865 866 __u8 bMaxBurst; 867 __u8 bMaxSequence; 868 __le16 wMaxStreamDelay; 869 __le16 wOverTheAirPacketSize; 870 __u8 bOverTheAirInterval; 871 __u8 bmCompAttributes; 872 #define USB_ENDPOINT_SWITCH_MASK 0x03 /* in bmCompAttributes */ 873 #define USB_ENDPOINT_SWITCH_NO 0 874 #define USB_ENDPOINT_SWITCH_SWITCH 1 875 #define USB_ENDPOINT_SWITCH_SCALE 2 876 } __attribute__((packed)); 877 878 /*-------------------------------------------------------------------------*/ 879 880 /* USB_REQ_SET_HANDSHAKE is a four-way handshake used between a wireless 881 * host and a device for connection set up, mutual authentication, and 882 * exchanging short lived session keys. The handshake depends on a CC. 883 */ 884 struct usb_handshake { 885 __u8 bMessageNumber; 886 __u8 bStatus; 887 __u8 tTKID[3]; 888 __u8 bReserved; 889 __u8 CDID[16]; 890 __u8 nonce[16]; 891 __u8 MIC[8]; 892 } __attribute__((packed)); 893 894 /*-------------------------------------------------------------------------*/ 895 896 /* USB_REQ_SET_CONNECTION modifies or revokes a connection context (CC). 897 * A CC may also be set up using non-wireless secure channels (including 898 * wired USB!), and some devices may support CCs with multiple hosts. 899 */ 900 struct usb_connection_context { 901 __u8 CHID[16]; /* persistent host id */ 902 __u8 CDID[16]; /* device id (unique w/in host context) */ 903 __u8 CK[16]; /* connection key */ 904 } __attribute__((packed)); 905 906 /*-------------------------------------------------------------------------*/ 907 908 /* USB 2.0 defines three speeds, here's how Linux identifies them */ 909 910 enum usb_device_speed { 911 USB_SPEED_UNKNOWN = 0, /* enumerating */ 912 USB_SPEED_LOW, USB_SPEED_FULL, /* usb 1.1 */ 913 USB_SPEED_HIGH, /* usb 2.0 */ 914 USB_SPEED_WIRELESS, /* wireless (usb 2.5) */ 915 USB_SPEED_SUPER, /* usb 3.0 */ 916 }; 917 918 919 enum usb_device_state { 920 /* NOTATTACHED isn't in the USB spec, and this state acts 921 * the same as ATTACHED ... but it's clearer this way. 922 */ 923 USB_STATE_NOTATTACHED = 0, 924 925 /* chapter 9 and authentication (wireless) device states */ 926 USB_STATE_ATTACHED, 927 USB_STATE_POWERED, /* wired */ 928 USB_STATE_RECONNECTING, /* auth */ 929 USB_STATE_UNAUTHENTICATED, /* auth */ 930 USB_STATE_DEFAULT, /* limited function */ 931 USB_STATE_ADDRESS, 932 USB_STATE_CONFIGURED, /* most functions */ 933 934 USB_STATE_SUSPENDED 935 936 /* NOTE: there are actually four different SUSPENDED 937 * states, returning to POWERED, DEFAULT, ADDRESS, or 938 * CONFIGURED respectively when SOF tokens flow again. 939 * At this level there's no difference between L1 and L2 940 * suspend states. (L2 being original USB 1.1 suspend.) 941 */ 942 }; 943 944 enum usb3_link_state { 945 USB3_LPM_U0 = 0, 946 USB3_LPM_U1, 947 USB3_LPM_U2, 948 USB3_LPM_U3 949 }; 950 951 /* 952 * A U1 timeout of 0x0 means the parent hub will reject any transitions to U1. 953 * 0xff means the parent hub will accept transitions to U1, but will not 954 * initiate a transition. 955 * 956 * A U1 timeout of 0x1 to 0x7F also causes the hub to initiate a transition to 957 * U1 after that many microseconds. Timeouts of 0x80 to 0xFE are reserved 958 * values. 959 * 960 * A U2 timeout of 0x0 means the parent hub will reject any transitions to U2. 961 * 0xff means the parent hub will accept transitions to U2, but will not 962 * initiate a transition. 963 * 964 * A U2 timeout of 0x1 to 0xFE also causes the hub to initiate a transition to 965 * U2 after N*256 microseconds. Therefore a U2 timeout value of 0x1 means a U2 966 * idle timer of 256 microseconds, 0x2 means 512 microseconds, 0xFE means 967 * 65.024ms. 968 */ 969 #define USB3_LPM_DISABLED 0x0 970 #define USB3_LPM_U1_MAX_TIMEOUT 0x7F 971 #define USB3_LPM_U2_MAX_TIMEOUT 0xFE 972 #define USB3_LPM_DEVICE_INITIATED 0xFF 973 974 struct usb_set_sel_req { 975 __u8 u1_sel; 976 __u8 u1_pel; 977 __le16 u2_sel; 978 __le16 u2_pel; 979 } __attribute__ ((packed)); 980 981 /* 982 * The Set System Exit Latency control transfer provides one byte each for 983 * U1 SEL and U1 PEL, so the max exit latency is 0xFF. U2 SEL and U2 PEL each 984 * are two bytes long. 985 */ 986 #define USB3_LPM_MAX_U1_SEL_PEL 0xFF 987 #define USB3_LPM_MAX_U2_SEL_PEL 0xFFFF 988 989 /*-------------------------------------------------------------------------*/ 990 991 /* 992 * As per USB compliance update, a device that is actively drawing 993 * more than 100mA from USB must report itself as bus-powered in 994 * the GetStatus(DEVICE) call. 995 * http://compliance.usb.org/index.asp?UpdateFile=Electrical&Format=Standard#34 996 */ 997 #define USB_SELF_POWER_VBUS_MAX_DRAW 100 998 999 #endif /* _UAPI__LINUX_USB_CH9_H */

Here is an explanation of a rule violation arisen while checking your driver against a corresponding kernel.

Note that it may be false positive, i.e. there isn't a real error indeed. Please analyze a given error trace and related source code to understand whether there is an error in your driver.

Error trace column contains a path on which the given rule is violated. You can expand/collapse some entity classes by clicking on corresponding checkboxes in a main menu or in an advanced Others menu. Also you can expand/collapse each particular entity by clicking on +/-. In hovering on some entities you can see some tips. Also the error trace is bound with related source code. Line numbers may be shown as links on the left. You can click on them to open corresponding lines in source code.

Source code column contains a content of files related with the error trace. There is source code of your driver (note that there are some LDV modifications at the end), kernel headers and rule model. Tabs show a currently opened file and other available files. In hovering on them you can see full file names. On clicking a corresponding file content will be shown.

Kernel Module Rule Verifier Verdict Status Timestamp Bug report
linux-3.16-rc1.tar.xz drivers/media/usb/usbtv/usbtv.ko 132_1a BLAST Bug Fixed 2015-03-11 11:29:25 L0155

Comment

L0155

[Home]