]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassCbi.c
Fix some typo.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassCbi.c
CommitLineData
e237e7ae 1/** @file\r
2\r
cc5166ff 3 Implementation of the USB mass storage Control/Bulk/Interrupt transpor.\r
4 Notice: it is being obseleted by the standard body in favor of the BOT\r
5 (Bulk-Only Transport).\r
6\r
c7e39923 7Copyright (c) 2007 - 2008, Intel Corporation\r
e237e7ae 8All rights reserved. This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
cc5166ff 16**/\r
e237e7ae 17\r
cc5166ff 18#include "UsbMass.h"\r
19#include "UsbMassCbi.h"\r
e237e7ae 20\r
cc5166ff 21/**\r
22 Call the Usb mass storage class transport protocol to\r
23 reset the device. The reset is defined as a Non-Data\r
24 command. Don't use UsbCbiExecCommand to send the command\r
25 to device because that may introduce recursive loop.\r
e237e7ae 26\r
cc5166ff 27 @param Context The USB CBI device protocol\r
28 @param ExtendedVerification The flag controlling the rule of reset\r
e237e7ae 29\r
cc5166ff 30 @retval EFI_SUCCESS the device is reset\r
31 @retval Others Failed to reset the device\r
e237e7ae 32\r
33**/\r
e237e7ae 34EFI_STATUS\r
35UsbCbiResetDevice (\r
36 IN VOID *Context,\r
37 IN BOOLEAN ExtendedVerification\r
38 );\r
39\r
40\r
41/**\r
42 Initialize the USB mass storage class CBI transport protocol.\r
43 If Context isn't NULL, it will save its context in it.\r
44\r
45 @param UsbIo The USB IO to use\r
e237e7ae 46 @param Context The variable to save context in\r
47\r
48 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory\r
49 @retval EFI_UNSUPPORTED The device isn't supported\r
50 @retval EFI_SUCCESS The CBI protocol is initialized.\r
cc5166ff 51 @retval Other The Usb cbi init failed.\r
e237e7ae 52\r
53**/\r
e237e7ae 54EFI_STATUS\r
55UsbCbiInit (\r
56 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
e237e7ae 57 OUT VOID **Context OPTIONAL\r
58 )\r
59{\r
60 USB_CBI_PROTOCOL *UsbCbi;\r
61 EFI_USB_INTERFACE_DESCRIPTOR *Interface;\r
62 EFI_USB_ENDPOINT_DESCRIPTOR EndPoint;\r
63 EFI_STATUS Status;\r
64 UINT8 Index;\r
65\r
66 //\r
67 // Allocate the CBI context\r
68 //\r
69 UsbCbi = AllocateZeroPool (\r
70 sizeof (USB_CBI_PROTOCOL) + 3 * sizeof (EFI_USB_ENDPOINT_DESCRIPTOR)\r
71 );\r
72\r
73 if (UsbCbi == NULL) {\r
74 return EFI_OUT_OF_RESOURCES;\r
75 }\r
76\r
77 UsbCbi->UsbIo = UsbIo;\r
78\r
79 //\r
80 // Get the interface descriptor and validate that it is a USB mass\r
81 // storage class CBI interface.\r
82 //\r
83 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &UsbCbi->Interface);\r
84 if (EFI_ERROR (Status)) {\r
85 goto ON_ERROR;\r
86 }\r
87\r
88 Interface = &UsbCbi->Interface;\r
89 if ((Interface->InterfaceProtocol != USB_MASS_STORE_CBI0)\r
90 && (Interface->InterfaceProtocol != USB_MASS_STORE_CBI1)) {\r
91 Status = EFI_UNSUPPORTED;\r
92 goto ON_ERROR;\r
93 }\r
94\r
95 //\r
96 // Locate and save the bulk-in, bulk-out, and interrupt endpoint\r
97 //\r
98 for (Index = 0; Index < Interface->NumEndpoints; Index++) {\r
99 Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &EndPoint);\r
100 if (EFI_ERROR (Status)) {\r
101 continue;\r
102 }\r
103\r
104 if (USB_IS_BULK_ENDPOINT (EndPoint.Attributes)) {\r
105 //\r
106 // Use the first Bulk-In and Bulk-Out endpoints\r
107 //\r
108 if (USB_IS_IN_ENDPOINT (EndPoint.EndpointAddress) &&\r
109 (UsbCbi->BulkInEndpoint == NULL)) {\r
110\r
111 UsbCbi->BulkInEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbCbi + 1);\r
84b5c78e 112 CopyMem(UsbCbi->BulkInEndpoint, &EndPoint, sizeof (EndPoint));;\r
e237e7ae 113 }\r
114\r
115 if (USB_IS_OUT_ENDPOINT (EndPoint.EndpointAddress) &&\r
116 (UsbCbi->BulkOutEndpoint == NULL)) {\r
117\r
118 UsbCbi->BulkOutEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbCbi + 1) + 1;\r
84b5c78e 119 CopyMem(UsbCbi->BulkOutEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 120 }\r
121\r
122 } else if (USB_IS_INTERRUPT_ENDPOINT (EndPoint.Attributes)) {\r
123 //\r
124 // Use the first interrupt endpoint if it is CBI0\r
125 //\r
126 if ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0) &&\r
127 (UsbCbi->InterruptEndpoint == NULL)) {\r
128\r
129 UsbCbi->InterruptEndpoint = (EFI_USB_ENDPOINT_DESCRIPTOR *) (UsbCbi + 1) + 2;\r
84b5c78e 130 CopyMem(UsbCbi->InterruptEndpoint, &EndPoint, sizeof (EndPoint));\r
e237e7ae 131 }\r
132 }\r
133 }\r
134\r
135 if ((UsbCbi->BulkInEndpoint == NULL)\r
136 || (UsbCbi->BulkOutEndpoint == NULL)\r
137 || ((Interface->InterfaceProtocol == USB_MASS_STORE_CBI0)\r
138 && (UsbCbi->InterruptEndpoint == NULL))) {\r
139 Status = EFI_UNSUPPORTED;\r
140 goto ON_ERROR;\r
141 }\r
142\r
143 if (Context != NULL) {\r
144 *Context = UsbCbi;\r
145 } else {\r
146 gBS->FreePool (UsbCbi);\r
147 }\r
148 return EFI_SUCCESS;\r
149\r
150ON_ERROR:\r
151 gBS->FreePool (UsbCbi);\r
152 return Status;\r
153}\r
154\r
155\r
e237e7ae 156/**\r
157 Send the command to the device using class specific control transfer.\r
158\r
159 @param UsbCbi The USB CBI protocol\r
160 @param Cmd The high level command to transfer to device\r
161 @param CmdLen The length of the command\r
162 @param Timeout The time to wait the command to finish\r
163\r
164 @retval EFI_SUCCESS The command is transferred to device\r
165 @retval Others The command failed to transfer to device\r
166\r
167**/\r
e237e7ae 168EFI_STATUS\r
169UsbCbiSendCommand (\r
170 IN USB_CBI_PROTOCOL *UsbCbi,\r
171 IN UINT8 *Cmd,\r
172 IN UINT8 CmdLen,\r
173 IN UINT32 Timeout\r
174 )\r
175{\r
176 EFI_USB_DEVICE_REQUEST Request;\r
177 EFI_STATUS Status;\r
178 UINT32 TransStatus;\r
179 UINTN DataLen;\r
180 INTN Retry;\r
181\r
182 //\r
183 // Fill in the device request, CBI use the "Accept Device-Specific\r
184 // Cmd" (ADSC) class specific request to send commands\r
185 //\r
186 Request.RequestType = 0x21;\r
187 Request.Request = 0;\r
188 Request.Value = 0;\r
189 Request.Index = UsbCbi->Interface.InterfaceNumber;\r
190 Request.Length = CmdLen;\r
191\r
192 Status = EFI_SUCCESS;\r
41e8ff27 193 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 194\r
195 for (Retry = 0; Retry < USB_CBI_MAX_RETRY; Retry++) {\r
196 //\r
197 // Use the UsbIo to send the command to the device\r
198 //\r
199 TransStatus = 0;\r
200 DataLen = CmdLen;\r
201\r
202 Status = UsbCbi->UsbIo->UsbControlTransfer (\r
203 UsbCbi->UsbIo,\r
204 &Request,\r
205 EfiUsbDataOut,\r
206 Timeout,\r
207 Cmd,\r
208 DataLen,\r
209 &TransStatus\r
210 );\r
211 //\r
212 // The device can fail the command by STALL the control endpoint.\r
213 // It can delay the command by NAK the data or status stage, this\r
214 // is a "class-specific exemption to the USB specification". Retry\r
215 // if the command is NAKed.\r
216 //\r
217 if (EFI_ERROR (Status) && (TransStatus == EFI_USB_ERR_NAK)) {\r
218 continue;\r
219 }\r
220\r
221 break;\r
222 }\r
223\r
224 return Status;\r
225}\r
226\r
227\r
228/**\r
229 Transfer data between the device and host. The CBI contains three phase,\r
230 command, data, and status. This is data phase.\r
231\r
232 @param UsbCbi The USB CBI device\r
233 @param DataDir The direction of the data transfer\r
234 @param Data The buffer to hold the data\r
235 @param TransLen The expected transfer length\r
236 @param Timeout The time to wait the command to execute\r
237\r
238 @retval EFI_SUCCESS The data transfer succeeded\r
239 @retval Others Failed to transfer all the data\r
240\r
241**/\r
e237e7ae 242EFI_STATUS\r
243UsbCbiDataTransfer (\r
244 IN USB_CBI_PROTOCOL *UsbCbi,\r
245 IN EFI_USB_DATA_DIRECTION DataDir,\r
246 IN OUT UINT8 *Data,\r
247 IN OUT UINTN *TransLen,\r
248 IN UINT32 Timeout\r
249 )\r
250{\r
251 EFI_USB_ENDPOINT_DESCRIPTOR *Endpoint;\r
252 EFI_STATUS Status;\r
253 UINT32 TransStatus;\r
254 UINTN Remain;\r
255 UINTN Increment;\r
256 UINT8 *Next;\r
257 UINTN Retry;\r
258\r
259 //\r
260 // It's OK if no data to transfer\r
261 //\r
262 if ((DataDir == EfiUsbNoData) || (*TransLen == 0)) {\r
263 return EFI_SUCCESS;\r
264 }\r
265\r
266 //\r
267 // Select the endpoint then issue the transfer\r
268 //\r
269 if (DataDir == EfiUsbDataIn) {\r
270 Endpoint = UsbCbi->BulkInEndpoint;\r
271 } else {\r
272 Endpoint = UsbCbi->BulkOutEndpoint;\r
273 }\r
274\r
275 Next = Data;\r
276 Remain = *TransLen;\r
277 Retry = 0;\r
278 Status = EFI_SUCCESS;\r
41e8ff27 279 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 280\r
281 //\r
282 // Transfer the data, if the device returns NAK, retry it.\r
283 //\r
284 while (Remain > 0) {\r
285 TransStatus = 0;\r
286\r
287 if (Remain > (UINTN) USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize) {\r
288 Increment = USB_CBI_MAX_PACKET_NUM * Endpoint->MaxPacketSize;\r
289 } else {\r
290 Increment = Remain;\r
291 }\r
292\r
293 Status = UsbCbi->UsbIo->UsbBulkTransfer (\r
294 UsbCbi->UsbIo,\r
295 Endpoint->EndpointAddress,\r
296 Next,\r
297 &Increment,\r
298 Timeout,\r
299 &TransStatus\r
300 );\r
301 if (EFI_ERROR (Status)) {\r
302 if (TransStatus == EFI_USB_ERR_NAK) {\r
303 //\r
304 // The device can NAK the host if either the data/buffer isn't\r
305 // aviable or the command is in-progress. The data can be partly\r
306 // transferred. The transfer is aborted if several succssive data\r
307 // transfer commands are NAKed.\r
308 //\r
309 if (Increment == 0) {\r
310 if (++Retry > USB_CBI_MAX_RETRY) {\r
311 goto ON_EXIT;\r
312 }\r
313\r
314 } else {\r
315 Next += Increment;\r
316 Remain -= Increment;\r
317 Retry = 0;\r
318 }\r
319\r
320 continue;\r
321 }\r
322\r
323 //\r
324 // The device can fail the command by STALL the bulk endpoint.\r
325 // Clear the stall if that is the case.\r
326 //\r
327 if (TransStatus == EFI_USB_ERR_STALL) {\r
328 UsbClearEndpointStall (UsbCbi->UsbIo, Endpoint->EndpointAddress);\r
329 }\r
330\r
331 goto ON_EXIT;\r
332 }\r
333\r
334 Next += Increment;\r
335 Remain -= Increment;\r
336 }\r
337\r
338ON_EXIT:\r
339 *TransLen -= Remain;\r
340 return Status;\r
341}\r
342\r
343\r
344/**\r
345 Get the result of high level command execution from interrupt\r
346 endpoint. This function returns the USB transfer status, and\r
347 put the high level command execution result in Result.\r
348\r
349 @param UsbCbi The USB CBI protocol\r
350 @param Timeout The time to wait the command to execute\r
351 @param Result GC_TODO: add argument description\r
352\r
353 @retval EFI_SUCCESS The high level command execution result is\r
354 retrieved in Result.\r
355 @retval Others Failed to retrieve the result.\r
356\r
357**/\r
e237e7ae 358EFI_STATUS\r
359UsbCbiGetStatus (\r
360 IN USB_CBI_PROTOCOL *UsbCbi,\r
361 IN UINT32 Timeout,\r
362 OUT USB_CBI_STATUS *Result\r
363 )\r
364{\r
365 UINTN Len;\r
366 UINT8 Endpoint;\r
367 EFI_STATUS Status;\r
368 UINT32 TransStatus;\r
369 INTN Retry;\r
370\r
371 Endpoint = UsbCbi->InterruptEndpoint->EndpointAddress;\r
372 Status = EFI_SUCCESS;\r
41e8ff27 373 Timeout = Timeout / USB_MASS_1_MILLISECOND;\r
e237e7ae 374\r
375 //\r
376 // Attemp to the read the result from interrupt endpoint\r
377 //\r
378 for (Retry = 0; Retry < USB_CBI_MAX_RETRY; Retry++) {\r
379 TransStatus = 0;\r
380 Len = sizeof (USB_CBI_STATUS);\r
381\r
382 Status = UsbCbi->UsbIo->UsbSyncInterruptTransfer (\r
383 UsbCbi->UsbIo,\r
384 Endpoint,\r
385 Result,\r
386 &Len,\r
387 Timeout,\r
388 &TransStatus\r
389 );\r
390 //\r
391 // The CBI can NAK the interrupt endpoint if the command is in-progress.\r
392 //\r
393 if (EFI_ERROR (Status) && (TransStatus == EFI_USB_ERR_NAK)) {\r
394 continue;\r
395 }\r
396\r
397 break;\r
398 }\r
399\r
400 return Status;\r
401}\r
402\r
403\r
404/**\r
cc5166ff 405 Execute USB mass storage command through the CBI0/CBI1 transport protocol.\r
e237e7ae 406\r
407 @param Context The USB CBI device\r
408 @param Cmd The command to transfer to device\r
409 @param CmdLen The length of the command\r
410 @param DataDir The direction of data transfer\r
411 @param Data The buffer to hold the data\r
412 @param DataLen The length of the buffer\r
c7e39923 413 @param Lun Should be 0, this field for bot only\r
e237e7ae 414 @param Timeout The time to wait\r
415 @param CmdStatus The result of the command execution\r
416\r
417 @retval EFI_SUCCESS The command is executed OK and result in CmdStatus.\r
cc5166ff 418 @retval Other Failed to execute the command\r
e237e7ae 419\r
420**/\r
e237e7ae 421EFI_STATUS\r
422UsbCbiExecCommand (\r
423 IN VOID *Context,\r
424 IN VOID *Cmd,\r
425 IN UINT8 CmdLen,\r
426 IN EFI_USB_DATA_DIRECTION DataDir,\r
427 IN VOID *Data,\r
428 IN UINT32 DataLen,\r
c7e39923 429 IN UINT8 Lun,\r
e237e7ae 430 IN UINT32 Timeout,\r
431 OUT UINT32 *CmdStatus\r
432 )\r
433{\r
434 USB_CBI_PROTOCOL *UsbCbi;\r
435 USB_CBI_STATUS Result;\r
436 EFI_STATUS Status;\r
437 UINTN TransLen;\r
438\r
439 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
440 UsbCbi = (USB_CBI_PROTOCOL *) Context;\r
441\r
442 //\r
443 // Send the command to the device. Return immediately if device\r
444 // rejects the command.\r
445 //\r
446 Status = UsbCbiSendCommand (UsbCbi, Cmd, CmdLen, Timeout);\r
447 if (EFI_ERROR (Status)) {\r
1c619535 448 DEBUG ((EFI_D_ERROR, "UsbCbiExecCommand: UsbCbiSendCommand (%r)\n",Status));\r
e237e7ae 449 return Status;\r
450 }\r
451\r
452 //\r
453 // Transfer the data, return this status if no interrupt endpoint\r
454 // is used to report the transfer status.\r
455 //\r
456 TransLen = (UINTN) DataLen;\r
457\r
458 Status = UsbCbiDataTransfer (UsbCbi, DataDir, Data, &TransLen, Timeout);\r
459 if (UsbCbi->InterruptEndpoint == NULL) {\r
1c619535 460 DEBUG ((EFI_D_ERROR, "UsbCbiExecCommand: UsbCbiDataTransfer (%r)\n",Status));\r
e237e7ae 461 return Status;\r
462 }\r
463\r
464 //\r
465 // Get the status, if that succeeds, interpret the result\r
466 //\r
467 Status = UsbCbiGetStatus (UsbCbi, Timeout, &Result);\r
468 if (EFI_ERROR (Status)) {\r
1c619535 469 DEBUG ((EFI_D_ERROR, "UsbCbiExecCommand: UsbCbiGetStatus (%r)\n",Status));\r
e237e7ae 470 return EFI_DEVICE_ERROR;\r
471 }\r
472\r
473 if (UsbCbi->Interface.InterfaceSubClass == USB_MASS_STORE_UFI) {\r
474 //\r
475 // For UFI device, ASC and ASCQ are returned.\r
476 //\r
477 if (Result.Type != 0) {\r
478 *CmdStatus = USB_MASS_CMD_FAIL;\r
479 }\r
480\r
481 } else {\r
482 //\r
483 // Check page 27, CBI spec 1.1 for vaious reture status.\r
484 //\r
485 switch (Result.Value & 0x03) {\r
486 case 0x00:\r
487 //\r
488 // Pass\r
489 //\r
490 *CmdStatus = USB_MASS_CMD_SUCCESS;\r
491 break;\r
492\r
493 case 0x02:\r
494 //\r
495 // Phase Error, response with reset. Fall through to Fail.\r
496 //\r
497 UsbCbiResetDevice (UsbCbi, FALSE);\r
498\r
499 case 0x01:\r
500 //\r
501 // Fail\r
502 //\r
503 *CmdStatus = USB_MASS_CMD_FAIL;\r
504 break;\r
505\r
506 case 0x03:\r
507 //\r
508 // Persistent Fail, need to send REQUEST SENSE.\r
509 //\r
510 *CmdStatus = USB_MASS_CMD_PERSISTENT;\r
511 break;\r
512 }\r
513 }\r
514\r
515 return EFI_SUCCESS;\r
516}\r
517\r
518\r
519/**\r
520 Call the Usb mass storage class transport protocol to\r
521 reset the device. The reset is defined as a Non-Data\r
522 command. Don't use UsbCbiExecCommand to send the command\r
523 to device because that may introduce recursive loop.\r
524\r
525 @param Context The USB CBI device protocol\r
cc5166ff 526 @param ExtendedVerification The flag controlling the rule of reset\r
e237e7ae 527\r
528 @retval EFI_SUCCESS the device is reset\r
529 @retval Others Failed to reset the device\r
530\r
531**/\r
e237e7ae 532EFI_STATUS\r
533UsbCbiResetDevice (\r
534 IN VOID *Context,\r
535 IN BOOLEAN ExtendedVerification\r
536 )\r
537{\r
538 UINT8 ResetCmd[USB_CBI_RESET_CMD_LEN];\r
539 USB_CBI_PROTOCOL *UsbCbi;\r
540 USB_CBI_STATUS Result;\r
541 EFI_STATUS Status;\r
542 UINT32 Timeout;\r
543\r
544 UsbCbi = (USB_CBI_PROTOCOL *) Context;\r
545\r
546 //\r
547 // Fill in the reset command.\r
548 //\r
549 SetMem (ResetCmd, USB_CBI_RESET_CMD_LEN, 0xFF);\r
550\r
551 ResetCmd[0] = 0x1D;\r
552 ResetCmd[1] = 0x04;\r
41e8ff27 553 Timeout = USB_CBI_RESET_DEVICE_TIMEOUT / USB_MASS_1_MILLISECOND;\r
e237e7ae 554\r
555 //\r
556 // Send the command to the device. Don't use UsbCbiExecCommand here.\r
557 //\r
558 Status = UsbCbiSendCommand (UsbCbi, ResetCmd, USB_CBI_RESET_CMD_LEN, Timeout);\r
559 if (EFI_ERROR (Status)) {\r
560 return Status;\r
561 }\r
562\r
563 //\r
564 // Just retrieve the status and ignore that. Then stall\r
565 // 50ms to wait it complete\r
566 //\r
567 UsbCbiGetStatus (UsbCbi, Timeout, &Result);\r
41e8ff27 568 gBS->Stall (USB_CBI_RESET_DEVICE_STALL);\r
e237e7ae 569\r
570 //\r
571 // Clear the Bulk-In and Bulk-Out stall condition and\r
572 // init data toggle.\r
573 //\r
574 UsbClearEndpointStall (UsbCbi->UsbIo, UsbCbi->BulkInEndpoint->EndpointAddress);\r
575 UsbClearEndpointStall (UsbCbi->UsbIo, UsbCbi->BulkOutEndpoint->EndpointAddress);\r
576 return Status;\r
577}\r
578\r
579\r
580/**\r
cc5166ff 581 Clean up the CBI protocol's resource.\r
e237e7ae 582\r
cc5166ff 583 @param Context The instance of CBI protocol.\r
e237e7ae 584\r
585 @retval EFI_SUCCESS The resource is cleaned up.\r
586\r
587**/\r
e237e7ae 588EFI_STATUS\r
589UsbCbiFini (\r
590 IN VOID *Context\r
591 )\r
592{\r
593 gBS->FreePool (Context);\r
594 return EFI_SUCCESS;\r
595}\r
596\r
597USB_MASS_TRANSPORT\r
598mUsbCbi0Transport = {\r
599 USB_MASS_STORE_CBI0,\r
600 UsbCbiInit,\r
601 UsbCbiExecCommand,\r
602 UsbCbiResetDevice,\r
c7e39923 603 NULL,\r
e237e7ae 604 UsbCbiFini\r
605};\r
606\r
607USB_MASS_TRANSPORT\r
608mUsbCbi1Transport = {\r
609 USB_MASS_STORE_CBI1,\r
610 UsbCbiInit,\r
611 UsbCbiExecCommand,\r
612 UsbCbiResetDevice,\r
c7e39923 613 NULL,\r
e237e7ae 614 UsbCbiFini\r
615};\r