usbip_common.c 19 KB

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