usbip_common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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 hub ttport %d\n", 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 %s, bus %s\n", dev_name(&udev->parent->dev),
  122. udev->bus->bus_name);
  123. dev_dbg(dev, "have_langid %d, string_langid %d\n",
  124. udev->have_langid, udev->string_langid);
  125. dev_dbg(dev, "maxchild %d\n", udev->maxchild);
  126. }
  127. static void usbip_dump_request_type(__u8 rt)
  128. {
  129. switch (rt & USB_RECIP_MASK) {
  130. case USB_RECIP_DEVICE:
  131. pr_debug("DEVICE");
  132. break;
  133. case USB_RECIP_INTERFACE:
  134. pr_debug("INTERF");
  135. break;
  136. case USB_RECIP_ENDPOINT:
  137. pr_debug("ENDPOI");
  138. break;
  139. case USB_RECIP_OTHER:
  140. pr_debug("OTHER ");
  141. break;
  142. default:
  143. pr_debug("------");
  144. break;
  145. }
  146. }
  147. static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
  148. {
  149. if (!cmd) {
  150. pr_debug(" : null pointer\n");
  151. return;
  152. }
  153. pr_debug(" ");
  154. pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
  155. cmd->bRequestType, cmd->bRequest,
  156. cmd->wValue, cmd->wIndex, cmd->wLength);
  157. pr_debug("\n ");
  158. if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  159. pr_debug("STANDARD ");
  160. switch (cmd->bRequest) {
  161. case USB_REQ_GET_STATUS:
  162. pr_debug("GET_STATUS\n");
  163. break;
  164. case USB_REQ_CLEAR_FEATURE:
  165. pr_debug("CLEAR_FEAT\n");
  166. break;
  167. case USB_REQ_SET_FEATURE:
  168. pr_debug("SET_FEAT\n");
  169. break;
  170. case USB_REQ_SET_ADDRESS:
  171. pr_debug("SET_ADDRRS\n");
  172. break;
  173. case USB_REQ_GET_DESCRIPTOR:
  174. pr_debug("GET_DESCRI\n");
  175. break;
  176. case USB_REQ_SET_DESCRIPTOR:
  177. pr_debug("SET_DESCRI\n");
  178. break;
  179. case USB_REQ_GET_CONFIGURATION:
  180. pr_debug("GET_CONFIG\n");
  181. break;
  182. case USB_REQ_SET_CONFIGURATION:
  183. pr_debug("SET_CONFIG\n");
  184. break;
  185. case USB_REQ_GET_INTERFACE:
  186. pr_debug("GET_INTERF\n");
  187. break;
  188. case USB_REQ_SET_INTERFACE:
  189. pr_debug("SET_INTERF\n");
  190. break;
  191. case USB_REQ_SYNCH_FRAME:
  192. pr_debug("SYNC_FRAME\n");
  193. break;
  194. default:
  195. pr_debug("REQ(%02X)\n", cmd->bRequest);
  196. break;
  197. }
  198. usbip_dump_request_type(cmd->bRequestType);
  199. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
  200. pr_debug("CLASS\n");
  201. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  202. pr_debug("VENDOR\n");
  203. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
  204. pr_debug("RESERVED\n");
  205. }
  206. }
  207. void usbip_dump_urb(struct urb *urb)
  208. {
  209. struct device *dev;
  210. if (!urb) {
  211. pr_debug("urb: null pointer!!\n");
  212. return;
  213. }
  214. if (!urb->dev) {
  215. pr_debug("urb->dev: null pointer!!\n");
  216. return;
  217. }
  218. dev = &urb->dev->dev;
  219. usbip_dump_usb_device(urb->dev);
  220. dev_dbg(dev, " pipe :%08x ", urb->pipe);
  221. usbip_dump_pipe(urb->pipe);
  222. dev_dbg(dev, " status :%d\n", urb->status);
  223. dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
  224. dev_dbg(dev, " transfer_buffer_length:%d\n",
  225. urb->transfer_buffer_length);
  226. dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
  227. if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
  228. usbip_dump_usb_ctrlrequest(
  229. (struct usb_ctrlrequest *)urb->setup_packet);
  230. dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
  231. dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
  232. dev_dbg(dev, " interval :%d\n", urb->interval);
  233. dev_dbg(dev, " error_count :%d\n", urb->error_count);
  234. }
  235. EXPORT_SYMBOL_GPL(usbip_dump_urb);
  236. void usbip_dump_header(struct usbip_header *pdu)
  237. {
  238. pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
  239. pdu->base.command,
  240. pdu->base.seqnum,
  241. pdu->base.devid,
  242. pdu->base.direction,
  243. pdu->base.ep);
  244. switch (pdu->base.command) {
  245. case USBIP_CMD_SUBMIT:
  246. pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
  247. pdu->u.cmd_submit.transfer_flags,
  248. pdu->u.cmd_submit.transfer_buffer_length,
  249. pdu->u.cmd_submit.start_frame,
  250. pdu->u.cmd_submit.number_of_packets,
  251. pdu->u.cmd_submit.interval);
  252. break;
  253. case USBIP_CMD_UNLINK:
  254. pr_debug("USBIP_CMD_UNLINK: seq %u\n",
  255. pdu->u.cmd_unlink.seqnum);
  256. break;
  257. case USBIP_RET_SUBMIT:
  258. pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
  259. pdu->u.ret_submit.status,
  260. pdu->u.ret_submit.actual_length,
  261. pdu->u.ret_submit.start_frame,
  262. pdu->u.ret_submit.number_of_packets,
  263. pdu->u.ret_submit.error_count);
  264. break;
  265. case USBIP_RET_UNLINK:
  266. pr_debug("USBIP_RET_UNLINK: status %d\n",
  267. pdu->u.ret_unlink.status);
  268. break;
  269. default:
  270. /* NOT REACHED */
  271. pr_err("unknown command\n");
  272. break;
  273. }
  274. }
  275. EXPORT_SYMBOL_GPL(usbip_dump_header);
  276. /* Receive data over TCP/IP. */
  277. int usbip_recv(struct socket *sock, void *buf, int size)
  278. {
  279. int result;
  280. struct msghdr msg;
  281. struct kvec iov;
  282. int total = 0;
  283. /* for blocks of if (usbip_dbg_flag_xmit) */
  284. char *bp = buf;
  285. int osize = size;
  286. if (!sock || !buf || !size)
  287. return -EINVAL;
  288. usbip_dbg_xmit("enter\n");
  289. do {
  290. sock->sk->sk_allocation = GFP_NOIO;
  291. iov.iov_base = buf;
  292. iov.iov_len = size;
  293. msg.msg_name = NULL;
  294. msg.msg_namelen = 0;
  295. msg.msg_control = NULL;
  296. msg.msg_controllen = 0;
  297. msg.msg_flags = MSG_NOSIGNAL;
  298. result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
  299. if (result <= 0)
  300. goto err;
  301. size -= result;
  302. buf += result;
  303. total += result;
  304. } while (size > 0);
  305. if (usbip_dbg_flag_xmit) {
  306. if (!in_interrupt())
  307. pr_debug("%-10s:", current->comm);
  308. else
  309. pr_debug("interrupt :");
  310. pr_debug("receiving....\n");
  311. usbip_dump_buffer(bp, osize);
  312. pr_debug("received, osize %d ret %d size %d total %d\n",
  313. osize, result, size, total);
  314. }
  315. return total;
  316. err:
  317. return result;
  318. }
  319. EXPORT_SYMBOL_GPL(usbip_recv);
  320. /* there may be more cases to tweak the flags. */
  321. static unsigned int tweak_transfer_flags(unsigned int flags)
  322. {
  323. flags &= ~URB_NO_TRANSFER_DMA_MAP;
  324. return flags;
  325. }
  326. static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
  327. int pack)
  328. {
  329. struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
  330. /*
  331. * Some members are not still implemented in usbip. I hope this issue
  332. * will be discussed when usbip is ported to other operating systems.
  333. */
  334. if (pack) {
  335. spdu->transfer_flags =
  336. tweak_transfer_flags(urb->transfer_flags);
  337. spdu->transfer_buffer_length = urb->transfer_buffer_length;
  338. spdu->start_frame = urb->start_frame;
  339. spdu->number_of_packets = urb->number_of_packets;
  340. spdu->interval = urb->interval;
  341. } else {
  342. urb->transfer_flags = spdu->transfer_flags;
  343. urb->transfer_buffer_length = spdu->transfer_buffer_length;
  344. urb->start_frame = spdu->start_frame;
  345. urb->number_of_packets = spdu->number_of_packets;
  346. urb->interval = spdu->interval;
  347. }
  348. }
  349. static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
  350. int pack)
  351. {
  352. struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
  353. if (pack) {
  354. rpdu->status = urb->status;
  355. rpdu->actual_length = urb->actual_length;
  356. rpdu->start_frame = urb->start_frame;
  357. rpdu->number_of_packets = urb->number_of_packets;
  358. rpdu->error_count = urb->error_count;
  359. } else {
  360. urb->status = rpdu->status;
  361. urb->actual_length = rpdu->actual_length;
  362. urb->start_frame = rpdu->start_frame;
  363. urb->number_of_packets = rpdu->number_of_packets;
  364. urb->error_count = rpdu->error_count;
  365. }
  366. }
  367. void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
  368. int pack)
  369. {
  370. switch (cmd) {
  371. case USBIP_CMD_SUBMIT:
  372. usbip_pack_cmd_submit(pdu, urb, pack);
  373. break;
  374. case USBIP_RET_SUBMIT:
  375. usbip_pack_ret_submit(pdu, urb, pack);
  376. break;
  377. default:
  378. /* NOT REACHED */
  379. pr_err("unknown command\n");
  380. break;
  381. }
  382. }
  383. EXPORT_SYMBOL_GPL(usbip_pack_pdu);
  384. static void correct_endian_basic(struct usbip_header_basic *base, int send)
  385. {
  386. if (send) {
  387. base->command = cpu_to_be32(base->command);
  388. base->seqnum = cpu_to_be32(base->seqnum);
  389. base->devid = cpu_to_be32(base->devid);
  390. base->direction = cpu_to_be32(base->direction);
  391. base->ep = cpu_to_be32(base->ep);
  392. } else {
  393. base->command = be32_to_cpu(base->command);
  394. base->seqnum = be32_to_cpu(base->seqnum);
  395. base->devid = be32_to_cpu(base->devid);
  396. base->direction = be32_to_cpu(base->direction);
  397. base->ep = be32_to_cpu(base->ep);
  398. }
  399. }
  400. static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
  401. int send)
  402. {
  403. if (send) {
  404. pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
  405. cpu_to_be32s(&pdu->transfer_buffer_length);
  406. cpu_to_be32s(&pdu->start_frame);
  407. cpu_to_be32s(&pdu->number_of_packets);
  408. cpu_to_be32s(&pdu->interval);
  409. } else {
  410. pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
  411. be32_to_cpus(&pdu->transfer_buffer_length);
  412. be32_to_cpus(&pdu->start_frame);
  413. be32_to_cpus(&pdu->number_of_packets);
  414. be32_to_cpus(&pdu->interval);
  415. }
  416. }
  417. static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
  418. int send)
  419. {
  420. if (send) {
  421. cpu_to_be32s(&pdu->status);
  422. cpu_to_be32s(&pdu->actual_length);
  423. cpu_to_be32s(&pdu->start_frame);
  424. cpu_to_be32s(&pdu->number_of_packets);
  425. cpu_to_be32s(&pdu->error_count);
  426. } else {
  427. be32_to_cpus(&pdu->status);
  428. be32_to_cpus(&pdu->actual_length);
  429. be32_to_cpus(&pdu->start_frame);
  430. be32_to_cpus(&pdu->number_of_packets);
  431. be32_to_cpus(&pdu->error_count);
  432. }
  433. }
  434. static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
  435. int send)
  436. {
  437. if (send)
  438. pdu->seqnum = cpu_to_be32(pdu->seqnum);
  439. else
  440. pdu->seqnum = be32_to_cpu(pdu->seqnum);
  441. }
  442. static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
  443. int send)
  444. {
  445. if (send)
  446. cpu_to_be32s(&pdu->status);
  447. else
  448. be32_to_cpus(&pdu->status);
  449. }
  450. void usbip_header_correct_endian(struct usbip_header *pdu, int send)
  451. {
  452. __u32 cmd = 0;
  453. if (send)
  454. cmd = pdu->base.command;
  455. correct_endian_basic(&pdu->base, send);
  456. if (!send)
  457. cmd = pdu->base.command;
  458. switch (cmd) {
  459. case USBIP_CMD_SUBMIT:
  460. correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
  461. break;
  462. case USBIP_RET_SUBMIT:
  463. correct_endian_ret_submit(&pdu->u.ret_submit, send);
  464. break;
  465. case USBIP_CMD_UNLINK:
  466. correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
  467. break;
  468. case USBIP_RET_UNLINK:
  469. correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
  470. break;
  471. default:
  472. /* NOT REACHED */
  473. pr_err("unknown command\n");
  474. break;
  475. }
  476. }
  477. EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
  478. static void usbip_iso_packet_correct_endian(
  479. struct usbip_iso_packet_descriptor *iso, int send)
  480. {
  481. /* does not need all members. but copy all simply. */
  482. if (send) {
  483. iso->offset = cpu_to_be32(iso->offset);
  484. iso->length = cpu_to_be32(iso->length);
  485. iso->status = cpu_to_be32(iso->status);
  486. iso->actual_length = cpu_to_be32(iso->actual_length);
  487. } else {
  488. iso->offset = be32_to_cpu(iso->offset);
  489. iso->length = be32_to_cpu(iso->length);
  490. iso->status = be32_to_cpu(iso->status);
  491. iso->actual_length = be32_to_cpu(iso->actual_length);
  492. }
  493. }
  494. static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
  495. struct usb_iso_packet_descriptor *uiso, int pack)
  496. {
  497. if (pack) {
  498. iso->offset = uiso->offset;
  499. iso->length = uiso->length;
  500. iso->status = uiso->status;
  501. iso->actual_length = uiso->actual_length;
  502. } else {
  503. uiso->offset = iso->offset;
  504. uiso->length = iso->length;
  505. uiso->status = iso->status;
  506. uiso->actual_length = iso->actual_length;
  507. }
  508. }
  509. /* must free buffer */
  510. struct usbip_iso_packet_descriptor*
  511. usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
  512. {
  513. struct usbip_iso_packet_descriptor *iso;
  514. int np = urb->number_of_packets;
  515. ssize_t size = np * sizeof(*iso);
  516. int i;
  517. iso = kzalloc(size, GFP_KERNEL);
  518. if (!iso)
  519. return NULL;
  520. for (i = 0; i < np; i++) {
  521. usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
  522. usbip_iso_packet_correct_endian(&iso[i], 1);
  523. }
  524. *bufflen = size;
  525. return iso;
  526. }
  527. EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
  528. /* some members of urb must be substituted before. */
  529. int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
  530. {
  531. void *buff;
  532. struct usbip_iso_packet_descriptor *iso;
  533. int np = urb->number_of_packets;
  534. int size = np * sizeof(*iso);
  535. int i;
  536. int ret;
  537. int total_length = 0;
  538. if (!usb_pipeisoc(urb->pipe))
  539. return 0;
  540. /* my Bluetooth dongle gets ISO URBs which are np = 0 */
  541. if (np == 0)
  542. return 0;
  543. buff = kzalloc(size, GFP_KERNEL);
  544. if (!buff)
  545. return -ENOMEM;
  546. ret = usbip_recv(ud->tcp_socket, buff, size);
  547. if (ret != size) {
  548. dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
  549. ret);
  550. kfree(buff);
  551. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  552. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  553. else
  554. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  555. return -EPIPE;
  556. }
  557. iso = (struct usbip_iso_packet_descriptor *) buff;
  558. for (i = 0; i < np; i++) {
  559. usbip_iso_packet_correct_endian(&iso[i], 0);
  560. usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
  561. total_length += urb->iso_frame_desc[i].actual_length;
  562. }
  563. kfree(buff);
  564. if (total_length != urb->actual_length) {
  565. dev_err(&urb->dev->dev,
  566. "total length of iso packets %d not equal to actual length of buffer %d\n",
  567. total_length, urb->actual_length);
  568. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  569. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  570. else
  571. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  572. return -EPIPE;
  573. }
  574. return ret;
  575. }
  576. EXPORT_SYMBOL_GPL(usbip_recv_iso);
  577. /*
  578. * This functions restores the padding which was removed for optimizing
  579. * the bandwidth during transfer over tcp/ip
  580. *
  581. * buffer and iso packets need to be stored and be in propeper endian in urb
  582. * before calling this function
  583. */
  584. void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
  585. {
  586. int np = urb->number_of_packets;
  587. int i;
  588. int actualoffset = urb->actual_length;
  589. if (!usb_pipeisoc(urb->pipe))
  590. return;
  591. /* if no packets or length of data is 0, then nothing to unpack */
  592. if (np == 0 || urb->actual_length == 0)
  593. return;
  594. /*
  595. * if actual_length is transfer_buffer_length then no padding is
  596. * present.
  597. */
  598. if (urb->actual_length == urb->transfer_buffer_length)
  599. return;
  600. /*
  601. * loop over all packets from last to first (to prevent overwritting
  602. * memory when padding) and move them into the proper place
  603. */
  604. for (i = np-1; i > 0; i--) {
  605. actualoffset -= urb->iso_frame_desc[i].actual_length;
  606. memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
  607. urb->transfer_buffer + actualoffset,
  608. urb->iso_frame_desc[i].actual_length);
  609. }
  610. }
  611. EXPORT_SYMBOL_GPL(usbip_pad_iso);
  612. /* some members of urb must be substituted before. */
  613. int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
  614. {
  615. int ret;
  616. int size;
  617. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
  618. /* the direction of urb must be OUT. */
  619. if (usb_pipein(urb->pipe))
  620. return 0;
  621. size = urb->transfer_buffer_length;
  622. } else {
  623. /* the direction of urb must be IN. */
  624. if (usb_pipeout(urb->pipe))
  625. return 0;
  626. size = urb->actual_length;
  627. }
  628. /* no need to recv xbuff */
  629. if (!(size > 0))
  630. return 0;
  631. if (size > urb->transfer_buffer_length) {
  632. /* should not happen, probably malicious packet */
  633. if (ud->side == USBIP_STUB) {
  634. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  635. return 0;
  636. } else {
  637. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  638. return -EPIPE;
  639. }
  640. }
  641. ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
  642. if (ret != size) {
  643. dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
  644. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
  645. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  646. } else {
  647. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  648. return -EPIPE;
  649. }
  650. }
  651. return ret;
  652. }
  653. EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
  654. static int __init usbip_core_init(void)
  655. {
  656. int ret;
  657. pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
  658. ret = usbip_init_eh();
  659. if (ret)
  660. return ret;
  661. return 0;
  662. }
  663. static void __exit usbip_core_exit(void)
  664. {
  665. usbip_finish_eh();
  666. return;
  667. }
  668. module_init(usbip_core_init);
  669. module_exit(usbip_core_exit);
  670. MODULE_AUTHOR(DRIVER_AUTHOR);
  671. MODULE_DESCRIPTION(DRIVER_DESC);
  672. MODULE_LICENSE("GPL");
  673. MODULE_VERSION(USBIP_VERSION);