f_mtp_samsung.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. /*
  2. * drivers/usb/gadget/f_mtp_samsung.c
  3. *
  4. * Function Driver for USB MTP,
  5. * f_mtp_samsung.c -- MTP Driver, for MTP development,
  6. *
  7. * Copyright (C) 2009 by Samsung Electronics,
  8. * Author:Deepak M.G. <deepak.guru@samsung.com>,
  9. * Author:Madhukar.J <madhukar.j@samsung.com>,
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. /*
  22. * f_mtp_samsung.c file is the driver for MTP device. Totally three
  23. * EndPoints will be configured in which 2 Bulk End Points
  24. * and 1 Interrupt End point. This driver will also register as
  25. * misc driver and exposes file operation funtions to user space.
  26. */
  27. /* Includes */
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/poll.h>
  31. #include <linux/delay.h>
  32. #include <linux/wait.h>
  33. #include <linux/err.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/types.h>
  36. #include <linux/device.h>
  37. #include <linux/miscdevice.h>
  38. #include <linux/kernel.h>
  39. #include <linux/kref.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/string.h>
  42. #include <linux/usb.h>
  43. #include <linux/usb_usual.h>
  44. #include <linux/usb/ch9.h>
  45. #include <linux/usb/composite.h>
  46. #include <linux/usb/gadget.h>
  47. #include <linux/hardirq.h>
  48. #include <linux/sched.h>
  49. #include <linux/usb/f_accessory.h>
  50. #include <asm-generic/siginfo.h>
  51. #include <linux/kernel.h>
  52. #include "f_mtp.h"
  53. #include "gadget_chips.h"
  54. /*-------------------------------------------------------------------------*/
  55. /*Only for Debug*/
  56. #define DEBUG_MTP 0
  57. /*#define CSY_TEST */
  58. #if DEBUG_MTP
  59. #define DEBUG_MTP_SETUP
  60. #define DEBUG_MTP_READ
  61. #define DEBUG_MTP_WRITE
  62. #else
  63. #undef DEBUG_MTP_SETUP
  64. #undef DEBUG_MTP_READ
  65. #undef DEBUG_MTP_WRITE
  66. #endif
  67. /*#define DEBUG_MTP_SETUP*/
  68. /*#define DEBUG_MTP_READ*/
  69. /*#define DEBUG_MTP_WRITE*/
  70. #ifdef DEBUG_MTP_SETUP
  71. #define DEBUG_MTPB(fmt, args...) printk(fmt, ##args)
  72. #else
  73. #define DEBUG_MTPB(fmt, args...) do {} while (0)
  74. #endif
  75. #ifdef DEBUG_MTP_READ
  76. #define DEBUG_MTPR(fmt, args...) printk(fmt, ##args)
  77. #else
  78. #define DEBUG_MTPR(fmt, args...) do {} while (0)
  79. #endif
  80. #ifdef DEBUG_MTP_WRITE
  81. #define DEBUG_MTPW(fmt, args...) printk(fmt, ##args)
  82. #else
  83. #define DEBUG_MTPW(fmt, args...) do {} while (0)
  84. #endif
  85. /*-------------------------------------------------------------------------*/
  86. #define MTPG_BULK_BUFFER_SIZE 16384
  87. #define MTPG_INTR_BUFFER_SIZE 28
  88. /* number of rx and tx requests to allocate */
  89. #define MTPG_RX_REQ_MAX 4
  90. #define MTPG_MTPG_TX_REQ_MAX 4
  91. #define MTPG_INTR_REQ_MAX 5
  92. /* ID for Microsoft MTP OS String */
  93. #define MTPG_OS_STRING_ID 0xEE
  94. #define DRIVER_NAME "usb_mtp_gadget"
  95. #define MAX_GUID_SIZE 0x28
  96. static const char mtpg_longname[] = "mtp";
  97. static const char shortname[] = DRIVER_NAME;
  98. static int mtp_pid;
  99. char guid_info[MAX_GUID_SIZE+1];
  100. /* MTP Device Structure*/
  101. struct mtpg_dev {
  102. struct usb_function function;
  103. struct usb_composite_dev *cdev;
  104. struct usb_gadget *gadget;
  105. spinlock_t lock;
  106. u8 config;
  107. int online;
  108. int error;
  109. int read_ready;
  110. struct list_head tx_idle;
  111. struct list_head rx_idle;
  112. struct list_head rx_done;
  113. struct list_head intr_idle;
  114. wait_queue_head_t read_wq;
  115. wait_queue_head_t write_wq;
  116. wait_queue_head_t intr_wq;
  117. struct usb_request *read_req;
  118. unsigned char *read_buf;
  119. unsigned read_count;
  120. struct usb_ep *bulk_in;
  121. struct usb_ep *bulk_out;
  122. struct usb_ep *int_in;
  123. struct usb_request *notify_req;
  124. struct workqueue_struct *wq;
  125. struct work_struct read_send_work;
  126. struct file *read_send_file;
  127. int64_t read_send_length;
  128. uint16_t read_send_cmd;
  129. uint32_t read_send_id;
  130. int read_send_result;
  131. atomic_t read_excl;
  132. atomic_t write_excl;
  133. atomic_t ioctl_excl;
  134. atomic_t open_excl;
  135. atomic_t wintfd_excl;
  136. char cancel_io_buf[USB_PTPREQUEST_CANCELIO_SIZE+1];
  137. int cancel_io;
  138. };
  139. /* Global mtpg_dev Structure
  140. * the_mtpg variable be used between mtpg_open() and mtpg_function_bind() */
  141. static struct mtpg_dev *the_mtpg;
  142. /* Three full-speed and high-speed endpoint descriptors: bulk-in, bulk-out,
  143. * and interrupt-in. */
  144. struct usb_interface_descriptor mtpg_interface_desc = {
  145. .bLength = USB_DT_INTERFACE_SIZE,
  146. .bDescriptorType = USB_DT_INTERFACE,
  147. .bInterfaceNumber = 0,
  148. .bNumEndpoints = 3,
  149. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  150. .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
  151. .bInterfaceProtocol = 0,
  152. };
  153. static struct usb_interface_descriptor ptp_interface_desc = {
  154. .bLength = USB_DT_INTERFACE_SIZE,
  155. .bDescriptorType = USB_DT_INTERFACE,
  156. .bInterfaceNumber = 0,
  157. .bNumEndpoints = 3,
  158. .bInterfaceClass = USB_CLASS_STILL_IMAGE,
  159. .bInterfaceSubClass = 1,
  160. .bInterfaceProtocol = 1,
  161. };
  162. static struct usb_endpoint_descriptor mtpg_superspeed_in_desc = {
  163. .bLength = USB_DT_ENDPOINT_SIZE,
  164. .bDescriptorType = USB_DT_ENDPOINT,
  165. .bEndpointAddress = USB_DIR_IN,
  166. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  167. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  168. };
  169. static struct usb_endpoint_descriptor mtpg_superspeed_out_desc = {
  170. .bLength = USB_DT_ENDPOINT_SIZE,
  171. .bDescriptorType = USB_DT_ENDPOINT,
  172. .bEndpointAddress = USB_DIR_OUT,
  173. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  174. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  175. };
  176. static struct usb_ss_ep_comp_descriptor mtpg_superspeed_bulk_comp_desc = {
  177. .bLength = sizeof mtpg_superspeed_bulk_comp_desc,
  178. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  179. /* the following 2 values can be tweaked if necessary */
  180. /* .bMaxBurst = 0, */
  181. /* .bmAttributes = 0, */
  182. };
  183. static struct usb_endpoint_descriptor fs_mtpg_in_desc = {
  184. .bLength = USB_DT_ENDPOINT_SIZE,
  185. .bDescriptorType = USB_DT_ENDPOINT,
  186. .bEndpointAddress = USB_DIR_IN,
  187. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  188. /* wMaxPacketSize set by autoconfiguration */
  189. };
  190. static struct usb_endpoint_descriptor fs_mtpg_out_desc = {
  191. .bLength = USB_DT_ENDPOINT_SIZE,
  192. .bDescriptorType = USB_DT_ENDPOINT,
  193. .bEndpointAddress = USB_DIR_OUT,
  194. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  195. /* wMaxPacketSize set by autoconfiguration */
  196. };
  197. static struct usb_endpoint_descriptor int_fs_notify_desc = {
  198. .bLength = USB_DT_ENDPOINT_SIZE,
  199. .bDescriptorType = USB_DT_ENDPOINT,
  200. .bEndpointAddress = USB_DIR_IN,
  201. .bmAttributes = USB_ENDPOINT_XFER_INT,
  202. .wMaxPacketSize = __constant_cpu_to_le16(MTPG_INTR_BUFFER_SIZE),
  203. .bInterval = 6,
  204. };
  205. static struct usb_descriptor_header *fs_mtpg_desc[] = {
  206. (struct usb_descriptor_header *) &mtpg_interface_desc,
  207. (struct usb_descriptor_header *) &fs_mtpg_in_desc,
  208. (struct usb_descriptor_header *) &fs_mtpg_out_desc,
  209. (struct usb_descriptor_header *) &int_fs_notify_desc,
  210. NULL,
  211. };
  212. static struct usb_endpoint_descriptor hs_mtpg_in_desc = {
  213. .bLength = USB_DT_ENDPOINT_SIZE,
  214. .bDescriptorType = USB_DT_ENDPOINT,
  215. /*bEndpointAddress copied from fs_mtpg_in_desc
  216. during mtpg_function_bind()*/
  217. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  218. .wMaxPacketSize = __constant_cpu_to_le16(512),
  219. };
  220. static struct usb_endpoint_descriptor hs_mtpg_out_desc = {
  221. .bLength = USB_DT_ENDPOINT_SIZE,
  222. .bDescriptorType = USB_DT_ENDPOINT,
  223. /*bEndpointAddress copied from fs_mtpg_out_desc
  224. during mtpg_function_bind()*/
  225. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  226. .wMaxPacketSize = __constant_cpu_to_le16(512),
  227. .bInterval = 1, /* NAK every 1 uframe */
  228. };
  229. static struct usb_endpoint_descriptor int_hs_notify_desc = {
  230. .bLength = USB_DT_ENDPOINT_SIZE,
  231. .bDescriptorType = USB_DT_ENDPOINT,
  232. .bEndpointAddress = USB_DIR_IN,
  233. .bmAttributes = USB_ENDPOINT_XFER_INT,
  234. .wMaxPacketSize = __constant_cpu_to_le16(MTPG_INTR_BUFFER_SIZE),
  235. .bInterval = 6,
  236. };
  237. static struct usb_ss_ep_comp_descriptor ss_intr_notify_desc = {
  238. .bLength = sizeof ss_intr_notify_desc,
  239. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  240. /* the following 3 values can be tweaked if necessary */
  241. /* .bMaxBurst = 0, */
  242. /* .bmAttributes = 0, */
  243. .wBytesPerInterval = __constant_cpu_to_le16(MTPG_INTR_BUFFER_SIZE),
  244. };
  245. /* In VZW Models size of MTU is fixed using Devguru AVD Descriptor */
  246. struct usb_mtp_avd_descriptor {
  247. __u8 bLength;
  248. __u8 bDescriptorType;
  249. __u8 bDescriptorSubType;
  250. __u16 bDAU1_Type;
  251. __u16 bDAU1_Length;
  252. __u8 bDAU1_Value;
  253. } __attribute__ ((packed));
  254. static struct usb_mtp_avd_descriptor mtp_avd_descriptor = {
  255. .bLength = 0x08,
  256. .bDescriptorType = 0x24,
  257. .bDescriptorSubType = 0x80,
  258. /* First DAU = MTU Size */
  259. .bDAU1_Type = 0x000C,
  260. .bDAU1_Length = 0x0001,
  261. .bDAU1_Value = 0x01,
  262. };
  263. static struct usb_descriptor_header *ss_mtpg_descs[] = {
  264. (struct usb_descriptor_header *) &mtpg_interface_desc,
  265. (struct usb_descriptor_header *) &mtpg_superspeed_in_desc,
  266. (struct usb_descriptor_header *) &mtpg_superspeed_bulk_comp_desc,
  267. (struct usb_descriptor_header *) &mtpg_superspeed_out_desc,
  268. (struct usb_descriptor_header *) &mtpg_superspeed_bulk_comp_desc,
  269. (struct usb_descriptor_header *) &int_hs_notify_desc,
  270. (struct usb_descriptor_header *) &ss_intr_notify_desc,
  271. (struct usb_descriptor_header *) &mtp_avd_descriptor,
  272. NULL,
  273. };
  274. static struct usb_descriptor_header *hs_mtpg_desc[] = {
  275. (struct usb_descriptor_header *) &mtpg_interface_desc,
  276. (struct usb_descriptor_header *) &hs_mtpg_in_desc,
  277. (struct usb_descriptor_header *) &hs_mtpg_out_desc,
  278. (struct usb_descriptor_header *) &int_hs_notify_desc,
  279. (struct usb_descriptor_header *) &mtp_avd_descriptor,
  280. NULL
  281. };
  282. static struct usb_descriptor_header *ss_ptpg_descs[] = {
  283. (struct usb_descriptor_header *) &ptp_interface_desc,
  284. (struct usb_descriptor_header *) &mtpg_superspeed_in_desc,
  285. (struct usb_descriptor_header *) &mtpg_superspeed_bulk_comp_desc,
  286. (struct usb_descriptor_header *) &mtpg_superspeed_out_desc,
  287. (struct usb_descriptor_header *) &mtpg_superspeed_bulk_comp_desc,
  288. (struct usb_descriptor_header *) &int_hs_notify_desc,
  289. (struct usb_descriptor_header *) &ss_intr_notify_desc,
  290. (struct usb_descriptor_header *) &mtp_avd_descriptor,
  291. NULL,
  292. };
  293. static struct usb_descriptor_header *fs_ptp_descs[] = {
  294. (struct usb_descriptor_header *) &ptp_interface_desc,
  295. (struct usb_descriptor_header *) &fs_mtpg_in_desc,
  296. (struct usb_descriptor_header *) &fs_mtpg_out_desc,
  297. (struct usb_descriptor_header *) &int_fs_notify_desc,
  298. (struct usb_descriptor_header *) &mtp_avd_descriptor,
  299. NULL,
  300. };
  301. static struct usb_descriptor_header *hs_ptp_descs[] = {
  302. (struct usb_descriptor_header *) &ptp_interface_desc,
  303. (struct usb_descriptor_header *) &hs_mtpg_in_desc,
  304. (struct usb_descriptor_header *) &hs_mtpg_out_desc,
  305. (struct usb_descriptor_header *) &int_hs_notify_desc,
  306. (struct usb_descriptor_header *) &mtp_avd_descriptor,
  307. NULL,
  308. };
  309. /* string IDs are assigned dynamically */
  310. #define F_MTP_IDX 0
  311. #define STRING_PRODUCT_IDX 1
  312. #define STRING_SERIAL_IDX 2
  313. /* default serial number takes at least two packets */
  314. static const char serial[] = "0123456789.0123456789.0123456789";
  315. static struct usb_string strings_dev_mtp[] = {
  316. [F_MTP_IDX].s = "MTP",
  317. [STRING_PRODUCT_IDX].s = mtpg_longname,
  318. [STRING_SERIAL_IDX].s = serial,
  319. { }, /* end of list */
  320. };
  321. static struct usb_gadget_strings stringtab_mtp = {
  322. .language = 0x0409, /* en-us */
  323. .strings = strings_dev_mtp,
  324. };
  325. static struct usb_gadget_strings *mtpg_dev_strings[] = {
  326. &stringtab_mtp,
  327. NULL,
  328. };
  329. /* Microsoft MTP OS String */
  330. static u8 mtpg_os_string[] = {
  331. 18, /* sizeof(mtpg_os_string) */
  332. USB_DT_STRING,
  333. /* Signature field: "MSFT100" */
  334. 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
  335. /* vendor code */
  336. 1,
  337. /* padding */
  338. 0
  339. };
  340. /* Microsoft Extended Configuration Descriptor Header Section */
  341. struct mtpg_ext_config_desc_header {
  342. __le32 dwLength;
  343. __u16 bcdVersion;
  344. __le16 wIndex;
  345. __u8 bCount;
  346. __u8 reserved[7];
  347. };
  348. /* Microsoft Extended Configuration Descriptor Function Section */
  349. struct mtpg_ext_config_desc_function {
  350. __u8 bFirstInterfaceNumber;
  351. __u8 bInterfaceCount;
  352. __u8 compatibleID[8];
  353. __u8 subCompatibleID[8];
  354. __u8 reserved[6];
  355. };
  356. /* MTP Extended Configuration Descriptor */
  357. struct {
  358. struct mtpg_ext_config_desc_header header;
  359. struct mtpg_ext_config_desc_function function;
  360. } mtpg_ext_config_desc = {
  361. .header = {
  362. .dwLength = __constant_cpu_to_le32
  363. (sizeof(mtpg_ext_config_desc)),
  364. .bcdVersion = __constant_cpu_to_le16(0x0100),
  365. .wIndex = __constant_cpu_to_le16(4),
  366. .bCount = __constant_cpu_to_le16(1),
  367. },
  368. .function = {
  369. .bFirstInterfaceNumber = 0,
  370. .bInterfaceCount = 1,
  371. .compatibleID = { 'M', 'T', 'P' },
  372. },
  373. };
  374. /* Function : Change config for multi configuration
  375. * Parameter : int conf_num (config number)
  376. * 0 - use mtp only without Samsung USB Driver
  377. * 1 - use mtp + acm with Samsung USB Driver
  378. * Description
  379. * Below function is for samsung multi configuration
  380. * feature made by soonyong,cho.
  381. * Please add below handler to set_config_desc of function.
  382. * Date : 2011-08-03
  383. */
  384. static int mtp_set_config_desc(int conf_num)
  385. {
  386. switch (conf_num) {
  387. case 0:
  388. mtpg_interface_desc.bInterfaceClass =
  389. USB_CLASS_VENDOR_SPEC;
  390. mtpg_interface_desc.bInterfaceSubClass =
  391. USB_SUBCLASS_VENDOR_SPEC;
  392. mtpg_interface_desc.bInterfaceProtocol =
  393. 0x0;
  394. break;
  395. case 1:
  396. mtpg_interface_desc.bInterfaceClass =
  397. USB_CLASS_STILL_IMAGE;
  398. mtpg_interface_desc.bInterfaceSubClass =
  399. 0x01;
  400. mtpg_interface_desc.bInterfaceProtocol =
  401. 0x01;
  402. break;
  403. }
  404. return 1;
  405. }
  406. /* -------------------------------------------------------------------------
  407. * Main Functionalities Start!
  408. * ------------------------------------------------------------------------- */
  409. static inline struct mtpg_dev *mtpg_func_to_dev(struct usb_function *f)
  410. {
  411. return container_of(f, struct mtpg_dev, function);
  412. }
  413. static inline int _lock(atomic_t *excl)
  414. {
  415. DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
  416. if (atomic_inc_return(excl) == 1) {
  417. return 0;
  418. } else {
  419. atomic_dec(excl);
  420. return -1;
  421. }
  422. }
  423. static inline void _unlock(atomic_t *excl)
  424. {
  425. atomic_dec(excl);
  426. }
  427. /* add a request to the tail of a list */
  428. static void mtpg_req_put(struct mtpg_dev *dev, struct list_head *head,
  429. struct usb_request *req)
  430. {
  431. unsigned long flags;
  432. DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
  433. spin_lock_irqsave(&dev->lock, flags);
  434. list_add_tail(&req->list, head);
  435. spin_unlock_irqrestore(&dev->lock, flags);
  436. }
  437. static ssize_t guid_show(struct device *dev,
  438. struct device_attribute *attr, char *buf)
  439. {
  440. printk(KERN_DEBUG "mtp: [%s]\tline = [%d]\n", __func__, __LINE__);
  441. memcpy(buf, guid_info, MAX_GUID_SIZE);
  442. return MAX_GUID_SIZE;
  443. }
  444. static ssize_t guid_store(struct device *dev,
  445. struct device_attribute *attr, const char *buf, size_t size)
  446. {
  447. int value;
  448. printk(KERN_DEBUG "mtp: [%s]\tline = [%d]\n", __func__, __LINE__);
  449. if (size > MAX_GUID_SIZE)
  450. return -EINVAL;
  451. value = strlcpy(guid_info, buf, size);
  452. return value;
  453. }
  454. /* remove a request from the head of a list */
  455. static struct usb_request *mtpg_req_get(struct mtpg_dev *dev,
  456. struct list_head *head)
  457. {
  458. unsigned long flags;
  459. struct usb_request *req;
  460. DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
  461. spin_lock_irqsave(&dev->lock, flags);
  462. if (list_empty(head)) {
  463. req = 0;
  464. } else {
  465. req = list_first_entry(head, struct usb_request, list);
  466. list_del(&req->list);
  467. }
  468. spin_unlock_irqrestore(&dev->lock, flags);
  469. return req;
  470. }
  471. static int mtp_send_signal(int value)
  472. {
  473. int ret;
  474. struct siginfo info;
  475. struct task_struct *t;
  476. memset(&info, 0, sizeof(struct siginfo));
  477. info.si_signo = SIG_SETUP;
  478. info.si_code = SI_QUEUE;
  479. info.si_int = value;
  480. rcu_read_lock();
  481. if (!current->nsproxy) {
  482. printk(KERN_DEBUG "process has gone\n");
  483. rcu_read_unlock();
  484. return -ENODEV;
  485. }
  486. t = pid_task(find_vpid(mtp_pid), PIDTYPE_PID);
  487. if (t == NULL) {
  488. printk(KERN_DEBUG "no such pid\n");
  489. rcu_read_unlock();
  490. return -ENODEV;
  491. }
  492. rcu_read_unlock();
  493. /*send the signal*/
  494. ret = send_sig_info(SIG_SETUP, &info, t);
  495. if (ret < 0) {
  496. printk(KERN_ERR "[%s]error sending signal\n", __func__);
  497. return ret;
  498. }
  499. return 0;
  500. }
  501. static int mtpg_open(struct inode *ip, struct file *fp)
  502. {
  503. printk(KERN_DEBUG "[%s]\tline = [%d]\n", __func__, __LINE__);
  504. if (_lock(&the_mtpg->open_excl)) {
  505. printk(KERN_ERR "mtpg_open fn mtpg device busy\n");
  506. return -EBUSY;
  507. }
  508. fp->private_data = the_mtpg;
  509. /* clear the error latch */
  510. DEBUG_MTPB("[%s] mtpg_open and clearing the error = 0\n", __func__);
  511. the_mtpg->error = 0;
  512. return 0;
  513. }
  514. static ssize_t mtpg_read(struct file *fp, char __user *buf,
  515. size_t count, loff_t *pos)
  516. {
  517. struct mtpg_dev *dev = fp->private_data;
  518. struct usb_request *req;
  519. int r = count, xfer;
  520. int ret;
  521. DEBUG_MTPR("[%s] and count = (%d)\n", __func__, count);
  522. if (_lock(&dev->read_excl))
  523. return -EBUSY;
  524. while (!((dev->online || dev->error) && dev->read_ready)) {
  525. DEBUG_MTPR("[%s] and line is = %d\n", __func__, __LINE__);
  526. ret = wait_event_interruptible(dev->read_wq,
  527. ((dev->online || dev->error) && dev->read_ready));
  528. if (ret < 0) {
  529. _unlock(&dev->read_excl);
  530. printk(KERN_DEBUG "[%s]line is = %d,mtp_read ret<0\n",
  531. __func__, __LINE__);
  532. return ret;
  533. }
  534. }
  535. while (count > 0) {
  536. DEBUG_MTPR("[%s] and line is = %d\n", __func__, __LINE__);
  537. if (dev->error) {
  538. r = -EIO;
  539. printk(KERN_ERR "[%s]\t%d:dev->error so break r=%d\n",
  540. __func__, __LINE__, r);
  541. break;
  542. }
  543. /* if we have idle read requests, get them queued */
  544. DEBUG_MTPR("[%s]\t%d: get request\n", __func__, __LINE__);
  545. while ((req = mtpg_req_get(dev, &dev->rx_idle))) {
  546. requeue_req:
  547. req->length = MTPG_BULK_BUFFER_SIZE;
  548. DEBUG_MTPR("[%s]\t%d:usb-ep-queue\n",
  549. __func__, __LINE__);
  550. ret = usb_ep_queue(dev->bulk_out, req, GFP_ATOMIC);
  551. DEBUG_MTPR("[%s]\t%d:Endpoint: %s\n",
  552. __func__, __LINE__, dev->bulk_out->name);
  553. if (ret < 0) {
  554. r = -EIO;
  555. dev->error = 1;
  556. mtpg_req_put(dev, &dev->rx_idle, req);
  557. printk(KERN_ERR "[%s]line[%d]FAIL r=%d\n",
  558. __func__, __LINE__, r);
  559. goto fail;
  560. } else {
  561. DEBUG_MTPR("[%s]rx req queue%pK\n",
  562. __func__, req);
  563. }
  564. }
  565. DEBUG_MTPR("[%s]\t%d:read_count = %d\n",
  566. __func__, __LINE__, dev->read_count);
  567. /* if we have data pending, give it to userspace */
  568. if (dev->read_count > 0) {
  569. DEBUG_MTPR("[%s]\t%d: read_count = %d\n",
  570. __func__, __LINE__, dev->read_count);
  571. if (dev->read_count < count)
  572. xfer = dev->read_count;
  573. else
  574. xfer = count;
  575. DEBUG_MTPR("[%s]copy_to_user 0x%x bytes on EP %pK\n",
  576. __func__, dev->read_count, dev->bulk_out);
  577. if (copy_to_user(buf, dev->read_buf, xfer)) {
  578. r = -EFAULT;
  579. printk(KERN_ERR "[%s]%d:cpytouer fail r=%d\n",
  580. __func__, __LINE__, r);
  581. break;
  582. }
  583. dev->read_buf += xfer;
  584. dev->read_count -= xfer;
  585. buf += xfer;
  586. count -= xfer;
  587. /* if we've emptied the buffer, release the request */
  588. if (dev->read_count == 0) {
  589. DEBUG_MTPR("[%s] and line is = %d\n",
  590. __func__, __LINE__);
  591. mtpg_req_put(dev, &dev->rx_idle, dev->read_req);
  592. dev->read_req = 0;
  593. }
  594. /*Updating the buffer size and returnung
  595. from mtpg_read */
  596. r = xfer;
  597. DEBUG_MTPR("[%s] \t %d: returning lenght %d\n",
  598. __func__, __LINE__, r);
  599. goto fail;
  600. }
  601. /* wait for a request to complete */
  602. req = 0;
  603. DEBUG_MTPR("[%s] and line is = %d\n", __func__, __LINE__);
  604. ret = wait_event_interruptible(dev->read_wq,
  605. ((req = mtpg_req_get(dev, &dev->rx_done))
  606. || dev->error));
  607. DEBUG_MTPR("[%s]\t%d: dev->error %d and req = %pK\n",
  608. __func__, __LINE__, dev->error, req);
  609. if (req != 0) {
  610. /* if we got a 0-len one we need to put it back into
  611. ** service. if we made it the current read req we'd
  612. ** be stuck forever
  613. */
  614. if (req->actual == 0)
  615. goto requeue_req;
  616. dev->read_req = req;
  617. dev->read_count = req->actual;
  618. dev->read_buf = req->buf;
  619. DEBUG_MTPR("[%s]\t%d: rx_req=%pK req->actual=%d\n",
  620. __func__, __LINE__, req, req->actual);
  621. }
  622. if (ret < 0) {
  623. r = ret;
  624. printk(KERN_DEBUG "[%s]\t%d after ret=%d brk ret=%d\n",
  625. __func__, __LINE__, ret, r);
  626. break;
  627. }
  628. }
  629. fail:
  630. _unlock(&dev->read_excl);
  631. DEBUG_MTPR("[%s]\t%d: RETURNING Back to USpace r=%d\n",
  632. __func__, __LINE__, r);
  633. return r;
  634. }
  635. static ssize_t mtpg_write(struct file *fp, const char __user *buf,
  636. size_t count, loff_t *pos)
  637. {
  638. struct mtpg_dev *dev = fp->private_data;
  639. struct usb_request *req = 0;
  640. int r = count, xfer;
  641. int ret;
  642. DEBUG_MTPW("[%s] \t%d ep bulk_out name = %s\n",
  643. __func__, __LINE__ , dev->bulk_out->name);
  644. if (_lock(&dev->write_excl))
  645. return -EBUSY;
  646. while (count > 0) {
  647. if (dev->error) {
  648. r = -EIO;
  649. printk(KERN_DEBUG "[%s]%d count>0 dev->error so brk\n",
  650. __func__, __LINE__);
  651. break;
  652. }
  653. /* get an idle tx request to use */
  654. req = 0;
  655. ret = wait_event_interruptible(dev->write_wq,
  656. ((req = mtpg_req_get(dev, &dev->tx_idle))
  657. || dev->error));
  658. if (ret < 0) {
  659. r = ret;
  660. printk(KERN_DEBUG "[%s]\t%d ret = %d\n",
  661. __func__, __LINE__, r);
  662. break;
  663. }
  664. if (req != 0) {
  665. if (count > MTPG_BULK_BUFFER_SIZE)
  666. xfer = MTPG_BULK_BUFFER_SIZE;
  667. else
  668. xfer = count;
  669. DEBUG_MTPW("[%s]\t%d copy_from_user length %d\n",
  670. __func__, __LINE__, xfer);
  671. if (copy_from_user(req->buf, buf, xfer)) {
  672. printk(KERN_ERR "mtpwrite cpyfrmusr error\n");
  673. r = -EFAULT;
  674. break;
  675. }
  676. req->length = xfer;
  677. ret = usb_ep_queue(dev->bulk_in, req, GFP_ATOMIC);
  678. if (ret < 0) {
  679. dev->error = 1;
  680. r = -EIO;
  681. printk(KERN_ERR "[%s]\t%d ep_que ret=%d brk ret=%d\n",
  682. __func__, __LINE__, ret, r);
  683. break;
  684. }
  685. buf += xfer;
  686. count -= xfer;
  687. /* zero this so we don't try to free it on error exit */
  688. req = 0;
  689. }
  690. }
  691. if (req) {
  692. DEBUG_MTPW("[%s] \t%d mtpg_req_put\n", __func__, __LINE__);
  693. mtpg_req_put(dev, &dev->tx_idle, req);
  694. }
  695. _unlock(&dev->write_excl);
  696. DEBUG_MTPW("[%s]\t%d RETURN back to USpace r=%d\n",
  697. __func__, __LINE__, r);
  698. return r;
  699. }
  700. static ssize_t interrupt_write(struct file *fd,
  701. struct mtp_event *event, size_t count)
  702. {
  703. struct mtpg_dev *dev = fd->private_data;
  704. struct usb_request *req = 0;
  705. int ret;
  706. DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
  707. if (count > MTPG_INTR_BUFFER_SIZE)
  708. return -EINVAL;
  709. ret = wait_event_interruptible_timeout(dev->intr_wq,
  710. (req = mtpg_req_get(dev, &dev->intr_idle)),
  711. msecs_to_jiffies(1000));
  712. if (!req) {
  713. printk(KERN_ERR "[%s]Alloc has failed\n", __func__);
  714. return -ENOMEM;
  715. }
  716. if (copy_from_user(req->buf, (void __user *)event->data, count)) {
  717. mtpg_req_put(dev, &dev->intr_idle, req);
  718. printk(KERN_ERR "[%s]copy from user has failed\n", __func__);
  719. return -EIO;
  720. }
  721. req->length = count;
  722. /*req->complete = interrupt_complete;*/
  723. ret = usb_ep_queue(dev->int_in, req, GFP_KERNEL);
  724. if (ret) {
  725. printk(KERN_ERR "[%s:%d]\n", __func__, __LINE__);
  726. mtpg_req_put(dev, &dev->intr_idle, req);
  727. }
  728. DEBUG_MTPB("[%s] \tline = [%d] returning ret is %d\\n",
  729. __func__, __LINE__, ret);
  730. return ret;
  731. }
  732. static void mtp_complete_ep0_transection(struct usb_ep *ep, struct usb_request *req)
  733. {
  734. if (req->status || req->actual != req->length) {
  735. DEBUG_MTPB("[%s]\tline = [%d]\n", __func__, __LINE__);
  736. }
  737. }
  738. static void read_send_work(struct work_struct *work)
  739. {
  740. struct mtpg_dev *dev = container_of(work, struct mtpg_dev,
  741. read_send_work);
  742. struct usb_request *req = 0;
  743. struct usb_container_header *hdr;
  744. struct file *file;
  745. loff_t file_pos = 0;
  746. int64_t count = 0;
  747. int xfer = 0;
  748. int ret = -1;
  749. int hdr_length = 0;
  750. int r = 0;
  751. int ZLP_flag = 0;
  752. /* read our parameters */
  753. smp_rmb();
  754. file = dev->read_send_file;
  755. count = dev->read_send_length;
  756. hdr_length = sizeof(struct usb_container_header);
  757. count += hdr_length;
  758. printk(KERN_DEBUG "[%s:%d] offset=[%lld]\t leth+hder=[%lld]\n",
  759. __func__, __LINE__, file_pos, count);
  760. /* Zero Length Packet should be sent if the last trasfer
  761. * size is equals to the max packet size.
  762. */
  763. if ((count & (dev->bulk_in->maxpacket - 1)) == 0)
  764. ZLP_flag = 1;
  765. while (count > 0 || ZLP_flag) {
  766. /*Breaking the loop after sending Zero Length Packet*/
  767. if (count == 0)
  768. ZLP_flag = 0;
  769. if (dev->cancel_io == 1) {
  770. dev->cancel_io = 0; /*reported to user space*/
  771. r = -EIO;
  772. printk(KERN_DEBUG "[%s]\t%d ret = %d\n",
  773. __func__, __LINE__, r);
  774. break;
  775. }
  776. /* get an idle tx request to use */
  777. req = 0;
  778. ret = wait_event_interruptible(dev->write_wq,
  779. ((req = mtpg_req_get(dev, &dev->tx_idle))
  780. || dev->error));
  781. if (ret < 0 || !req) {
  782. r = ret;
  783. printk(KERN_DEBUG "[%s]\t%d ret = %d\n",
  784. __func__, __LINE__, r);
  785. break;
  786. }
  787. if (count > MTPG_BULK_BUFFER_SIZE)
  788. xfer = MTPG_BULK_BUFFER_SIZE;
  789. else
  790. xfer = count;
  791. if (hdr_length) {
  792. hdr = (struct usb_container_header *)req->buf;
  793. hdr->Length = __cpu_to_le32(count);
  794. hdr->Type = __cpu_to_le16(2);
  795. hdr->Code = __cpu_to_le16(dev->read_send_cmd);
  796. hdr->TransactionID = __cpu_to_le32(dev->read_send_id);
  797. }
  798. ret = vfs_read(file, req->buf + hdr_length,
  799. xfer - hdr_length, &file_pos);
  800. if (ret < 0 || !req) {
  801. r = ret;
  802. printk(KERN_DEBUG "[%s]\t%d ret = %d\n",
  803. __func__, __LINE__, r);
  804. break;
  805. }
  806. xfer = ret + hdr_length;
  807. hdr_length = 0;
  808. req->length = xfer;
  809. ret = usb_ep_queue(dev->bulk_in, req, GFP_KERNEL);
  810. if (ret < 0 || !req) {
  811. dev->error = 1;
  812. r = -EIO;
  813. printk(KERN_DEBUG "[%s]\t%d ret = %d\n",
  814. __func__, __LINE__, r);
  815. break;
  816. }
  817. count -= xfer;
  818. req = 0;
  819. }
  820. if (req)
  821. mtpg_req_put(dev, &dev->tx_idle, req);
  822. DEBUG_MTPB("[%s] \tline = [%d] \t r = [%d]\n", __func__, __LINE__, r);
  823. dev->read_send_result = r;
  824. smp_wmb();
  825. }
  826. static long mtpg_ioctl(struct file *fd, unsigned int code, unsigned long arg)
  827. {
  828. struct mtpg_dev *dev = fd->private_data;
  829. struct mtp_event event;
  830. struct usb_composite_dev *cdev;
  831. struct usb_request *req;
  832. int status = 0;
  833. int size = 0;
  834. int ret_value = 0;
  835. int max_pkt = 0;
  836. char *buf_ptr = NULL;
  837. char buf[USB_PTPREQUEST_GETSTATUS_SIZE+1] = {0};
  838. cdev = dev->cdev;
  839. if (!cdev) {
  840. printk(KERN_ERR "usb: %s cdev not ready\n", __func__);
  841. return -EAGAIN;
  842. }
  843. req = cdev->req;
  844. if (!cdev->req) {
  845. printk(KERN_ERR "usb: %s cdev->req not ready\n", __func__);
  846. return -EAGAIN;
  847. }
  848. DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
  849. switch (code) {
  850. case MTP_ONLY_ENABLE:
  851. printk(KERN_DEBUG "[%s:%d] MTP_ONLY_ENABLE ioctl:\n",
  852. __func__, __LINE__);
  853. if (dev->cdev && dev->cdev->gadget) {
  854. usb_gadget_disconnect(cdev->gadget);
  855. printk(KERN_DEBUG "[%s:%d] B4 disconectng gadget\n",
  856. __func__, __LINE__);
  857. msleep(20);
  858. usb_gadget_connect(cdev->gadget);
  859. printk(KERN_DEBUG "[%s:%d] after usb_gadget_connect\n",
  860. __func__, __LINE__);
  861. }
  862. status = 10;
  863. printk(KERN_DEBUG "[%s:%d] MTP_ONLY_ENABLE clearing error 0\n",
  864. __func__, __LINE__);
  865. the_mtpg->error = 0;
  866. break;
  867. case MTP_DISABLE:
  868. /*mtp_function_enable(mtp_disable_desc);*/
  869. if (dev->cdev && dev->cdev->gadget) {
  870. usb_gadget_disconnect(dev->cdev->gadget);
  871. mdelay(5);
  872. usb_gadget_connect(dev->cdev->gadget);
  873. }
  874. break;
  875. case MTP_CLEAR_HALT:
  876. status = usb_ep_clear_halt(dev->bulk_in);
  877. status = usb_ep_clear_halt(dev->bulk_out);
  878. break;
  879. case MTP_WRITE_INT_DATA:
  880. printk(KERN_INFO "[%s]\t%d MTP intrpt_Write no slep\n",
  881. __func__, __LINE__);
  882. if (copy_from_user(&event, (void __user *)arg, sizeof(event))){
  883. status = -EFAULT;
  884. printk(KERN_ERR "[%s]\t%d:copyfrmuser fail\n",
  885. __func__, __LINE__);
  886. break;
  887. }
  888. ret_value = interrupt_write(fd, &event, MTP_MAX_PACKET_LEN_FROM_APP);
  889. if (ret_value < 0) {
  890. printk(KERN_ERR "[%s]\t%d interptFD failed\n",
  891. __func__, __LINE__);
  892. status = -EIO;
  893. } else {
  894. printk(KERN_DEBUG "[%s]\t%d intruptFD suces\n",
  895. __func__, __LINE__);
  896. status = MTP_MAX_PACKET_LEN_FROM_APP;
  897. }
  898. break;
  899. case SET_MTP_USER_PID:
  900. mtp_pid = arg;
  901. printk(KERN_DEBUG "[%s]SET_MTP_USER_PID;pid=%d\tline=[%d]\n",
  902. __func__, mtp_pid, __LINE__);
  903. break;
  904. case GET_SETUP_DATA:
  905. buf_ptr = (char *)arg;
  906. printk(KERN_DEBUG "[%s] GET_SETUP_DATA\tline = [%d]\n",
  907. __func__, __LINE__);
  908. if (copy_to_user(buf_ptr, dev->cancel_io_buf,
  909. USB_PTPREQUEST_CANCELIO_SIZE)) {
  910. status = -EIO;
  911. printk(KERN_ERR "[%s]\t%d:coptousr failed\n",
  912. __func__, __LINE__);
  913. }
  914. break;
  915. case SEND_RESET_ACK:
  916. /*req->zero = 1;*/
  917. req->length = 0;
  918. /*printk(KERN_DEBUG "[%s]SEND_RESET_ACK and usb_ep_queu
  919. ZERO data size = %d\tline=[%d]\n",
  920. __func__, size, __LINE__);*/
  921. req->complete = mtp_complete_ep0_transection;
  922. status = usb_ep_queue(cdev->gadget->ep0,
  923. req, GFP_ATOMIC);
  924. if (status < 0)
  925. printk(KERN_ERR "[%s]ep_queue line = [%d]\n",
  926. __func__, __LINE__);
  927. break;
  928. case SET_SETUP_DATA:
  929. buf_ptr = (char *)arg;
  930. if (copy_from_user(buf, buf_ptr,
  931. USB_PTPREQUEST_GETSTATUS_SIZE)) {
  932. status = -EIO;
  933. printk(KERN_ERR "[%s]\t%d:copyfrmuser fail\n",
  934. __func__, __LINE__);
  935. break;
  936. }
  937. size = buf[0];
  938. printk(KERN_DEBUG "[%s]SET_SETUP_DATA size=%d line=[%d]\n",
  939. __func__, size, __LINE__);
  940. memcpy(req->buf, buf, size);
  941. req->zero = 0;
  942. req->length = size;
  943. req->complete = mtp_complete_ep0_transection;
  944. status = usb_ep_queue(cdev->gadget->ep0, req,
  945. GFP_ATOMIC);
  946. if (status < 0)
  947. printk(KERN_ERR "[%s]usbepqueue line=[%d]\n",
  948. __func__, __LINE__);
  949. break;
  950. case SET_ZLP_DATA:
  951. /*req->zero = 1;*/
  952. req = mtpg_req_get(dev, &dev->tx_idle);
  953. if (!req) {
  954. printk(KERN_DEBUG "[%s] Failed to get ZLP_DATA\n",
  955. __func__);
  956. return -EAGAIN;
  957. }
  958. req->length = 0;
  959. printk(KERN_DEBUG "[%s]ZLP_DATA data=%d\tline=[%d]\n",
  960. __func__, size, __LINE__);
  961. status = usb_ep_queue(dev->bulk_in, req, GFP_ATOMIC);
  962. if (status < 0) {
  963. printk(KERN_ERR "[%s]usbepqueue line=[%d]\n",
  964. __func__, __LINE__);
  965. } else {
  966. printk(KERN_DEBUG "%sZLPstatus=%d\tline=%d\n",
  967. __func__, __LINE__, status);
  968. status = 20;
  969. }
  970. break;
  971. case GET_HIGH_FULL_SPEED:
  972. printk(KERN_DEBUG "[%s]GET_HIGH_FULLSPEED line=[%d]\n",
  973. __func__, __LINE__);
  974. max_pkt = dev->bulk_in->maxpacket;
  975. printk(KERN_DEBUG "[%s] line = %d max_pkt = [%d]\n",
  976. __func__, __LINE__, max_pkt);
  977. if (max_pkt == 64)
  978. status = 64;
  979. else
  980. status = 512;
  981. break;
  982. case SEND_FILE_WITH_HEADER:
  983. {
  984. struct read_send_info info;
  985. struct work_struct *work;
  986. struct file *file = NULL;
  987. printk(KERN_DEBUG "[%s]SEND_FILE_WITH_HEADER line=[%d]\n",
  988. __func__, __LINE__);
  989. if (copy_from_user(&info, (void __user *)arg, sizeof(info))) {
  990. status = -EFAULT;
  991. goto exit;
  992. }
  993. file = fget(info.Fd);
  994. if (!file) {
  995. status = -EBADF;
  996. printk(KERN_DEBUG "[%s] line=[%d] bad file number\n",
  997. __func__, __LINE__);
  998. goto exit;
  999. }
  1000. dev->read_send_file = file;
  1001. dev->read_send_length = info.Length;
  1002. smp_wmb();
  1003. work = &dev->read_send_work;
  1004. dev->read_send_cmd = info.Code;
  1005. dev->read_send_id = info.TransactionID;
  1006. queue_work(dev->wq, work);
  1007. /* Wait for the work to be complted on work queue */
  1008. flush_workqueue(dev->wq);
  1009. fput(file);
  1010. smp_rmb();
  1011. status = dev->read_send_result;
  1012. break;
  1013. }
  1014. case MTP_VBUS_DISABLE:
  1015. printk(KERN_DEBUG "[%s] line=[%d] \n",
  1016. __func__, __LINE__);
  1017. if (dev->cdev && dev->cdev->gadget) {
  1018. usb_gadget_vbus_disconnect(cdev->gadget);
  1019. printk(KERN_DEBUG "Restricted policy so disconnecting mtp gadget\n");
  1020. }
  1021. break;
  1022. default:
  1023. status = -ENOTTY;
  1024. }
  1025. exit:
  1026. return status;
  1027. }
  1028. static int mtpg_release_device(struct inode *ip, struct file *fp)
  1029. {
  1030. printk(KERN_DEBUG "[%s]\tline = [%d]\n", __func__, __LINE__);
  1031. if (the_mtpg != NULL)
  1032. _unlock(&the_mtpg->open_excl);
  1033. return 0;
  1034. }
  1035. /* file operations for MTP device /dev/usb_mtp_gadget */
  1036. static const struct file_operations mtpg_fops = {
  1037. .owner = THIS_MODULE,
  1038. .read = mtpg_read,
  1039. .write = mtpg_write,
  1040. .open = mtpg_open,
  1041. .unlocked_ioctl = mtpg_ioctl,
  1042. .release = mtpg_release_device,
  1043. };
  1044. static struct miscdevice mtpg_device = {
  1045. .minor = MISC_DYNAMIC_MINOR,
  1046. .name = shortname,
  1047. .fops = &mtpg_fops,
  1048. };
  1049. struct usb_request *alloc_ep_req(struct usb_ep *ep,
  1050. unsigned len, gfp_t kmalloc_flags)
  1051. {
  1052. struct usb_request *req;
  1053. DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
  1054. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  1055. if (req) {
  1056. req->length = len;
  1057. req->buf = kmalloc(len, GFP_ATOMIC);
  1058. if (!req->buf) {
  1059. usb_ep_free_request(ep, req);
  1060. req = NULL;
  1061. }
  1062. }
  1063. return req;
  1064. }
  1065. static void mtpg_request_free(struct usb_request *req, struct usb_ep *ep)
  1066. {
  1067. DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
  1068. if (req) {
  1069. kfree(req->buf);
  1070. usb_ep_free_request(ep, req);
  1071. }
  1072. }
  1073. static struct usb_request *mtpg_request_new(struct usb_ep *ep, int buffer_size)
  1074. {
  1075. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  1076. DEBUG_MTPB("[%s] \tline = [%d]\n", __func__, __LINE__);
  1077. if (!req) {
  1078. printk(KERN_ERR "[%s]\tline %d ERROR\n", __func__, __LINE__);
  1079. return NULL;
  1080. }
  1081. /* now allocate buffers for the requests */
  1082. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  1083. if (!req->buf) {
  1084. usb_ep_free_request(ep, req);
  1085. return NULL;
  1086. }
  1087. return req;
  1088. }
  1089. static void mtpg_complete_in(struct usb_ep *ep, struct usb_request *req)
  1090. {
  1091. struct mtpg_dev *dev = the_mtpg;
  1092. DEBUG_MTPB("[%s]\t %d req->status is = %d\n",
  1093. __func__, __LINE__, req->status);
  1094. if (req->status != 0)
  1095. dev->error = 1;
  1096. mtpg_req_put(dev, &dev->tx_idle, req);
  1097. wake_up(&dev->write_wq);
  1098. }
  1099. static void mtpg_complete_out(struct usb_ep *ep, struct usb_request *req)
  1100. {
  1101. struct mtpg_dev *dev = the_mtpg;
  1102. DEBUG_MTPB("[%s]\tline = [%d]req->status is = %d\n",
  1103. __func__, __LINE__, req->status);
  1104. if (req->status != 0) {
  1105. dev->error = 1;
  1106. DEBUG_MTPB("[%s]\t%d dev->error is=%d for rx_idle\n",
  1107. __func__, __LINE__, dev->error);
  1108. mtpg_req_put(dev, &dev->rx_idle, req);
  1109. } else {
  1110. DEBUG_MTPB("[%s]\t%d for rx_done\n", __func__, __LINE__);
  1111. mtpg_req_put(dev, &dev->rx_done, req);
  1112. }
  1113. wake_up(&dev->read_wq);
  1114. }
  1115. static void mtpg_complete_intr(struct usb_ep *ep, struct usb_request *req)
  1116. {
  1117. struct mtpg_dev *dev = the_mtpg;
  1118. /*printk(KERN_INFO "[%s]\tline = [%d]\n", __func__, __LINE__);*/
  1119. if (req->status != 0)
  1120. dev->error = 1;
  1121. mtpg_req_put(dev, &dev->intr_idle, req);
  1122. wake_up(&dev->intr_wq);
  1123. }
  1124. static DEVICE_ATTR(guid, S_IRUGO | S_IWUSR,
  1125. guid_show, guid_store);
  1126. static void
  1127. mtpg_function_unbind(struct usb_configuration *c, struct usb_function *f)
  1128. {
  1129. struct mtpg_dev *dev = mtpg_func_to_dev(f);
  1130. struct usb_request *req;
  1131. printk(KERN_DEBUG "[%s]\tline = [%d]\n", __func__, __LINE__);
  1132. while ((req = mtpg_req_get(dev, &dev->rx_idle)))
  1133. mtpg_request_free(req, dev->bulk_out);
  1134. while ((req = mtpg_req_get(dev, &dev->tx_idle)))
  1135. mtpg_request_free(req, dev->bulk_in);
  1136. while ((req = mtpg_req_get(dev, &dev->intr_idle)))
  1137. mtpg_request_free(req, dev->int_in);
  1138. memset(guid_info, 0, sizeof (guid_info));
  1139. //printk(KERN_DEBUG "mtp: %s guid after reset = %s\n", __func__, guid_info);
  1140. }
  1141. static int
  1142. mtpg_function_bind(struct usb_configuration *c, struct usb_function *f)
  1143. {
  1144. struct usb_composite_dev *cdev = c->cdev;
  1145. struct mtpg_dev *mtpg = mtpg_func_to_dev(f);
  1146. struct usb_request *req;
  1147. struct usb_ep *ep;
  1148. int i, id;
  1149. /* Allocate string descriptor numbers ... note that string
  1150. * contents can be overridden by the composite_dev glue.
  1151. */
  1152. printk(KERN_DEBUG "[%s]\tline = [%d]\n", __func__, __LINE__);
  1153. id = usb_interface_id(c, f);
  1154. if (id < 0) {
  1155. printk(KERN_ERR "[%s]Error in usb_interface_id\n", __func__);
  1156. return id;
  1157. }
  1158. mtpg_interface_desc.bInterfaceNumber = id;
  1159. ep = usb_ep_autoconfig(cdev->gadget, &fs_mtpg_in_desc);
  1160. if (!ep) {
  1161. printk(KERN_ERR "[%s]Error usb_ep_autoconfig IN\n", __func__);
  1162. goto autoconf_fail;
  1163. }
  1164. ep->driver_data = mtpg; /* claim the endpoint */
  1165. mtpg->bulk_in = ep;
  1166. the_mtpg->bulk_in = ep;
  1167. ep = usb_ep_autoconfig(cdev->gadget, &fs_mtpg_out_desc);
  1168. if (!ep) {
  1169. printk(KERN_ERR "[%s]Eror usb_ep_autoconfig OUT\n", __func__);
  1170. goto autoconf_fail;
  1171. }
  1172. ep->driver_data = mtpg; /* claim the endpoint */
  1173. mtpg->bulk_out = ep;
  1174. the_mtpg->bulk_out = ep;
  1175. /* Interrupt Support for MTP */
  1176. ep = usb_ep_autoconfig(cdev->gadget, &int_fs_notify_desc);
  1177. if (!ep) {
  1178. printk(KERN_ERR "[%s]Eror usb_ep_autoconfig INT\n", __func__);
  1179. goto autoconf_fail;
  1180. }
  1181. ep->driver_data = mtpg;
  1182. mtpg->int_in = ep;
  1183. the_mtpg->int_in = ep;
  1184. for (i = 0; i < MTPG_INTR_REQ_MAX; i++) {
  1185. req = mtpg_request_new(mtpg->int_in, MTPG_INTR_BUFFER_SIZE);
  1186. if (!req)
  1187. goto out;
  1188. req->complete = mtpg_complete_intr;
  1189. mtpg_req_put(mtpg, &mtpg->intr_idle, req);
  1190. }
  1191. for (i = 0; i < MTPG_RX_REQ_MAX; i++) {
  1192. req = mtpg_request_new(mtpg->bulk_out, MTPG_BULK_BUFFER_SIZE);
  1193. if (!req)
  1194. goto out;
  1195. req->complete = mtpg_complete_out;
  1196. mtpg_req_put(mtpg, &mtpg->rx_idle, req);
  1197. }
  1198. for (i = 0; i < MTPG_MTPG_TX_REQ_MAX; i++) {
  1199. req = mtpg_request_new(mtpg->bulk_in, MTPG_BULK_BUFFER_SIZE);
  1200. if (!req)
  1201. goto out;
  1202. req->complete = mtpg_complete_in;
  1203. mtpg_req_put(mtpg, &mtpg->tx_idle, req);
  1204. }
  1205. if (gadget_is_dualspeed(cdev->gadget)) {
  1206. DEBUG_MTPB("[%s]\tdual speed line = [%d]\n",
  1207. __func__, __LINE__);
  1208. /* Assume endpoint addresses are the same for both speeds */
  1209. hs_mtpg_in_desc.bEndpointAddress =
  1210. fs_mtpg_in_desc.bEndpointAddress;
  1211. hs_mtpg_out_desc.bEndpointAddress =
  1212. fs_mtpg_out_desc.bEndpointAddress;
  1213. int_hs_notify_desc.bEndpointAddress =
  1214. int_fs_notify_desc.bEndpointAddress;
  1215. }
  1216. if (gadget_is_superspeed(c->cdev->gadget)) {
  1217. DEBUG_MTPB("[%s]\tdual speed line = [%d]\n",
  1218. __func__, __LINE__);
  1219. mtpg_superspeed_in_desc.bEndpointAddress =
  1220. fs_mtpg_in_desc.bEndpointAddress;
  1221. mtpg_superspeed_out_desc.bEndpointAddress =
  1222. fs_mtpg_out_desc.bEndpointAddress;
  1223. }
  1224. printk("%s speed %s: IN/%s, OUT/%s\n",
  1225. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  1226. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  1227. f->name, mtpg->bulk_in->name, mtpg->bulk_out->name);
  1228. mtpg->cdev = cdev;
  1229. the_mtpg->cdev = cdev;
  1230. return 0;
  1231. autoconf_fail:
  1232. printk(KERN_ERR "mtpg unable to autoconfigure all endpoints\n");
  1233. return -ENOTSUPP;
  1234. out:
  1235. mtpg_function_unbind(c, f);
  1236. return -1;
  1237. }
  1238. static int mtpg_function_set_alt(struct usb_function *f,
  1239. unsigned intf, unsigned alt)
  1240. {
  1241. struct mtpg_dev *dev = mtpg_func_to_dev(f);
  1242. struct usb_composite_dev *cdev = f->config->cdev;
  1243. int ret;
  1244. if (dev->int_in->driver_data)
  1245. usb_ep_disable(dev->int_in);
  1246. ret = config_ep_by_speed(cdev->gadget, f, dev->int_in);
  1247. if (ret) {
  1248. dev->int_in->desc = NULL;
  1249. ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
  1250. dev->int_in->name, ret);
  1251. return ret;
  1252. }
  1253. ret = usb_ep_enable(dev->int_in);
  1254. if (ret) {
  1255. ERROR(cdev, "failed to enable ep %s, result %d\n",
  1256. dev->int_in->name, ret);
  1257. return ret;
  1258. }
  1259. dev->int_in->driver_data = dev;
  1260. ret = config_ep_by_speed(cdev->gadget, f, dev->bulk_in);
  1261. if (ret) {
  1262. dev->bulk_in->desc = NULL;
  1263. ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
  1264. dev->bulk_in->name, ret);
  1265. return ret;
  1266. }
  1267. ret = usb_ep_enable(dev->bulk_in);
  1268. if (ret) {
  1269. ERROR(cdev, "failed to enable ep %s, result %d\n",
  1270. dev->bulk_in->name, ret);
  1271. return ret;
  1272. }
  1273. dev->bulk_in->driver_data = dev;
  1274. ret = config_ep_by_speed(cdev->gadget, f, dev->bulk_out);
  1275. if (ret) {
  1276. dev->bulk_out->desc = NULL;
  1277. ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
  1278. dev->bulk_out->name, ret);
  1279. return ret;
  1280. }
  1281. ret = usb_ep_enable(dev->bulk_out);
  1282. if (ret) {
  1283. ERROR(cdev, "failed to enable ep %s, result %d\n",
  1284. dev->bulk_out->name, ret);
  1285. return ret;
  1286. }
  1287. dev->bulk_out->driver_data = dev;
  1288. dev->online = 1;
  1289. dev->error = 0;
  1290. dev->read_ready = 1;
  1291. dev->cancel_io = 0;
  1292. /* readers may be blocked waiting for us to go online */
  1293. wake_up(&dev->read_wq);
  1294. return 0;
  1295. }
  1296. static void mtpg_function_disable(struct usb_function *f)
  1297. {
  1298. struct mtpg_dev *dev = mtpg_func_to_dev(f);
  1299. printk(KERN_DEBUG "[%s]\tline = [%d]\n", __func__, __LINE__);
  1300. memset(guid_info, 0, sizeof (guid_info));
  1301. //printk(KERN_DEBUG "mtp: %s guid after reset = %s\n", __func__, guid_info);
  1302. dev->online = 0;
  1303. dev->error = 1;
  1304. usb_ep_disable(dev->int_in);
  1305. dev->int_in->driver_data = NULL;
  1306. usb_ep_disable(dev->bulk_in);
  1307. dev->bulk_in->driver_data = NULL;
  1308. usb_ep_disable(dev->bulk_out);
  1309. dev->bulk_out->driver_data = NULL;
  1310. wake_up(&dev->read_wq);
  1311. }
  1312. /*PIMA15740-2000 spec: Class specific setup request for MTP*/
  1313. static void
  1314. mtp_complete_cancel_io(struct usb_ep *ep, struct usb_request *req)
  1315. {
  1316. int i;
  1317. struct mtpg_dev *dev = ep->driver_data;
  1318. DEBUG_MTPB("[%s]\tline = [%d]\n", __func__, __LINE__);
  1319. if (req->status != 0) {
  1320. DEBUG_MTPB("[%s]req->status !=0\tline = [%d]\n",
  1321. __func__, __LINE__);
  1322. return;
  1323. }
  1324. if (req->actual != USB_PTPREQUEST_CANCELIO_SIZE) {
  1325. DEBUG_MTPB("[%s]USB_PTPREQUEST_CANCELIO_SIZE line = [%d]\n",
  1326. __func__, __LINE__);
  1327. usb_ep_set_halt(ep);
  1328. } else {
  1329. memset(dev->cancel_io_buf, 0, USB_PTPREQUEST_CANCELIO_SIZE+1);
  1330. memcpy(dev->cancel_io_buf, req->buf,
  1331. USB_PTPREQUEST_CANCELIO_SIZE);
  1332. dev->cancel_io = 1;
  1333. /*Debugging*/
  1334. for (i = 0; i < USB_PTPREQUEST_CANCELIO_SIZE; i++)
  1335. DEBUG_MTPB("[%s]cancel_io_buf[%d]=%x\tline = [%d]\n",
  1336. __func__, i, dev->cancel_io_buf[i], __LINE__);
  1337. mtp_send_signal(USB_PTPREQUEST_CANCELIO);
  1338. }
  1339. }
  1340. static void
  1341. mtp_complete_get_guid(struct usb_ep *ep, struct usb_request *req)
  1342. {
  1343. int size;
  1344. printk(KERN_DEBUG "[%s]\tline = [%d]\n", __func__, __LINE__);
  1345. DEBUG_MTPB("[%s]\tline = [%d]\n", __func__, __LINE__);
  1346. if (req->status != 0) {
  1347. DEBUG_MTPB("[%s]req->status !=0\tline = [%d]\n",
  1348. __func__, __LINE__);
  1349. return;
  1350. }
  1351. if (req->actual >= sizeof(guid_info)) {
  1352. size = sizeof(guid_info)-1;
  1353. }
  1354. else
  1355. size = req->actual;
  1356. //printk(KERN_DEBUG "mtp: %s guid = %s\n", __func__, guid_info);
  1357. memset(guid_info, 0, sizeof(guid_info));
  1358. //printk(KERN_DEBUG "mtp: %s guid after reset = %s\n", __func__, guid_info);
  1359. memcpy(guid_info, req->buf, size);
  1360. }
  1361. static int mtp_ctrlrequest(struct usb_composite_dev *cdev,
  1362. const struct usb_ctrlrequest *ctrl)
  1363. {
  1364. struct mtpg_dev *dev = the_mtpg;
  1365. struct usb_request *req = cdev->req;
  1366. int signal_request = 0;
  1367. int value = -EOPNOTSUPP;
  1368. u16 w_index = le16_to_cpu(ctrl->wIndex);
  1369. u16 w_value = le16_to_cpu(ctrl->wValue);
  1370. u16 w_length = le16_to_cpu(ctrl->wLength);
  1371. if (ctrl->bRequestType ==
  1372. (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  1373. && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
  1374. && (w_value >> 8) == USB_DT_STRING
  1375. && (w_value & 0xFF) == MTPG_OS_STRING_ID) {
  1376. value = (w_length < sizeof(mtpg_os_string)
  1377. ? w_length : sizeof(mtpg_os_string));
  1378. memcpy(cdev->req->buf, mtpg_os_string, value);
  1379. if (value >= 0) {
  1380. int rc;
  1381. cdev->req->zero = value < w_length;
  1382. cdev->req->length = value;
  1383. rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
  1384. if (rc < 0)
  1385. printk(KERN_DEBUG "[%s:%d] setup queue error\n",
  1386. __func__, __LINE__);
  1387. }
  1388. return value;
  1389. } else if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  1390. if ((ctrl->bRequest == 1 || ctrl->bRequest == 0x54 ||
  1391. ctrl->bRequest == 0x6F || ctrl->bRequest == 0xFE)
  1392. && (ctrl->bRequestType & USB_DIR_IN)
  1393. && (w_index == 4 || w_index == 5)) {
  1394. value = (w_length < sizeof(mtpg_ext_config_desc) ?
  1395. w_length : sizeof(mtpg_ext_config_desc));
  1396. memcpy(cdev->req->buf, &mtpg_ext_config_desc, value);
  1397. if (value >= 0) {
  1398. int rc;
  1399. cdev->req->zero = value < w_length;
  1400. cdev->req->length = value;
  1401. rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
  1402. if (rc < 0)
  1403. printk(KERN_DEBUG "[%s:%d] setup queue error\n",
  1404. __func__, __LINE__);
  1405. }
  1406. return value;
  1407. }
  1408. else if ((ctrl->bRequest == 0xA3) && ( !(ctrl->bRequestType & USB_DIR_IN)))
  1409. {
  1410. printk("mtp: [%s] RECEIVE PC GUID / line[%d]\n",
  1411. __func__, __LINE__);
  1412. value = w_length;
  1413. cdev->gadget->ep0->driver_data = dev;
  1414. req->complete = mtp_complete_get_guid;
  1415. req->zero = 0;
  1416. req->length = value;
  1417. value = usb_ep_queue(cdev->gadget->ep0,
  1418. req, GFP_ATOMIC);
  1419. if (value < 0) {
  1420. printk(KERN_ERR "[%s:%d]Error usb_ep_queue\n",
  1421. __func__, __LINE__);
  1422. } else
  1423. DEBUG_MTPB("[%s] ep-queue-sucecc line[%d]\n",
  1424. __func__, __LINE__);
  1425. }
  1426. printk(KERN_DEBUG "mtp: mtp_ctrlrequest "\
  1427. "%02x.%02x v%04x i%04x l%u\n",
  1428. ctrl->bRequestType, ctrl->bRequest,
  1429. w_value, w_index, w_length);
  1430. }
  1431. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  1432. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  1433. | USB_PTPREQUEST_CANCELIO:
  1434. DEBUG_MTPB("[%s]\tline=[%d]w_v=%x, w_i=%x, w_l=%x\n",
  1435. __func__, __LINE__, w_value, w_index, w_length);
  1436. /* if (w_value == 0x00 && w_index ==
  1437. mtpg_interface_desc.bInterfaceNumber
  1438. && w_length == 0x06) */
  1439. if (w_value == 0x00 && w_length == 0x06) {
  1440. DEBUG_MTPB("[%s]PTPREQUESTCANCLIO line[%d]\n",
  1441. __func__, __LINE__);
  1442. value = w_length;
  1443. cdev->gadget->ep0->driver_data = dev;
  1444. req->complete = mtp_complete_cancel_io;
  1445. req->zero = 0;
  1446. req->length = value;
  1447. value = usb_ep_queue(cdev->gadget->ep0,
  1448. req, GFP_ATOMIC);
  1449. if (value < 0) {
  1450. printk(KERN_ERR "[%s:%d]Error usb_ep_queue\n",
  1451. __func__, __LINE__);
  1452. } else
  1453. DEBUG_MTPB("[%s] ep-queue-sucecc line[%d]\n",
  1454. __func__, __LINE__);
  1455. }
  1456. return value;
  1457. break;
  1458. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  1459. | USB_PTPREQUEST_RESET:
  1460. DEBUG_MTPB("[%s] USB_PTPREQUEST_RESET\tline = [%d]\n",
  1461. __func__, __LINE__);
  1462. signal_request = USB_PTPREQUEST_RESET;
  1463. break;
  1464. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  1465. | USB_PTPREQUEST_GETSTATUS:
  1466. signal_request = USB_PTPREQUEST_GETSTATUS;
  1467. break;
  1468. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  1469. | USB_PTPREQUEST_GETEVENT:
  1470. signal_request = USB_PTPREQUEST_GETEVENT;
  1471. break;
  1472. default:
  1473. DEBUG_MTPB("[%s] INVALID REQUEST \tline = [%d]\n",
  1474. __func__, __LINE__);
  1475. return value;
  1476. }
  1477. value = mtp_send_signal(signal_request);
  1478. return value;
  1479. }
  1480. static int mtp_bind_config(struct usb_configuration *c, bool ptp_config)
  1481. {
  1482. struct mtpg_dev *mtpg = the_mtpg;
  1483. int status = 0;
  1484. if (strings_dev_mtp[F_MTP_IDX].id == 0) {
  1485. status = usb_string_id(c->cdev);
  1486. if (status < 0)
  1487. return status;
  1488. strings_dev_mtp[F_MTP_IDX].id = status;
  1489. mtpg_interface_desc.iInterface = status;
  1490. }
  1491. mtpg->cdev = c->cdev;
  1492. mtpg->function.name = mtpg_longname;
  1493. mtpg->function.strings = mtpg_dev_strings;
  1494. /*Test the switch */
  1495. if (ptp_config) {
  1496. mtpg->function.descriptors = fs_ptp_descs;
  1497. mtpg->function.hs_descriptors = hs_ptp_descs;
  1498. if (gadget_is_superspeed(c->cdev->gadget))
  1499. mtpg->function.ss_descriptors = ss_ptpg_descs;
  1500. } else {
  1501. mtpg->function.descriptors = fs_mtpg_desc;
  1502. mtpg->function.hs_descriptors = hs_mtpg_desc;
  1503. if (gadget_is_superspeed(c->cdev->gadget))
  1504. mtpg->function.ss_descriptors = ss_mtpg_descs;
  1505. }
  1506. mtpg->function.bind = mtpg_function_bind;
  1507. mtpg->function.unbind = mtpg_function_unbind;
  1508. mtpg->function.set_alt = mtpg_function_set_alt;
  1509. mtpg->function.disable = mtpg_function_disable;
  1510. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  1511. mtpg->function.set_config_desc = mtp_set_config_desc;
  1512. #endif
  1513. return usb_add_function(c, &mtpg->function);
  1514. }
  1515. static int mtp_setup(void)
  1516. {
  1517. struct mtpg_dev *mtpg;
  1518. int rc;
  1519. int err;
  1520. printk(KERN_DEBUG "[%s] \tline = [%d]\n", __func__, __LINE__);
  1521. memset(guid_info, '0', 1);
  1522. mtpg = kzalloc(sizeof(*mtpg), GFP_KERNEL);
  1523. if (!mtpg) {
  1524. printk(KERN_ERR "mtpg_dev_alloc memory failed\n");
  1525. return -ENOMEM;
  1526. }
  1527. spin_lock_init(&mtpg->lock);
  1528. init_waitqueue_head(&mtpg->intr_wq);
  1529. init_waitqueue_head(&mtpg->read_wq);
  1530. init_waitqueue_head(&mtpg->write_wq);
  1531. atomic_set(&mtpg->open_excl, 0);
  1532. atomic_set(&mtpg->read_excl, 0);
  1533. atomic_set(&mtpg->write_excl, 0);
  1534. atomic_set(&mtpg->wintfd_excl, 0);
  1535. INIT_LIST_HEAD(&mtpg->rx_idle);
  1536. INIT_LIST_HEAD(&mtpg->rx_done);
  1537. INIT_LIST_HEAD(&mtpg->tx_idle);
  1538. INIT_LIST_HEAD(&mtpg->intr_idle);
  1539. mtpg->wq = create_singlethread_workqueue("mtp_read_send");
  1540. if (!mtpg->wq) {
  1541. printk(KERN_ERR "mtpg_dev_alloc work queue creation failed\n");
  1542. rc = -ENOMEM;
  1543. goto err_work;
  1544. }
  1545. INIT_WORK(&mtpg->read_send_work, read_send_work);
  1546. /* the_mtpg must be set before calling usb_gadget_register_driver */
  1547. the_mtpg = mtpg;
  1548. rc = misc_register(&mtpg_device);
  1549. if (rc != 0) {
  1550. printk(KERN_ERR " misc_register of mtpg Failed\n");
  1551. goto err_misc_register;
  1552. }
  1553. err = device_create_file(mtpg_device.this_device, &dev_attr_guid);
  1554. if (err) {
  1555. printk(KERN_DEBUG "mtp: %s failed to create guid attr\n",
  1556. __func__);
  1557. return err;
  1558. }
  1559. else
  1560. printk(KERN_DEBUG "mtp: %s success to create guid attr\n",
  1561. __func__);
  1562. return 0;
  1563. err_work:
  1564. err_misc_register:
  1565. the_mtpg = NULL;
  1566. kfree(mtpg);
  1567. printk(KERN_ERR "mtp gadget driver failed to initialize\n");
  1568. return rc;
  1569. }
  1570. static void mtp_cleanup(void)
  1571. {
  1572. struct mtpg_dev *mtpg = the_mtpg;
  1573. printk(KERN_DEBUG "[%s:::%d]\n", __func__, __LINE__);
  1574. if (!mtpg)
  1575. return;
  1576. misc_deregister(&mtpg_device);
  1577. the_mtpg = NULL;
  1578. kfree(mtpg);
  1579. }
  1580. MODULE_AUTHOR("Deepak And Madhukar");
  1581. MODULE_LICENSE("GPL");