Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: gadget: printer: add configfs support

Add support for configfs interface so that f_printer can be used as a
component of usb gadgets composed with it.

Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>

authored by

Andrzej Pietrasiewicz and committed by
Felipe Balbi
ee1cd515 a2a8e48a

+204 -2
+9
Documentation/ABI/testing/configfs-usb-gadget-printer
··· 1 + What: /config/usb-gadget/gadget/functions/printer.name 2 + Date: Apr 2015 3 + KernelVersion: 4.1 4 + Description: 5 + The attributes: 6 + 7 + pnp_string - Data to be passed to the host in pnp string 8 + q_len - Number of requests per endpoint 9 +
+47
Documentation/usb/gadget-testing.txt
··· 19 19 16. UAC1 function 20 20 17. UAC2 function 21 21 18. UVC function 22 + 19. PRINTER function 22 23 23 24 24 25 1. ACM function ··· 727 726 http://www.spinics.net/lists/linux-usb/msg99220.html 728 727 729 728 host: luvcview -f yuv 729 + 730 + 19. PRINTER function 731 + ==================== 732 + 733 + The function is provided by usb_f_printer.ko module. 734 + 735 + Function-specific configfs interface 736 + ------------------------------------ 737 + 738 + The function name to use when creating the function directory is "printer". 739 + The printer function provides these attributes in its function directory: 740 + 741 + pnp_string - Data to be passed to the host in pnp string 742 + q_len - Number of requests per endpoint 743 + 744 + Testing the PRINTER function 745 + ---------------------------- 746 + 747 + The most basic testing: 748 + 749 + device: run the gadget 750 + # ls -l /devices/virtual/usb_printer_gadget/ 751 + 752 + should show g_printer<number>. 753 + 754 + If udev is active, then /dev/g_printer<number> should appear automatically. 755 + 756 + host: 757 + 758 + If udev is active, then e.g. /dev/usb/lp0 should appear. 759 + 760 + host->device transmission: 761 + 762 + device: 763 + # cat /dev/g_printer<number> 764 + host: 765 + # cat > /dev/usb/lp0 766 + 767 + device->host transmission: 768 + 769 + # cat > /dev/g_printer<number> 770 + host: 771 + # cat /dev/usb/lp0 772 + 773 + More advanced testing can be done with the prn_example 774 + described in Documentation/usb/gadget-printer.txt.
+13
drivers/usb/gadget/Kconfig
··· 437 437 device. It provides a userspace API to process UVC control requests 438 438 and stream video data to the host. 439 439 440 + config USB_CONFIGFS_F_PRINTER 441 + bool "Printer function" 442 + select USB_F_PRINTER 443 + help 444 + The Printer function channels data between the USB host and a 445 + userspace program driving the print engine. The user space 446 + program reads and writes the device file /dev/g_printer<X> to 447 + receive or send printer data. It can use ioctl calls to 448 + the device file to get or set printer status. 449 + 450 + For more information, see Documentation/usb/gadget_printer.txt 451 + which includes sample code for accessing the device file. 452 + 440 453 source "drivers/usb/gadget/legacy/Kconfig" 441 454 442 455 endchoice
+128 -2
drivers/usb/gadget/function/f_printer.c
··· 1140 1140 spin_unlock_irqrestore(&dev->lock, flags); 1141 1141 } 1142 1142 1143 + static inline struct f_printer_opts 1144 + *to_f_printer_opts(struct config_item *item) 1145 + { 1146 + return container_of(to_config_group(item), struct f_printer_opts, 1147 + func_inst.group); 1148 + } 1149 + 1150 + CONFIGFS_ATTR_STRUCT(f_printer_opts); 1151 + CONFIGFS_ATTR_OPS(f_printer_opts); 1152 + 1153 + static void printer_attr_release(struct config_item *item) 1154 + { 1155 + struct f_printer_opts *opts = to_f_printer_opts(item); 1156 + 1157 + usb_put_function_instance(&opts->func_inst); 1158 + } 1159 + 1160 + static struct configfs_item_operations printer_item_ops = { 1161 + .release = printer_attr_release, 1162 + .show_attribute = f_printer_opts_attr_show, 1163 + .store_attribute = f_printer_opts_attr_store, 1164 + }; 1165 + 1166 + static ssize_t f_printer_opts_pnp_string_show(struct f_printer_opts *opts, 1167 + char *page) 1168 + { 1169 + int result; 1170 + 1171 + mutex_lock(&opts->lock); 1172 + result = strlcpy(page, opts->pnp_string + 2, PNP_STRING_LEN - 2); 1173 + mutex_unlock(&opts->lock); 1174 + 1175 + return result; 1176 + } 1177 + 1178 + static ssize_t f_printer_opts_pnp_string_store(struct f_printer_opts *opts, 1179 + const char *page, size_t len) 1180 + { 1181 + int result, l; 1182 + 1183 + mutex_lock(&opts->lock); 1184 + result = strlcpy(opts->pnp_string + 2, page, PNP_STRING_LEN - 2); 1185 + l = strlen(opts->pnp_string + 2) + 2; 1186 + opts->pnp_string[0] = (l >> 8) & 0xFF; 1187 + opts->pnp_string[1] = l & 0xFF; 1188 + mutex_unlock(&opts->lock); 1189 + 1190 + return result; 1191 + } 1192 + 1193 + static struct f_printer_opts_attribute f_printer_opts_pnp_string = 1194 + __CONFIGFS_ATTR(pnp_string, S_IRUGO | S_IWUSR, 1195 + f_printer_opts_pnp_string_show, 1196 + f_printer_opts_pnp_string_store); 1197 + 1198 + static ssize_t f_printer_opts_q_len_show(struct f_printer_opts *opts, 1199 + char *page) 1200 + { 1201 + int result; 1202 + 1203 + mutex_lock(&opts->lock); 1204 + result = sprintf(page, "%d\n", opts->q_len); 1205 + mutex_unlock(&opts->lock); 1206 + 1207 + return result; 1208 + } 1209 + 1210 + static ssize_t f_printer_opts_q_len_store(struct f_printer_opts *opts, 1211 + const char *page, size_t len) 1212 + { 1213 + int ret; 1214 + u16 num; 1215 + 1216 + mutex_lock(&opts->lock); 1217 + if (opts->refcnt) { 1218 + ret = -EBUSY; 1219 + goto end; 1220 + } 1221 + 1222 + ret = kstrtou16(page, 0, &num); 1223 + if (ret) 1224 + goto end; 1225 + 1226 + if (num > 65535) { 1227 + ret = -EINVAL; 1228 + goto end; 1229 + } 1230 + 1231 + opts->q_len = (unsigned)num; 1232 + ret = len; 1233 + end: 1234 + mutex_unlock(&opts->lock); 1235 + return ret; 1236 + } 1237 + 1238 + static struct f_printer_opts_attribute f_printer_opts_q_len = 1239 + __CONFIGFS_ATTR(q_len, S_IRUGO | S_IWUSR, f_printer_opts_q_len_show, 1240 + f_printer_opts_q_len_store); 1241 + 1242 + static struct configfs_attribute *printer_attrs[] = { 1243 + &f_printer_opts_pnp_string.attr, 1244 + &f_printer_opts_q_len.attr, 1245 + NULL, 1246 + }; 1247 + 1248 + static struct config_item_type printer_func_type = { 1249 + .ct_item_ops = &printer_item_ops, 1250 + .ct_attrs = printer_attrs, 1251 + .ct_owner = THIS_MODULE, 1252 + }; 1253 + 1143 1254 static inline int gprinter_get_minor(void) 1144 1255 { 1145 1256 return ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL); ··· 1291 1180 if (!opts) 1292 1181 return ERR_PTR(-ENOMEM); 1293 1182 1183 + mutex_init(&opts->lock); 1294 1184 opts->func_inst.free_func_inst = gprinter_free_inst; 1295 1185 ret = &opts->func_inst; 1296 1186 ··· 1313 1201 if (idr_is_empty(&printer_ida.idr)) 1314 1202 gprinter_cleanup(); 1315 1203 } 1204 + config_group_init_type_name(&opts->func_inst.group, "", 1205 + &printer_func_type); 1316 1206 1317 1207 unlock: 1318 1208 mutex_unlock(&printer_ida_lock); ··· 1324 1210 static void gprinter_free(struct usb_function *f) 1325 1211 { 1326 1212 struct printer_dev *dev = func_to_printer(f); 1213 + struct f_printer_opts *opts; 1327 1214 1215 + opts = container_of(f->fi, struct f_printer_opts, func_inst); 1328 1216 kfree(dev); 1217 + mutex_lock(&opts->lock); 1218 + --opts->refcnt; 1219 + mutex_unlock(&opts->lock); 1329 1220 } 1330 1221 1331 1222 static void printer_func_unbind(struct usb_configuration *c, ··· 1384 1265 1385 1266 opts = container_of(fi, struct f_printer_opts, func_inst); 1386 1267 1387 - if (opts->minor >= minors) 1268 + mutex_lock(&opts->lock); 1269 + if (opts->minor >= minors) { 1270 + mutex_unlock(&opts->lock); 1388 1271 return ERR_PTR(-ENOENT); 1272 + } 1389 1273 1390 1274 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1391 - if (!dev) 1275 + if (!dev) { 1276 + mutex_unlock(&opts->lock); 1392 1277 return ERR_PTR(-ENOMEM); 1278 + } 1393 1279 1280 + ++opts->refcnt; 1394 1281 dev->minor = opts->minor; 1395 1282 dev->pnp_string = opts->pnp_string; 1396 1283 dev->q_len = opts->q_len; 1284 + mutex_unlock(&opts->lock); 1397 1285 1398 1286 dev->function.name = "printer"; 1399 1287 dev->function.bind = printer_func_bind;
+7
drivers/usb/gadget/function/u_printer.h
··· 25 25 int minor; 26 26 char pnp_string[PNP_STRING_LEN]; 27 27 unsigned q_len; 28 + 29 + /* 30 + * Protect the data from concurrent access by read/write 31 + * and create symlink/remove symlink 32 + */ 33 + struct mutex lock; 34 + int refcnt; 28 35 }; 29 36 30 37 #endif /* U_PRINTER_H */