usbip_common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3. * Copyright (C) 2015-2016 Samsung Electronics
  4. * Krzysztof Opasiak <k.opasiak@samsung.com>
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. * USA.
  20. */
  21. #include <asm/byteorder.h>
  22. #include <linux/file.h>
  23. #include <linux/fs.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/stat.h>
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <net/sock.h>
  30. #include "usbip_common.h"
  31. #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
  32. #define DRIVER_DESC "USB/IP Core"
  33. #ifdef CONFIG_USBIP_DEBUG
  34. unsigned long usbip_debug_flag = 0xffffffff;
  35. #else
  36. unsigned long usbip_debug_flag;
  37. #endif
  38. EXPORT_SYMBOL_GPL(usbip_debug_flag);
  39. module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
  40. MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
  41. /* FIXME */
  42. struct device_attribute dev_attr_usbip_debug;
  43. EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
  44. static ssize_t usbip_debug_show(struct device *dev,
  45. struct device_attribute *attr, char *buf)
  46. {
  47. return sprintf(buf, "%lx\n", usbip_debug_flag);
  48. }
  49. static ssize_t usbip_debug_store(struct device *dev,
  50. struct device_attribute *attr, const char *buf,
  51. size_t count)
  52. {
  53. if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
  54. return -EINVAL;
  55. return count;
  56. }
  57. DEVICE_ATTR_RW(usbip_debug);
  58. static void usbip_dump_buffer(char *buff, int bufflen)
  59. {
  60. print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
  61. buff, bufflen, false);
  62. }
  63. static void usbip_dump_pipe(unsigned int p)
  64. {
  65. unsigned char type = usb_pipetype(p);
  66. unsigned char ep = usb_pipeendpoint(p);
  67. unsigned char dev = usb_pipedevice(p);
  68. unsigned char dir = usb_pipein(p);
  69. pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
  70. switch (type) {
  71. case PIPE_ISOCHRONOUS:
  72. pr_debug("ISO\n");
  73. break;
  74. case PIPE_INTERRUPT:
  75. pr_debug("INT\n");
  76. break;
  77. case PIPE_CONTROL:
  78. pr_debug("CTRL\n");
  79. break;
  80. case PIPE_BULK:
  81. pr_debug("BULK\n");
  82. break;
  83. default:
  84. pr_debug("ERR\n");
  85. break;
  86. }
  87. }
  88. static void usbip_dump_usb_device(struct usb_device *udev)
  89. {
  90. struct device *dev = &udev->dev;
  91. int i;
  92. dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)",
  93. udev->devnum, udev->devpath, usb_speed_string(udev->speed));
  94. pr_debug("tt %p, ttport %d\n", udev->tt, udev->ttport);
  95. dev_dbg(dev, " ");
  96. for (i = 0; i < 16; i++)
  97. pr_debug(" %2u", i);
  98. pr_debug("\n");
  99. dev_dbg(dev, " toggle0(IN) :");
  100. for (i = 0; i < 16; i++)
  101. pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
  102. pr_debug("\n");
  103. dev_dbg(dev, " toggle1(OUT):");
  104. for (i = 0; i < 16; i++)
  105. pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
  106. pr_debug("\n");
  107. dev_dbg(dev, " epmaxp_in :");
  108. for (i = 0; i < 16; i++) {
  109. if (udev->ep_in[i])
  110. pr_debug(" %2u",
  111. le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
  112. }
  113. pr_debug("\n");
  114. dev_dbg(dev, " epmaxp_out :");
  115. for (i = 0; i < 16; i++) {
  116. if (udev->ep_out[i])
  117. pr_debug(" %2u",
  118. le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
  119. }
  120. pr_debug("\n");
  121. dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
  122. dev_dbg(dev,
  123. "descriptor %p, config %p, actconfig %p, rawdescriptors %p\n",
  124. &udev->descriptor, udev->config,
  125. udev->actconfig, udev->rawdescriptors);
  126. dev_dbg(dev, "have_langid %d, string_langid %d\n",
  127. udev->have_langid, udev->string_langid);
  128. dev_dbg(dev, "maxchild %d\n", udev->maxchild);
  129. }
  130. static void usbip_dump_request_type(__u8 rt)
  131. {
  132. switch (rt & USB_RECIP_MASK) {
  133. case USB_RECIP_DEVICE:
  134. pr_debug("DEVICE");
  135. break;
  136. case USB_RECIP_INTERFACE:
  137. pr_debug("INTERF");
  138. break;
  139. case USB_RECIP_ENDPOINT:
  140. pr_debug("ENDPOI");
  141. break;
  142. case USB_RECIP_OTHER:
  143. pr_debug("OTHER ");
  144. break;
  145. default:
  146. pr_debug("------");
  147. break;
  148. }
  149. }
  150. static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
  151. {
  152. if (!cmd) {
  153. pr_debug(" : null pointer\n");
  154. return;
  155. }
  156. pr_debug(" ");
  157. pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
  158. cmd->bRequestType, cmd->bRequest,
  159. cmd->wValue, cmd->wIndex, cmd->wLength);
  160. pr_debug("\n ");
  161. if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  162. pr_debug("STANDARD ");
  163. switch (cmd->bRequest) {
  164. case USB_REQ_GET_STATUS:
  165. pr_debug("GET_STATUS\n");
  166. break;
  167. case USB_REQ_CLEAR_FEATURE:
  168. pr_debug("CLEAR_FEAT\n");
  169. break;
  170. case USB_REQ_SET_FEATURE:
  171. pr_debug("SET_FEAT\n");
  172. break;
  173. case USB_REQ_SET_ADDRESS:
  174. pr_debug("SET_ADDRRS\n");
  175. break;
  176. case USB_REQ_GET_DESCRIPTOR:
  177. pr_debug("GET_DESCRI\n");
  178. break;
  179. case USB_REQ_SET_DESCRIPTOR:
  180. pr_debug("SET_DESCRI\n");
  181. break;
  182. case USB_REQ_GET_CONFIGURATION:
  183. pr_debug("GET_CONFIG\n");
  184. break;
  185. case USB_REQ_SET_CONFIGURATION:
  186. pr_debug("SET_CONFIG\n");
  187. break;
  188. case USB_REQ_GET_INTERFACE:
  189. pr_debug("GET_INTERF\n");
  190. break;
  191. case USB_REQ_SET_INTERFACE:
  192. pr_debug("SET_INTERF\n");
  193. break;
  194. case USB_REQ_SYNCH_FRAME:
  195. pr_debug("SYNC_FRAME\n");
  196. break;
  197. default:
  198. pr_debug("REQ(%02X)\n", cmd->bRequest);
  199. break;
  200. }
  201. usbip_dump_request_type(cmd->bRequestType);
  202. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
  203. pr_debug("CLASS\n");
  204. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  205. pr_debug("VENDOR\n");
  206. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
  207. pr_debug("RESERVED\n");
  208. }
  209. }
  210. void usbip_dump_urb(struct urb *urb)
  211. {
  212. struct device *dev;
  213. if (!urb) {
  214. pr_debug("urb: null pointer!!\n");
  215. return;
  216. }
  217. if (!urb->dev) {
  218. pr_debug("urb->dev: null pointer!!\n");
  219. return;
  220. }
  221. dev = &urb->dev->dev;
  222. dev_dbg(dev, " urb :%p\n", urb);
  223. dev_dbg(dev, " dev :%p\n", urb->dev);
  224. usbip_dump_usb_device(urb->dev);
  225. dev_dbg(dev, " pipe :%08x ", urb->pipe);
  226. usbip_dump_pipe(urb->pipe);
  227. dev_dbg(dev, " status :%d\n", urb->status);
  228. dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
  229. dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
  230. dev_dbg(dev, " transfer_buffer_length:%d\n",
  231. urb->transfer_buffer_length);
  232. dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
  233. dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
  234. if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
  235. usbip_dump_usb_ctrlrequest(
  236. (struct usb_ctrlrequest *)urb->setup_packet);
  237. dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
  238. dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
  239. dev_dbg(dev, " interval :%d\n", urb->interval);
  240. dev_dbg(dev, " error_count :%d\n", urb->error_count);
  241. dev_dbg(dev, " context :%p\n", urb->context);
  242. dev_dbg(dev, " complete :%p\n", urb->complete);
  243. }
  244. EXPORT_SYMBOL_GPL(usbip_dump_urb);
  245. void usbip_dump_header(struct usbip_header *pdu)
  246. {
  247. pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
  248. pdu->base.command,
  249. pdu->base.seqnum,
  250. pdu->base.devid,
  251. pdu->base.direction,
  252. pdu->base.ep);
  253. switch (pdu->base.command) {
  254. case USBIP_CMD_SUBMIT:
  255. pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
  256. pdu->u.cmd_submit.transfer_flags,
  257. pdu->u.cmd_submit.transfer_buffer_length,
  258. pdu->u.cmd_submit.start_frame,
  259. pdu->u.cmd_submit.number_of_packets,
  260. pdu->u.cmd_submit.interval);
  261. break;
  262. case USBIP_CMD_UNLINK:
  263. pr_debug("USBIP_CMD_UNLINK: seq %u\n",
  264. pdu->u.cmd_unlink.seqnum);
  265. break;
  266. case USBIP_RET_SUBMIT:
  267. pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
  268. pdu->u.ret_submit.status,
  269. pdu->u.ret_submit.actual_length,
  270. pdu->u.ret_submit.start_frame,
  271. pdu->u.ret_submit.number_of_packets,
  272. pdu->u.ret_submit.error_count);
  273. break;
  274. case USBIP_RET_UNLINK:
  275. pr_debug("USBIP_RET_UNLINK: status %d\n",
  276. pdu->u.ret_unlink.status);
  277. break;
  278. default:
  279. /* NOT REACHED */
  280. pr_err("unknown command\n");
  281. break;
  282. }
  283. }
  284. EXPORT_SYMBOL_GPL(usbip_dump_header);
  285. /* Receive data over TCP/IP. */
  286. int usbip_recv(struct socket *sock, void *buf, int size)
  287. {
  288. int result;
  289. struct kvec iov = {.iov_base = buf, .iov_len = size};
  290. struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
  291. int total = 0;
  292. iov_iter_kvec(&msg.msg_iter, READ|ITER_KVEC, &iov, 1, size);
  293. usbip_dbg_xmit("enter\n");
  294. if (!sock || !buf || !size) {
  295. pr_err("invalid arg, sock %p buff %p size %d\n", sock, buf,
  296. size);
  297. return -EINVAL;
  298. }
  299. do {
  300. int sz = msg_data_left(&msg);
  301. sock->sk->sk_allocation = GFP_NOIO;
  302. result = sock_recvmsg(sock, &msg, MSG_WAITALL);
  303. if (result <= 0) {
  304. pr_debug("receive sock %p buf %p size %u ret %d total %d\n",
  305. sock, buf + total, sz, result, total);
  306. goto err;
  307. }
  308. total += result;
  309. } while (msg_data_left(&msg));
  310. if (usbip_dbg_flag_xmit) {
  311. if (!in_interrupt())
  312. pr_debug("%-10s:", current->comm);
  313. else
  314. pr_debug("interrupt :");
  315. pr_debug("receiving....\n");
  316. usbip_dump_buffer(buf, size);
  317. pr_debug("received, osize %d ret %d size %zd total %d\n",
  318. size, result, msg_data_left(&msg), total);
  319. }
  320. return total;
  321. err:
  322. return result;
  323. }
  324. EXPORT_SYMBOL_GPL(usbip_recv);
  325. /* there may be more cases to tweak the flags. */
  326. static unsigned int tweak_transfer_flags(unsigned int flags)
  327. {
  328. flags &= ~URB_NO_TRANSFER_DMA_MAP;
  329. return flags;
  330. }
  331. static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
  332. int pack)
  333. {
  334. struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
  335. /*
  336. * Some members are not still implemented in usbip. I hope this issue
  337. * will be discussed when usbip is ported to other operating systems.
  338. */
  339. if (pack) {
  340. spdu->transfer_flags =
  341. tweak_transfer_flags(urb->transfer_flags);
  342. spdu->transfer_buffer_length = urb->transfer_buffer_length;
  343. spdu->start_frame = urb->start_frame;
  344. spdu->number_of_packets = urb->number_of_packets;
  345. spdu->interval = urb->interval;
  346. } else {
  347. urb->transfer_flags = spdu->transfer_flags;
  348. urb->transfer_buffer_length = spdu->transfer_buffer_length;
  349. urb->start_frame = spdu->start_frame;
  350. urb->number_of_packets = spdu->number_of_packets;
  351. urb->interval = spdu->interval;
  352. }
  353. }
  354. static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
  355. int pack)
  356. {
  357. struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
  358. if (pack) {
  359. rpdu->status = urb->status;
  360. rpdu->actual_length = urb->actual_length;
  361. rpdu->start_frame = urb->start_frame;
  362. rpdu->number_of_packets = urb->number_of_packets;
  363. rpdu->error_count = urb->error_count;
  364. } else {
  365. urb->status = rpdu->status;
  366. urb->actual_length = rpdu->actual_length;
  367. urb->start_frame = rpdu->start_frame;
  368. urb->number_of_packets = rpdu->number_of_packets;
  369. urb->error_count = rpdu->error_count;
  370. }
  371. }
  372. void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
  373. int pack)
  374. {
  375. switch (cmd) {
  376. case USBIP_CMD_SUBMIT:
  377. usbip_pack_cmd_submit(pdu, urb, pack);
  378. break;
  379. case USBIP_RET_SUBMIT:
  380. usbip_pack_ret_submit(pdu, urb, pack);
  381. break;
  382. default:
  383. /* NOT REACHED */
  384. pr_err("unknown command\n");
  385. break;
  386. }
  387. }
  388. EXPORT_SYMBOL_GPL(usbip_pack_pdu);
  389. static void correct_endian_basic(struct usbip_header_basic *base, int send)
  390. {
  391. if (send) {
  392. base->command = cpu_to_be32(base->command);
  393. base->seqnum = cpu_to_be32(base->seqnum);
  394. base->devid = cpu_to_be32(base->devid);
  395. base->direction = cpu_to_be32(base->direction);
  396. base->ep = cpu_to_be32(base->ep);
  397. } else {
  398. base->command = be32_to_cpu(base->command);
  399. base->seqnum = be32_to_cpu(base->seqnum);
  400. base->devid = be32_to_cpu(base->devid);
  401. base->direction = be32_to_cpu(base->direction);
  402. base->ep = be32_to_cpu(base->ep);
  403. }
  404. }
  405. static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
  406. int send)
  407. {
  408. if (send) {
  409. pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
  410. cpu_to_be32s(&pdu->transfer_buffer_length);
  411. cpu_to_be32s(&pdu->start_frame);
  412. cpu_to_be32s(&pdu->number_of_packets);
  413. cpu_to_be32s(&pdu->interval);
  414. } else {
  415. pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
  416. be32_to_cpus(&pdu->transfer_buffer_length);
  417. be32_to_cpus(&pdu->start_frame);
  418. be32_to_cpus(&pdu->number_of_packets);
  419. be32_to_cpus(&pdu->interval);
  420. }
  421. }
  422. static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
  423. int send)
  424. {
  425. if (send) {
  426. cpu_to_be32s(&pdu->status);
  427. cpu_to_be32s(&pdu->actual_length);
  428. cpu_to_be32s(&pdu->start_frame);
  429. cpu_to_be32s(&pdu->number_of_packets);
  430. cpu_to_be32s(&pdu->error_count);
  431. } else {
  432. be32_to_cpus(&pdu->status);
  433. be32_to_cpus(&pdu->actual_length);
  434. be32_to_cpus(&pdu->start_frame);
  435. be32_to_cpus(&pdu->number_of_packets);
  436. be32_to_cpus(&pdu->error_count);
  437. }
  438. }
  439. static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
  440. int send)
  441. {
  442. if (send)
  443. pdu->seqnum = cpu_to_be32(pdu->seqnum);
  444. else
  445. pdu->seqnum = be32_to_cpu(pdu->seqnum);
  446. }
  447. static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
  448. int send)
  449. {
  450. if (send)
  451. cpu_to_be32s(&pdu->status);
  452. else
  453. be32_to_cpus(&pdu->status);
  454. }
  455. void usbip_header_correct_endian(struct usbip_header *pdu, int send)
  456. {
  457. __u32 cmd = 0;
  458. if (send)
  459. cmd = pdu->base.command;
  460. correct_endian_basic(&pdu->base, send);
  461. if (!send)
  462. cmd = pdu->base.command;
  463. switch (cmd) {
  464. case USBIP_CMD_SUBMIT:
  465. correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
  466. break;
  467. case USBIP_RET_SUBMIT:
  468. correct_endian_ret_submit(&pdu->u.ret_submit, send);
  469. break;
  470. case USBIP_CMD_UNLINK:
  471. correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
  472. break;
  473. case USBIP_RET_UNLINK:
  474. correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
  475. break;
  476. default:
  477. /* NOT REACHED */
  478. pr_err("unknown command\n");
  479. break;
  480. }
  481. }
  482. EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
  483. static void usbip_iso_packet_correct_endian(
  484. struct usbip_iso_packet_descriptor *iso, int send)
  485. {
  486. /* does not need all members. but copy all simply. */
  487. if (send) {
  488. iso->offset = cpu_to_be32(iso->offset);
  489. iso->length = cpu_to_be32(iso->length);
  490. iso->status = cpu_to_be32(iso->status);
  491. iso->actual_length = cpu_to_be32(iso->actual_length);
  492. } else {
  493. iso->offset = be32_to_cpu(iso->offset);
  494. iso->length = be32_to_cpu(iso->length);
  495. iso->status = be32_to_cpu(iso->status);
  496. iso->actual_length = be32_to_cpu(iso->actual_length);
  497. }
  498. }
  499. static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
  500. struct usb_iso_packet_descriptor *uiso, int pack)
  501. {
  502. if (pack) {
  503. iso->offset = uiso->offset;
  504. iso->length = uiso->length;
  505. iso->status = uiso->status;
  506. iso->actual_length = uiso->actual_length;
  507. } else {
  508. uiso->offset = iso->offset;
  509. uiso->length = iso->length;
  510. uiso->status = iso->status;
  511. uiso->actual_length = iso->actual_length;
  512. }
  513. }
  514. /* must free buffer */
  515. struct usbip_iso_packet_descriptor*
  516. usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
  517. {
  518. struct usbip_iso_packet_descriptor *iso;
  519. int np = urb->number_of_packets;
  520. ssize_t size = np * sizeof(*iso);
  521. int i;
  522. iso = kzalloc(size, GFP_KERNEL);
  523. if (!iso)
  524. return NULL;
  525. for (i = 0; i < np; i++) {
  526. usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
  527. usbip_iso_packet_correct_endian(&iso[i], 1);
  528. }
  529. *bufflen = size;
  530. return iso;
  531. }
  532. EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
  533. /* some members of urb must be substituted before. */
  534. int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
  535. {
  536. void *buff;
  537. struct usbip_iso_packet_descriptor *iso;
  538. int np = urb->number_of_packets;
  539. int size = np * sizeof(*iso);
  540. int i;
  541. int ret;
  542. int total_length = 0;
  543. if (!usb_pipeisoc(urb->pipe))
  544. return 0;
  545. /* my Bluetooth dongle gets ISO URBs which are np = 0 */
  546. if (np == 0)
  547. return 0;
  548. buff = kzalloc(size, GFP_KERNEL);
  549. if (!buff)
  550. return -ENOMEM;
  551. ret = usbip_recv(ud->tcp_socket, buff, size);
  552. if (ret != size) {
  553. dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
  554. ret);
  555. kfree(buff);
  556. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  557. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  558. else
  559. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  560. return -EPIPE;
  561. }
  562. iso = (struct usbip_iso_packet_descriptor *) buff;
  563. for (i = 0; i < np; i++) {
  564. usbip_iso_packet_correct_endian(&iso[i], 0);
  565. usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
  566. total_length += urb->iso_frame_desc[i].actual_length;
  567. }
  568. kfree(buff);
  569. if (total_length != urb->actual_length) {
  570. dev_err(&urb->dev->dev,
  571. "total length of iso packets %d not equal to actual length of buffer %d\n",
  572. total_length, urb->actual_length);
  573. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  574. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  575. else
  576. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  577. return -EPIPE;
  578. }
  579. return ret;
  580. }
  581. EXPORT_SYMBOL_GPL(usbip_recv_iso);
  582. /*
  583. * This functions restores the padding which was removed for optimizing
  584. * the bandwidth during transfer over tcp/ip
  585. *
  586. * buffer and iso packets need to be stored and be in propeper endian in urb
  587. * before calling this function
  588. */
  589. void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
  590. {
  591. int np = urb->number_of_packets;
  592. int i;
  593. int actualoffset = urb->actual_length;
  594. if (!usb_pipeisoc(urb->pipe))
  595. return;
  596. /* if no packets or length of data is 0, then nothing to unpack */
  597. if (np == 0 || urb->actual_length == 0)
  598. return;
  599. /*
  600. * if actual_length is transfer_buffer_length then no padding is
  601. * present.
  602. */
  603. if (urb->actual_length == urb->transfer_buffer_length)
  604. return;
  605. /*
  606. * loop over all packets from last to first (to prevent overwriting
  607. * memory when padding) and move them into the proper place
  608. */
  609. for (i = np-1; i > 0; i--) {
  610. actualoffset -= urb->iso_frame_desc[i].actual_length;
  611. memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
  612. urb->transfer_buffer + actualoffset,
  613. urb->iso_frame_desc[i].actual_length);
  614. }
  615. }
  616. EXPORT_SYMBOL_GPL(usbip_pad_iso);
  617. /* some members of urb must be substituted before. */
  618. int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
  619. {
  620. int ret;
  621. int size;
  622. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
  623. /* the direction of urb must be OUT. */
  624. if (usb_pipein(urb->pipe))
  625. return 0;
  626. size = urb->transfer_buffer_length;
  627. } else {
  628. /* the direction of urb must be IN. */
  629. if (usb_pipeout(urb->pipe))
  630. return 0;
  631. size = urb->actual_length;
  632. }
  633. /* no need to recv xbuff */
  634. if (!(size > 0))
  635. return 0;
  636. if (size > urb->transfer_buffer_length) {
  637. /* should not happen, probably malicious packet */
  638. if (ud->side == USBIP_STUB) {
  639. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  640. return 0;
  641. } else {
  642. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  643. return -EPIPE;
  644. }
  645. }
  646. ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
  647. if (ret != size) {
  648. dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
  649. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
  650. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  651. } else {
  652. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  653. return -EPIPE;
  654. }
  655. }
  656. return ret;
  657. }
  658. EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
  659. static int __init usbip_core_init(void)
  660. {
  661. int ret;
  662. ret = usbip_init_eh();
  663. if (ret)
  664. return ret;
  665. return 0;
  666. }
  667. static void __exit usbip_core_exit(void)
  668. {
  669. usbip_finish_eh();
  670. return;
  671. }
  672. module_init(usbip_core_init);
  673. module_exit(usbip_core_exit);
  674. MODULE_AUTHOR(DRIVER_AUTHOR);
  675. MODULE_DESCRIPTION(DRIVER_DESC);
  676. MODULE_LICENSE("GPL");