usb_msc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /******************************************************************************
  2. *
  3. * Freescale Semiconductor Inc.
  4. * (c) Copyright 2004-2009 Freescale Semiconductor, Inc.
  5. * ALL RIGHTS RESERVED.
  6. *
  7. ******************************************************************************
  8. *
  9. * THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR
  10. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  11. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  12. * IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  13. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  14. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  15. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  16. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  17. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  18. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  19. * THE POSSIBILITY OF SUCH DAMAGE.
  20. *
  21. **************************************************************************//*!
  22. *
  23. * @file usb_msc.c
  24. *
  25. * @author
  26. *
  27. * @version
  28. *
  29. * @date May-08-2009
  30. *
  31. * @brief The file contains USB stack MSC layer implimentation.
  32. *
  33. *****************************************************************************/
  34. /******************************************************************************
  35. * Includes
  36. *****************************************************************************/
  37. #include "usb_msc.h"
  38. #include "usb_descriptor.h"
  39. /*****************************************************************************
  40. * Constant and Macro's
  41. *****************************************************************************/
  42. /****************************************************************************
  43. * Global Variables
  44. ****************************************************************************/
  45. /* Add all the variables needed for usb_msc.c to this structure */
  46. MSC_GLOBAL_VARIABLE_STRUCT g_msc;
  47. static uint_32 g_current_offset = 0;
  48. static uint_32 g_transfer_remaining = 0;
  49. /*****************************************************************************
  50. * Local Types - None
  51. *****************************************************************************/
  52. /*****************************************************************************
  53. * Local Functions Prototypes
  54. *****************************************************************************/
  55. #ifndef COMPOSITE_DEV
  56. static uint_8 USB_Other_Requests
  57. #else
  58. uint_8 USB_MSC_Other_Requests
  59. #endif
  60. (uint_8 controller_ID,
  61. USB_SETUP_STRUCT * setup_packet,
  62. uint_8_ptr *data,
  63. uint_32 *size);
  64. uint_8 process_mass_storage_command (uint_8 controller_ID,
  65. PTR_CBW cbw_ptr,
  66. uint_32* csw_residue_ptr,
  67. uint_8* csw_status_ptr);
  68. /*****************************************************************************
  69. * Local Variables - None
  70. *****************************************************************************/
  71. /*****************************************************************************
  72. * Local Functions
  73. *****************************************************************************/
  74. /**************************************************************************//*!
  75. *
  76. * @name process_mass_storage_command
  77. *
  78. * @brief Process a Mass storage class command
  79. * This function is added here to add more sub class specific commands)
  80. *
  81. * @param controller_ID: To identify the controller
  82. * @param cbw_ptr : pointer to Command Block Wrapper sent by host
  83. * @param csw_residue_ptr: pointer to dCSWDataResidue of Command Status Wrapper
  84. * @param csw_status_ptr : pointer to bCSWStatus of Command Status Wrapper
  85. *
  86. * @return error
  87. *
  88. *****************************************************************************/
  89. uint_8 process_mass_storage_command
  90. (
  91. uint_8 controller_ID,
  92. PTR_CBW cbw_ptr,
  93. uint_32* csw_residue_ptr,
  94. uint_8* csw_status_ptr
  95. )
  96. {
  97. uint_8 error = USBERR_UNKNOWN_ERROR;/* initializing to error value */
  98. switch (cbw_ptr->command_block[0])
  99. {
  100. /* commands to be supported by all devices */
  101. case INQUIRY_COMMAND : /*opcode : 0x12*/
  102. error = msc_inquiry_command(controller_ID, cbw_ptr,csw_residue_ptr,
  103. csw_status_ptr);
  104. break;
  105. case READ_10_COMMAND : /*opcode : 0x28 */
  106. case READ_12_COMMAND : /*opcode : 0xA8 */
  107. error = msc_read_command(controller_ID, cbw_ptr,csw_residue_ptr,csw_status_ptr);
  108. break;
  109. case REQUEST_SENSE_COMMAND : /*opcode : 0x03*/
  110. error = msc_request_sense_command(controller_ID, cbw_ptr,csw_residue_ptr,
  111. csw_status_ptr);
  112. break;
  113. case TEST_UNIT_READY_COMMAND : /*opcode : 0x00 */
  114. error = msc_test_unit_ready_command(controller_ID, cbw_ptr,csw_residue_ptr,
  115. csw_status_ptr);
  116. break;
  117. case WRITE_10_COMMAND : /*opcode : 0x2A */
  118. case WRITE_12_COMMAND : /*opcode : 0xAA */
  119. error = msc_write_command(controller_ID, cbw_ptr,csw_residue_ptr,csw_status_ptr);
  120. break;
  121. case PREVENT_ALLOW_MEDIUM_REM_COMMAND : /*opcode :0x1E */
  122. error = msc_prevent_allow_medium_removal(controller_ID, cbw_ptr,csw_residue_ptr,
  123. csw_status_ptr);
  124. break;
  125. case FORMAT_UNIT_COMMAND : /*opcode : 0x04*/
  126. error = msc_format_unit_command(controller_ID, cbw_ptr,csw_residue_ptr,
  127. csw_status_ptr);
  128. break;
  129. case READ_CAPACITY_10_COMMAND : /*opcode : 0x25*/
  130. error = msc_read_capacity_command(controller_ID, cbw_ptr,csw_residue_ptr,
  131. csw_status_ptr);
  132. break;
  133. case MODE_SENSE_10_COMMAND : /* opcode :0x5A*/
  134. case MODE_SENSE_6_COMMAND : /* opcode : 0x1A */
  135. error = msc_mode_sense_command(controller_ID, cbw_ptr,csw_residue_ptr,
  136. csw_status_ptr);
  137. break;
  138. case MODE_SELECT_10_COMMAND : /*opcode : 0x55 */
  139. case MODE_SELECT_6_COMMAND : /*opcode : 0x15 */
  140. error = msc_mode_select_command(controller_ID, cbw_ptr,csw_residue_ptr,
  141. csw_status_ptr);
  142. break;
  143. case READ_FORMAT_CAPACITIES_COMMAND : /*opcode : 0x23 */
  144. error = msc_read_format_capacity_command(controller_ID, cbw_ptr,csw_residue_ptr,
  145. csw_status_ptr);
  146. break;
  147. case SEND_DIAGNOSTIC_COMMAND : /*opcode : 0x1D*/
  148. error = msc_send_diagnostic_command(controller_ID, cbw_ptr,csw_residue_ptr,
  149. csw_status_ptr);
  150. break;
  151. case VERIFY_COMMAND : /*opcode : 0x2F*/
  152. error = msc_verify_command(controller_ID, cbw_ptr,csw_residue_ptr,csw_status_ptr);
  153. break;
  154. case START_STOP_UNIT_COMMAND : /*opcode : 0x1B*/
  155. error = msc_start_stop_unit_command(controller_ID, cbw_ptr,csw_residue_ptr,csw_status_ptr);
  156. break;
  157. default: /* for all unsupported commands */
  158. error = msc_unsupported_command(controller_ID, cbw_ptr,csw_residue_ptr,
  159. csw_status_ptr);
  160. g_msc.out_flag = FALSE;
  161. g_msc.in_flag = FALSE;
  162. g_msc.out_stall_flag = FALSE;
  163. g_msc.in_stall_flag = FALSE;
  164. break;
  165. }
  166. return error;
  167. }
  168. /**************************************************************************//*!
  169. *
  170. * @name USB_Service_Bulk_In
  171. *
  172. * @brief The funtion ic callback function of DIC Bulk In Endpoint
  173. *
  174. * @param event
  175. *
  176. * @return None
  177. *
  178. *****************************************************************************/
  179. void USB_Service_Bulk_In(PTR_USB_DEV_EVENT_STRUCT event)
  180. {
  181. uint_8 event_type;
  182. #if IMPLEMENT_QUEUING
  183. uint_8 index;
  184. uint_8 producer, consumer;
  185. USB_ENDPOINTS *usb_ep_data = USB_Desc_Get_Endpoints(event->controller_ID);
  186. USB_CLASS_MSC_QUEUE queue;
  187. /* map the endpoint num to the index of the endpoint structure */
  188. for(index = 0; index < usb_ep_data->count; index++)
  189. {
  190. if(usb_ep_data->ep[index].ep_num == event->ep_num)
  191. break;
  192. }
  193. producer = g_msc.ep[index].bin_producer;
  194. /* if there are no errors de-queue the queue and decrement the no. of
  195. transfers left, else send the same data again */
  196. g_msc.ep[index].bin_consumer++;
  197. consumer = g_msc.ep[index].bin_consumer;
  198. if(consumer != producer)
  199. {/*if bin is not empty */
  200. queue = g_msc.ep[index].queue[consumer%MAX_QUEUE_ELEMS];
  201. (void)USB_Class_Send_Data(queue.controller_ID, queue.channel,
  202. queue.app_data.data_ptr, queue.app_data.data_size);
  203. return;
  204. }
  205. #endif
  206. if(g_transfer_remaining >= event->len)
  207. { /* decrement the global count */
  208. g_transfer_remaining -= event->len;
  209. }
  210. /* check if there is need to stall BULK IN ENDPOINT And
  211. there isn't any transfer in progress*/
  212. if((g_msc.in_stall_flag == TRUE)&&(!g_transfer_remaining))
  213. {
  214. uint_8 component = (uint_8)(event->ep_num |
  215. (uint_8)(event->direction<<COMPONENT_PREPARE_SHIFT));
  216. g_msc.in_stall_flag = FALSE; /* clear the flag */
  217. g_msc.in_flag = FALSE; /* clear send flag */
  218. /* now, stalling the status phase - CASE 5th of THIRTEEN CASES*/
  219. (void)_usb_device_set_status(&(event->controller_ID),
  220. (uint_8)(component|USB_STATUS_ENDPOINT),(uint_16)USB_STATUS_STALLED);
  221. return;
  222. }
  223. /* If its not a data phase on bulk endpoint */
  224. if ((!g_msc.in_flag) && (event->len == MSC_CSW_LENGTH))
  225. {
  226. USB_memcopy(event->buffer_ptr, &(g_msc.csw_struct), sizeof(CSW));
  227. }
  228. if(g_msc.in_flag) /* bulk in transaction has occurred before CSW */
  229. {
  230. if(g_msc.param_callback != NULL)
  231. {
  232. event_type = (uint_8)(g_transfer_remaining?
  233. USB_MSC_DEVICE_READ_REQUEST : USB_APP_SEND_COMPLETE);
  234. if(event_type == USB_APP_SEND_COMPLETE)
  235. {
  236. APP_DATA_STRUCT bulk_in_recv;
  237. bulk_in_recv.data_ptr = event->buffer_ptr;
  238. bulk_in_recv.data_size = event->len;
  239. g_msc.param_callback(event->controller_ID, event_type,
  240. (void*)&bulk_in_recv);
  241. }
  242. else
  243. {
  244. LBA_APP_STRUCT lba_data;
  245. g_current_offset += event->len;
  246. lba_data.offset = g_current_offset;
  247. lba_data.size = (g_transfer_remaining > MSC_SEND_DATA_BUFF_SIZE) ?
  248. MSC_SEND_DATA_BUFF_SIZE : g_transfer_remaining;
  249. /* whichever is smaller */
  250. lba_data.buff_ptr = g_msc.msc_lba_send_buff;
  251. g_msc.param_callback(event->controller_ID, USB_MSC_DEVICE_READ_REQUEST,
  252. (void*)&lba_data);
  253. (void)USB_MSC_Bulk_Send_Data(event->controller_ID, lba_data.buff_ptr,
  254. lba_data.size);
  255. }
  256. }
  257. if(!g_transfer_remaining)
  258. { /* marks the end of data phase */
  259. g_msc.in_flag = FALSE; /* clear the flag for next CBW */
  260. /* Send the command status information */
  261. (void)USB_MSC_Bulk_Send_Data(event->controller_ID,
  262. (uint_8_ptr)&(g_msc.csw_struct), MSC_CSW_LENGTH);
  263. }
  264. }
  265. else if ((event->len == MSC_CSW_LENGTH) /* CSW is 13 bytes in length */
  266. && (g_msc.csw_struct.signature == USB_DCSWSIGNATURE)) /*valid CSW signature*/
  267. {
  268. /* this flag will now be set on reset or after CSW being sent */
  269. g_msc.cbw_valid_flag = TRUE;
  270. /* prepare for next CBW */
  271. (void)USB_MSC_Bulk_Recv_Data(&(event->controller_ID),
  272. (uint_8_ptr)&(g_msc.cbw_struct),MSC_CBW_LENGTH);
  273. }
  274. }
  275. /**************************************************************************//*!
  276. *
  277. * @name USB_Service_Bulk_Out
  278. *
  279. * @brief The function is callback function of DIC Bulk Out Endpoint
  280. *
  281. * @param event
  282. *
  283. * @return None
  284. *
  285. *****************************************************************************/
  286. void USB_Service_Bulk_Out(PTR_USB_DEV_EVENT_STRUCT event)
  287. {
  288. uint_8 event_type;
  289. uint_8 error;
  290. /* check if there is need to stall BULK OUT ENDPOINT And
  291. there isn't any transfer in progress*/
  292. if((g_msc.out_stall_flag == TRUE)&&(!g_transfer_remaining))
  293. {
  294. uint_8 component = (uint_8)(event->ep_num |
  295. (event->direction<<COMPONENT_PREPARE_SHIFT));
  296. g_msc.out_stall_flag = FALSE; /* clear the flag */
  297. g_msc.out_flag = FALSE; /* clear send flag */
  298. /* now, stalling the status phase - CASE 5th of THIRTEEN CASES*/
  299. (void)_usb_device_set_status(&(event->controller_ID),
  300. (uint_8)(component|USB_STATUS_ENDPOINT),
  301. (uint_16)USB_STATUS_STALLED);
  302. return;
  303. }
  304. /* If its not a data phase on bulk endpoint */
  305. if ((!g_msc.out_flag) && (event->len == MSC_CBW_LENGTH) &&
  306. (event->buffer_ptr != (uint_8_ptr)&(g_msc.cbw_struct)) )
  307. { /* last check condition saves memcopy overhead in case data was
  308. already received in correct buffer */
  309. USB_memcopy(event->buffer_ptr, (uint_8_ptr)&(g_msc.cbw_struct),
  310. sizeof(CBW));
  311. }
  312. if(g_msc.out_flag) /* bulk out transaction has occurred after CBW */
  313. {
  314. if(g_msc.param_callback != NULL)
  315. {
  316. event_type = (uint_8)(g_transfer_remaining?
  317. USB_MSC_DEVICE_WRITE_REQUEST : USB_APP_DATA_RECEIVED);
  318. if(event_type == USB_APP_DATA_RECEIVED)
  319. {
  320. APP_DATA_STRUCT bulk_out_recv;
  321. bulk_out_recv.data_ptr = event->buffer_ptr;
  322. bulk_out_recv.data_size = event->len;
  323. g_msc.param_callback(event->controller_ID, event_type,(void*)&bulk_out_recv);
  324. }
  325. else
  326. {
  327. LBA_APP_STRUCT lba_data1;
  328. lba_data1.buff_ptr = g_msc.msc_lba_recv_buff;
  329. lba_data1.offset = g_current_offset;
  330. lba_data1.size = event->len;
  331. g_msc.param_callback(event->controller_ID, event_type,(void*)&lba_data1);
  332. }
  333. }
  334. if(g_transfer_remaining >= event->len)
  335. { /* decrement the global count */
  336. g_transfer_remaining -= event->len;
  337. if((g_msc.out_stall_flag == TRUE)&&(!g_transfer_remaining))
  338. {
  339. uint_8 component = (uint_8)(event->ep_num |
  340. (event->direction<<COMPONENT_PREPARE_SHIFT));
  341. g_msc.out_stall_flag = FALSE; /* clear the flag */
  342. g_msc.out_flag = FALSE; /* clear send flag */
  343. /* now, stalling the status phase - CASE 5th of THIRTEEN CASES*/
  344. (void)_usb_device_set_status(&(event->controller_ID),
  345. (uint_8)(component|USB_STATUS_ENDPOINT),
  346. (uint_16)USB_STATUS_STALLED);
  347. return;
  348. }
  349. }
  350. if(g_transfer_remaining)
  351. { /* Transfers Remaining -- Again invoke recv_data */
  352. LBA_APP_STRUCT lba_data0;
  353. g_current_offset += event->len;
  354. lba_data0.size = (g_transfer_remaining > MSC_RECV_DATA_BUFF_SIZE) ?
  355. MSC_RECV_DATA_BUFF_SIZE : g_transfer_remaining; /* whichever is smaller */
  356. lba_data0.buff_ptr = g_msc.msc_lba_recv_buff;
  357. (void)USB_MSC_Bulk_Recv_Data(&(event->controller_ID),
  358. lba_data0.buff_ptr,lba_data0.size);
  359. return;
  360. }
  361. else
  362. { /* marks the end of data phase */
  363. g_msc.out_flag = FALSE; /* clear the flag for next CBW */
  364. /* Send the command status information */
  365. (void)USB_MSC_Bulk_Send_Data(event->controller_ID,
  366. (uint_8_ptr)&(g_msc.csw_struct), MSC_CSW_LENGTH);
  367. }
  368. }
  369. else if(/* check for valid and meaningful CBW */
  370. /* CBW received after device had sent a CSW or after a reset */
  371. (g_msc.cbw_valid_flag)
  372. /* CBW is 31 bytes in length */
  373. && (event->len == MSC_CBW_LENGTH)
  374. /* valid CBW signature*/
  375. && (g_msc.cbw_struct.signature == USB_DCBWSIGNATURE)
  376. /* all reserved bits should be zero*/
  377. && (!((g_msc.cbw_struct.lun & 0xF0) || (g_msc.cbw_struct.cb_length & 0xE0)))
  378. /* host should send command to valid LUN */
  379. && (g_msc.cbw_struct.lun<g_msc.device_info.num_lun_supported)
  380. /* valid cbwcb length*/
  381. && ((g_msc.cbw_struct.cb_length >= 0x01)&&(g_msc.cbw_struct.cb_length <= 0x10))
  382. )
  383. {
  384. g_msc.re_stall_flag = TRUE;
  385. /* A valid CBW was received */
  386. g_msc.csw_struct.signature = USB_DCSWSIGNATURE;
  387. g_msc.csw_struct.tag = g_msc.cbw_struct.tag;
  388. /*this flag will now be set on reset or after CSW being sent */
  389. g_msc.cbw_valid_flag = FALSE;
  390. USB_BYTE_SWAP4((uint_32)g_msc.cbw_struct.data_length,(uint_32*)&(g_msc.cbw_struct.data_length));
  391. /* set flag if device is going to recv data in coming transaction */
  392. g_msc.out_flag = (boolean)(( (!(g_msc.cbw_struct.flag & USB_CBW_DIRECTION_BIT))
  393. && (g_msc.cbw_struct.data_length))?TRUE:FALSE);
  394. /* set flag if send is going to send data in coming transaction */
  395. g_msc.in_flag = (boolean)(( (g_msc.cbw_struct.flag & USB_CBW_DIRECTION_BIT)
  396. && (g_msc.cbw_struct.data_length))?TRUE:FALSE);
  397. /* Process the command */
  398. error = process_mass_storage_command(event->controller_ID, &(g_msc.cbw_struct),
  399. (uint_32*)&(g_msc.csw_struct.residue), &(g_msc.csw_struct.csw_status));
  400. if(error == USBERR_ENDPOINT_STALLED)
  401. {
  402. if(g_msc.out_flag==TRUE)
  403. {
  404. g_msc.out_stall_flag = TRUE;
  405. g_msc.out_flag = FALSE; /* so as to send status phase */
  406. }
  407. else if(g_msc.in_flag==TRUE)
  408. {
  409. g_msc.in_stall_flag = TRUE;
  410. }
  411. }
  412. /* if there is no data phase on bulk endpoints */
  413. if(!(g_msc.out_flag || g_msc.in_flag))
  414. { /* Send the command status information */
  415. (void)USB_MSC_Bulk_Send_Data(event->controller_ID,
  416. (uint_8_ptr)&(g_msc.csw_struct), MSC_CSW_LENGTH);
  417. }
  418. }
  419. else /* Invalid/NMreceived */
  420. {
  421. uint_8 direction;
  422. uint_8 ep_num;
  423. /* prepare the component to be sent in lower layer with
  424. endpoint number and direction*/
  425. uint_8 component;
  426. direction = USB_RECV;
  427. ep_num = BULK_OUT_ENDPOINT;
  428. component = (uint_8)(ep_num | (direction<<COMPONENT_PREPARE_SHIFT));
  429. /* in case of error Stall endpoint and update the status */
  430. (void)_usb_device_set_status(&(event->controller_ID),
  431. (uint_8)(component | USB_STATUS_ENDPOINT),
  432. (uint_16)USB_STATUS_STALLED);
  433. direction = USB_SEND;
  434. ep_num = BULK_IN_ENDPOINT;
  435. component = (uint_8)(ep_num | (direction << COMPONENT_PREPARE_SHIFT));
  436. /* in case of error Stall endpoint and update the status*/
  437. (void)_usb_device_set_status(&(event->controller_ID),
  438. (uint_8)(component | USB_STATUS_ENDPOINT),
  439. (uint_16)USB_STATUS_STALLED);
  440. /* make the following CBWs invalid */
  441. g_msc.cbw_valid_flag = FALSE;
  442. }
  443. }
  444. /**************************************************************************//*!
  445. *
  446. * @name USB_Class_MSC_Event
  447. *
  448. * @brief The funtion initializes MSC endpoints
  449. *
  450. * @param controller_ID: To identify the controller
  451. * @param event pointer to event structure
  452. * @param val gives the configuration value
  453. *
  454. * @return None
  455. *
  456. *****************************************************************************/
  457. void USB_Class_MSC_Event(uint_8 controller_ID, uint_8 event, void* val)
  458. {
  459. #if IMPLEMENT_QUEUING
  460. uint_8 index;
  461. #endif
  462. /* get the endpoints from the descriptor module */
  463. USB_ENDPOINTS *usb_ep_data = USB_Desc_Get_Endpoints(controller_ID);
  464. if(event == USB_APP_ENUM_COMPLETE)
  465. {
  466. uint_8 index_num = 0;
  467. uint_8 count = 0,ep_count = 0;
  468. #ifdef COMPOSITE_DEV
  469. DEV_ARCHITECTURE_STRUCT_PTR dev_arc_ptr;
  470. CLASS_ARC_STRUCT_PTR dev_class_ptr;
  471. dev_arc_ptr = (DEV_ARCHITECTURE_STRUCT *)USB_Desc_Get_Class_Architecture(controller_ID);
  472. for(count = 0; count < dev_arc_ptr->cl_count; count++)
  473. {
  474. dev_class_ptr = (CLASS_ARC_STRUCT_PTR)dev_arc_ptr->value[count];
  475. /* Initializes sub_classes */
  476. ep_count = dev_class_ptr->value[0];
  477. if(dev_class_ptr->class_type == 0x08/*MSD_CC*/)
  478. break;
  479. index_num +=dev_class_ptr->value[0];
  480. }
  481. #else
  482. ep_count = usb_ep_data->count;
  483. #endif
  484. /* deinitialize all endpoints in case they were initialized */
  485. for(count=index_num; count<ep_count+index_num; count++)
  486. {
  487. USB_EP_STRUCT_PTR ep_struct_ptr=
  488. (USB_EP_STRUCT_PTR) (&usb_ep_data->ep[count]);
  489. (void)_usb_device_deinit_endpoint(&controller_ID,
  490. ep_struct_ptr->ep_num, ep_struct_ptr->direction);
  491. }
  492. /* intialize all non control endpoints */
  493. for(count=index_num; count<ep_count+index_num; count++)
  494. {
  495. USB_EP_STRUCT_PTR ep_struct_ptr=
  496. (USB_EP_STRUCT_PTR) (&usb_ep_data->ep[count]);
  497. uint_8 component = (uint_8)(ep_struct_ptr->ep_num |
  498. (ep_struct_ptr->direction<<COMPONENT_PREPARE_SHIFT));
  499. (void)_usb_device_init_endpoint(&controller_ID,ep_struct_ptr->ep_num,
  500. (uint_16)ep_struct_ptr->size, ep_struct_ptr->direction, ep_struct_ptr->type, FALSE);
  501. #ifdef MULTIPLE_DEVICES
  502. /* register callback service for Non Control EndPoints */
  503. if(ep_struct_ptr->type == USB_BULK_PIPE)
  504. {
  505. if(ep_struct_ptr->direction == USB_RECV)
  506. {
  507. (void)_usb_device_register_service(controller_ID,
  508. (uint_8)(USB_SERVICE_EP0+ep_struct_ptr->ep_num),
  509. USB_Service_Bulk_Out);
  510. }
  511. else
  512. {
  513. (void)_usb_device_register_service(controller_ID,
  514. (uint_8)(USB_SERVICE_EP0+ep_struct_ptr->ep_num),
  515. USB_Service_Bulk_In);
  516. }
  517. }
  518. #endif
  519. /* set the EndPoint Status as Idle in the device layer */
  520. /* (no need to specify direction for this case) */
  521. (void)_usb_device_set_status(&controller_ID,
  522. (uint_8)(USB_STATUS_ENDPOINT|component),
  523. (uint_16)USB_STATUS_IDLE);
  524. }
  525. g_msc.re_stall_flag = FALSE;
  526. g_msc.out_flag = FALSE;
  527. g_msc.in_flag = FALSE;
  528. g_msc.out_stall_flag = FALSE;
  529. g_msc.in_stall_flag = FALSE;
  530. g_msc.cbw_valid_flag = TRUE; /*making the first CBW valid */
  531. g_transfer_remaining = 0;
  532. }
  533. else if(event == USB_APP_BUS_RESET)
  534. {
  535. #if IMPLEMENT_QUEUING
  536. for(index = 0; index < usb_ep_data->count; index++)
  537. {
  538. g_msc.ep[index].bin_consumer = 0x00;
  539. g_msc.ep[index].bin_producer = 0x00;
  540. }
  541. #endif
  542. }
  543. else if(event == USB_APP_EP_UNSTALLED)
  544. {
  545. uint_8 value;
  546. value = *((uint_8_ptr)val);
  547. if( (g_msc.re_stall_flag == TRUE)
  548. &&
  549. (((value & 0x0F) == BULK_IN_ENDPOINT) ||
  550. ((value & 0x0F) == BULK_OUT_ENDPOINT)))
  551. { /* For MASS Storage Class BULK ENDPOINTS have to be unstalled
  552. only on receiving Bulk Only Reset.
  553. Therefore, if Host sends clear feature to unstall these
  554. endpoints, re-stall them */
  555. (void)_usb_device_set_status(&controller_ID,
  556. (uint_8)(value|USB_STATUS_ENDPOINT),
  557. (uint_16)USB_STATUS_STALLED);
  558. }
  559. }
  560. else if(event == USB_APP_EP_STALLED)
  561. {
  562. /* Code to be added here,
  563. if there is some action needed at app level */
  564. }
  565. if(g_msc.msc_callback != NULL)
  566. {
  567. g_msc.msc_callback(controller_ID,event,val);
  568. }
  569. }
  570. /**************************************************************************//*!
  571. *
  572. * @name USB_Other_Requests
  573. *
  574. * @brief The funtion provides flexibilty to add class and vendor specific
  575. * requests
  576. *
  577. * @param controller_ID: To identify the controller
  578. * @param setup_packet: setup packet received
  579. * @param data: data to be send back
  580. * @param size: size to be returned
  581. *
  582. * @return status:
  583. * USB_OK : When Successfull
  584. * Others : When Error
  585. *
  586. *****************************************************************************/
  587. #ifndef COMPOSITE_DEV
  588. static uint_8 USB_Other_Requests
  589. #else
  590. uint_8 USB_MSC_Other_Requests
  591. #endif
  592. (
  593. uint_8 controller_ID,
  594. USB_SETUP_STRUCT * setup_packet,
  595. uint_8_ptr *data,
  596. uint_32 *size
  597. )
  598. {
  599. uint_8 error = USBERR_INVALID_REQ_TYPE;
  600. *size=0;
  601. if((setup_packet->request_type & USB_REQUEST_CLASS_MASK) ==
  602. USB_REQUEST_CLASS_CLASS)
  603. { /* class request so handle it here */
  604. error = USB_OK;
  605. /* call for class/subclass specific requests */
  606. switch(setup_packet->request)
  607. {
  608. case GET_MAX_LUN :
  609. if((setup_packet->index < USB_MAX_SUPPORTED_INTERFACES)&&
  610. (!setup_packet->value)&&(setup_packet->length == 0x0001)&&
  611. ((setup_packet->request_type & USB_DATA_DIREC_MASK) ==
  612. USB_DATA_TO_HOST))
  613. {
  614. /* return the maximum number of logical units supported */
  615. *data = &g_msc.lun;
  616. *size=1;
  617. g_msc.re_stall_flag = TRUE;
  618. }
  619. else
  620. { /* for Get Max LUN request with invalid wIndex parameter,
  621. host expects stall */
  622. error = USBERR_INVALID_REQ_TYPE;
  623. }
  624. break;
  625. case BULK_ONLY_MASS_STORAGE_RESET :
  626. /* Steps to be taken in this command :
  627. 1) ready the device for the next CBW from the host
  628. 2) preserve the value of its bulk data toggle bits
  629. 3) preserve the value of its bulk endpoint STALL conditions
  630. 4) device shall NAK the status stage of device request until
  631. command is complete*/
  632. if((setup_packet->index < USB_MAX_SUPPORTED_INTERFACES)&&
  633. (!setup_packet->value)&&(!setup_packet->length) &&
  634. ((setup_packet->request_type & USB_DATA_DIREC_MASK) ==
  635. USB_DATA_TO_DEVICE))
  636. {
  637. /* get the endpoints from the descriptor module */
  638. USB_ENDPOINTS *usb_ep_data = USB_Desc_Get_Endpoints(controller_ID);
  639. uint_8 count;
  640. count = 0;
  641. /* deinitialize and initialize bulk endpoints */
  642. while(count < usb_ep_data->count)
  643. {
  644. USB_EP_STRUCT_PTR ep_struct_ptr=
  645. (USB_EP_STRUCT_PTR) (&usb_ep_data->ep[count]);
  646. if(ep_struct_ptr->type == USB_BULK_PIPE)
  647. {
  648. uint_8 component = (uint_8)(ep_struct_ptr->ep_num |
  649. (ep_struct_ptr->direction<<COMPONENT_PREPARE_SHIFT));
  650. (void)_usb_device_set_status(&controller_ID,(uint_8)
  651. (USB_STATUS_ENDPOINT|component),
  652. (uint_16)USB_STATUS_STALLED);
  653. /* set EndPoint Status as Idle */
  654. (void)_usb_device_set_status(&controller_ID,(uint_8)
  655. (USB_STATUS_ENDPOINT|component),
  656. (uint_16)USB_STATUS_IDLE);
  657. }
  658. count++;
  659. }
  660. g_msc.out_flag = FALSE;
  661. g_msc.in_flag = FALSE;
  662. g_msc.out_stall_flag = FALSE;
  663. g_msc.in_stall_flag = FALSE;
  664. g_msc.cbw_valid_flag = TRUE; /*making the first CBW valid */
  665. g_msc.re_stall_flag = FALSE;
  666. g_transfer_remaining = 0;
  667. }
  668. else
  669. { /* for Get Max LUN request with invalid wIndex parameter,
  670. host expects stall */
  671. error = USBERR_INVALID_REQ_TYPE;
  672. }
  673. break;
  674. default :break;
  675. }
  676. }
  677. else if((setup_packet->request_type & USB_REQUEST_CLASS_MASK) ==
  678. USB_REQUEST_CLASS_VENDOR)
  679. { /* vendor specific request */
  680. if(g_msc.vendor_callback != NULL)
  681. {
  682. error = g_msc.vendor_callback(controller_ID, setup_packet,data,size);
  683. }
  684. }
  685. return error;
  686. }
  687. /*****************************************************************************
  688. * Global Functions
  689. *****************************************************************************/
  690. #if(defined LITTLE_ENDIAN)
  691. /* SWAP four consecutive bytes to correct the endianness of data received*/
  692. void USB_BYTE_SWAP4(uint_32 a, uint_32* b)
  693. {
  694. *b = a;
  695. }
  696. #else
  697. /* SWAP four consecutive bytes to correct the endianness of data received*/
  698. void USB_BYTE_SWAP4(uint_32 a, uint_32* b)
  699. {
  700. uint_32 c = (a & 0x000000ff) << (uint_32)24;
  701. c |= (a & 0x0000ff00) << (uint_32)8;
  702. c |= (a & 0x00ff0000) >> (uint_32)8;
  703. c |= (a & 0xff000000) >> (uint_32)24;
  704. *b=c;
  705. }
  706. #endif
  707. /* To clear the buffer */
  708. void USB_memzero(void* ptr, uint_32 size)
  709. {
  710. uint_32 index;
  711. for(index = 0; index < size; index++)
  712. {
  713. *((uint_8_ptr)ptr + index) = 0x00;
  714. }
  715. }
  716. /* To copy data from one memory location to another */
  717. void USB_memcopy(void* src, void* dst, uint_32 size)
  718. {
  719. uint_32 index;
  720. for(index = 0; index < size; index++)
  721. {
  722. *((uint_8_ptr)dst + index) = *((uint_8_ptr)src + index);
  723. }
  724. }
  725. /**************************************************************************//*!
  726. *
  727. * @name USB_Class_MSC_Init
  728. *
  729. * @brief The funtion initializes the Device and Controller layer
  730. *
  731. * @param controller_ID: To Identify the controller
  732. * @param class_callback: event callback
  733. * @param vendor_req_callback: vendor specific class request callback
  734. * @param param_callback: application params callback
  735. *
  736. * @return status
  737. * USB_OK : When Successfull
  738. * Others : Errors
  739. ******************************************************************************
  740. *
  741. *This function initializes the MSC Class layer and layers it is dependednt on
  742. ******************************************************************************/
  743. uint_8 USB_Class_MSC_Init
  744. (
  745. uint_8 controller_ID,/*[IN] the USB device controller to initialize*/
  746. USB_CLASS_CALLBACK msc_class_callback,
  747. USB_REQ_FUNC vendor_req_callback,
  748. USB_CLASS_CALLBACK param_callback
  749. )
  750. {
  751. #if IMPLEMENT_QUEUING
  752. uint_8 index;
  753. #endif
  754. uint_8 error;
  755. USB_ENDPOINTS *usb_ep_data = USB_Desc_Get_Endpoints(controller_ID);
  756. /* initialize the Global Variable Structure */
  757. USB_memzero(&g_msc, sizeof(MSC_GLOBAL_VARIABLE_STRUCT));
  758. #ifndef COMPOSITE_DEV
  759. /* Initialize the device layer*/
  760. error= _usb_device_init(controller_ID, NULL, (uint_8)(usb_ep_data->count+1), TRUE);
  761. /* +1 is for Control Endpoint */
  762. /* Initialize number of Logical Units */
  763. if(error == USB_OK)
  764. {
  765. /* Initialize the generic class functions */
  766. error = USB_Class_Init(controller_ID,USB_Class_MSC_Event,
  767. USB_Other_Requests);
  768. #endif
  769. /* Initialize the pstn subclass functions */
  770. error = USB_MSC_SCSI_Init(controller_ID, param_callback);
  771. #if IMPLEMENT_QUEUING
  772. for(index = 0; index < usb_ep_data->count; index++)
  773. {
  774. g_msc.ep[index].endpoint = usb_ep_data->ep[index].ep_num;
  775. g_msc.ep[index].type = usb_ep_data->ep[index].type;
  776. g_msc.ep[index].bin_consumer = 0x00;
  777. g_msc.ep[index].bin_producer = 0x00;
  778. }
  779. #endif
  780. if(error == USB_OK)
  781. {
  782. /* save the callback pointer */
  783. g_msc.msc_callback = msc_class_callback;
  784. /* save the callback pointer */
  785. g_msc.vendor_callback = vendor_req_callback;
  786. /* save the callback pointer */
  787. g_msc.param_callback = param_callback;
  788. }
  789. #ifndef COMPOSITE_DEV
  790. }
  791. #endif
  792. if(g_msc.param_callback != NULL)
  793. {
  794. g_msc.param_callback(controller_ID, USB_MSC_DEVICE_GET_INFO, &g_msc.device_info);
  795. }
  796. /* Initialize number of Logical Units */
  797. g_msc.lun = (uint_8)(g_msc.device_info.num_lun_supported-1);
  798. return error;
  799. }
  800. /**************************************************************************//*!
  801. *
  802. * @name USB_Class_MSC_DeInit
  803. *
  804. * @brief The funtion de-initializes the Device and Controller layer
  805. *
  806. * @param controller_ID : Controller ID
  807. *
  808. * @return status:
  809. * USB_OK : When Successfull
  810. * Others : When Error
  811. *
  812. ******************************************************************************
  813. *This function de-initializes the MSC Class layer
  814. *****************************************************************************/
  815. uint_8 USB_Class_MSC_DeInit
  816. (
  817. uint_8 controller_ID /* [IN] Controller ID */
  818. )
  819. {
  820. uint_8 status = USB_OK;
  821. #ifdef COMPOSITE_DEV
  822. UNUSED(controller_ID)
  823. #endif
  824. /* free the HID class callback pointer */
  825. g_msc.msc_callback = NULL;
  826. /* free the vendor request callback pointer */
  827. g_msc.vendor_callback = NULL;
  828. /* free the callback to ask application for class specific params*/
  829. g_msc.param_callback = NULL;
  830. #ifndef COMPOSITE_DEV
  831. /* Call common class deinit function */
  832. status = USB_Class_DeInit(controller_ID);
  833. if(status == USB_OK)
  834. /* Call device deinit function */
  835. status = _usb_device_deinit();
  836. #endif
  837. return status;
  838. }
  839. /**************************************************************************//*!
  840. *
  841. * @name USB_Class_MSC_Send_Data
  842. *
  843. * @brief
  844. *
  845. * @param controller_ID: To identify the controller :
  846. * @param ep_num : endpoint num
  847. * @param app_buff : buffer to send
  848. * @param size : length of the transfer
  849. *
  850. * @return status
  851. * USB_OK : When Successfull
  852. * Others : Errors
  853. *****************************************************************************/
  854. uint_8 USB_Class_MSC_Send_Data
  855. (
  856. uint_8 controller_ID,
  857. uint_8 ep_num,
  858. uint_8_ptr app_buff,
  859. uint_32 size
  860. )
  861. {
  862. uint_8 error = USB_OK;
  863. #if IMPLEMENT_QUEUING
  864. uint_8 index;
  865. uint_8 producer, consumer;
  866. USB_ENDPOINTS *usb_ep_data = USB_Desc_Get_Endpoints(controller_ID);
  867. /* map the endpoint num to the index of the endpoint structure */
  868. for(index = 0; index < usb_ep_data->count; index++)
  869. {
  870. if(usb_ep_data->ep[index].ep_num == ep_num)
  871. break;
  872. }
  873. producer = g_msc.ep[index].bin_producer;
  874. consumer = g_msc.ep[index].bin_consumer;
  875. if(((uint_8)(producer - consumer)) != (uint_8)(MAX_QUEUE_ELEMS))
  876. {/* the bin is not full*/
  877. uint_8 queue_num = (uint_8)(producer % MAX_QUEUE_ELEMS);
  878. /* put all send request parameters in the endpoint data structure*/
  879. g_msc.ep[index].queue[queue_num].channel = ep_num;
  880. g_msc.ep[index].queue[queue_num].app_data.data_ptr = app_buff;
  881. g_msc.ep[index].queue[queue_num].app_data.data_size = size;
  882. g_msc.ep[index].queue[queue_num].controller_ID = controller_ID;
  883. /* increment producer bin by 1*/
  884. g_msc.ep[index].bin_producer = ++producer;
  885. if((uint_8)(producer - consumer) == (uint_8)1)
  886. {
  887. #endif
  888. error = USB_Class_Send_Data(controller_ID, ep_num, app_buff,size);
  889. #if IMPLEMENT_QUEUING
  890. }
  891. }
  892. else /* bin is full */
  893. {
  894. error = USBERR_DEVICE_BUSY;
  895. }
  896. #endif
  897. return error;
  898. }
  899. /**************************************************************************//*!
  900. *
  901. * @name USB_MSC_LBA_Transfer
  902. *
  903. * @brief
  904. *
  905. * @param controller_ID: To identify the controller
  906. * @param direction : transfer direction
  907. * @param lba_info_ptr : buffer to send
  908. *
  909. * @return status
  910. * USB_OK : When Successfull
  911. * Others : Errors
  912. *****************************************************************************/
  913. uint_8 USB_MSC_LBA_Transfer
  914. (
  915. uint_8 controller_ID,
  916. boolean direction,
  917. PTR_LBA_INFO_STRUCT lba_info_ptr
  918. )
  919. {
  920. uint_8 error;
  921. LBA_APP_STRUCT lba_data;
  922. if((!((lba_info_ptr->starting_lba<g_msc.device_info.total_lba_device_supports)&&
  923. (lba_info_ptr->lba_transfer_num <= (g_msc.device_info.total_lba_device_supports -
  924. lba_info_ptr->starting_lba)))) || (g_msc.param_callback == NULL))
  925. {
  926. /* host trying to access region beyond MASS STORAGE SPACE
  927. Or no param_callback is registered */
  928. return(uint_8)((direction?USBERR_TX_FAILED:USBERR_RX_FAILED));
  929. }
  930. g_transfer_remaining = lba_info_ptr->lba_transfer_num *
  931. g_msc.device_info.length_of_each_lba_of_device;
  932. g_current_offset = lba_info_ptr->starting_lba *
  933. g_msc.device_info.length_of_each_lba_of_device;
  934. lba_data.offset = g_current_offset;
  935. if(direction == USB_SEND)
  936. {
  937. lba_data.size = (g_transfer_remaining > MSC_SEND_DATA_BUFF_SIZE) ?
  938. MSC_SEND_DATA_BUFF_SIZE : g_transfer_remaining; /* whichever is smaller */
  939. lba_data.buff_ptr = g_msc.msc_lba_send_buff;
  940. g_msc.param_callback(controller_ID, USB_MSC_DEVICE_READ_REQUEST,
  941. (void*)&lba_data);
  942. error = USB_MSC_Bulk_Send_Data(controller_ID,lba_data.buff_ptr,lba_data.size);
  943. }
  944. else
  945. {
  946. lba_data.size = (g_transfer_remaining > MSC_RECV_DATA_BUFF_SIZE) ?
  947. MSC_RECV_DATA_BUFF_SIZE : g_transfer_remaining; /* whichever is smaller */
  948. lba_data.buff_ptr = g_msc.msc_lba_recv_buff;
  949. error = USB_MSC_Bulk_Recv_Data(&controller_ID,lba_data.buff_ptr,lba_data.size);
  950. }
  951. return error;
  952. }
  953. /* EOF */