xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usb_ecos/usbhost/drvMassStor.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 
79 //=============================================================================
80 #include <MsCommon.h>
81 #include  "include/drvConfig.h"
82 #include  "include/drvErrno.h"
83 #include "include/drvUSB.h"
84 //#include "scsiglue.h"
85 #include "drvTransport.h"
86 #include "drvProtocol.h"
87 //#include "initializers.h"
88 #include "include/drvBitops.h"
89 #include "include/drvCPE_AMBA.h"
90 #include "drvUsbHostConfig.h"
91 #include "drvHCD.h"
92 
93 #if 1
94 
95 #ifdef CONFIG_USB_STORAGE_HP8200e
96 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
97 #include "shuttle_usbat.h"
98 #endif
99 
100 #ifdef CONFIG_USB_STORAGE_SDDR09
101 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
102 #include "sddr09.h"
103 #endif
104 
105 #ifdef CONFIG_USB_STORAGE_SDDR55
106 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
107 #include "sddr55.h"
108 #endif
109 
110 #ifdef CONFIG_USB_STORAGE_DPCM
111 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
112 #include "dpcm.h"
113 #endif
114 
115 #ifdef CONFIG_USB_STORAGE_FREECOM
116 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
117 #include "freecom.h"
118 #endif
119 
120 #ifdef CONFIG_USB_STORAGE_ISD200
121 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
122 #include "isd200.h"
123 #endif
124 
125 #ifdef CONFIG_USB_STORAGE_DATAFAB
126 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
127 #include "datafab.h"
128 #endif
129 
130 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
131 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
132 #include "jumpshot.h"
133 #endif
134 
135 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
136 #ifdef MASS_DEBUG
137 #define US_DEBUGP(x,...) diag_printf(x,__VA_ARGS__)
138 #else
139 #define US_DEBUGP(x,...)
140 #endif
141 
142 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
143 extern struct cpe_dev cpe_ehci_dev;
144 
145 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
146 static int storage_probe(struct usb_interface *iface, const struct usb_device_id *id);
147 
148 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
149 static void storage_disconnect(struct usb_interface *iface);
150 
151 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
152 extern void RemoveUSBDiskPort(U8 uPort, U8 uLunNum);
153 
154 
155 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
156 #define USB_STOR_DEVICE_VER(vend,prod,lo,hi) \
157 	 USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, (vend), (prod), (lo), (hi),0,0,0,0,0,0,0
158 
159 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
160 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
161 		    vendorName, productName,useProtocol, useTransport, \
162 		    initFunction, flags) \
163 { USB_STOR_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
164 
165 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
166 #define GET_INTERFACE_INFO(cl,sc,pr) \
167 	 USB_DEVICE_ID_MATCH_INT_INFO,0,0,0,0,0,0,0, (cl), (sc), (pr),0
168 
169 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
170 #undef UNUSUAL_DEV
171 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
172 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
173 		    vendor_name, product_name, use_protocol, use_transport, \
174 		    init_function, Flags) \
175 { \
176 	 vendor_name,	\
177 	 product_name,	\
178 	 use_protocol,	\
179 	 use_transport,	\
180 	 init_function,	\
181 	 Flags \
182 }
183 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
184 
185 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
186 static struct usb_device_id storage_usb_ids [] =
187 {
188 
189 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
190 //#include "unusual_devs.h"
191 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
192 //#undef UNUSUAL_DEV
193 
194         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
195 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_RBC, US_PR_CB) },
196 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE,US_SC_8020,US_PR_CB) },
197 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE,US_SC_QIC,US_PR_CB) },
198 
199         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
200 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE,US_SC_UFI,US_PR_CB) },
201 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE,US_SC_8070,US_PR_CB) },
202 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE,US_SC_SCSI,US_PR_CB) },
203 
204         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
205 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_RBC, US_PR_CBI) },
206 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_8020, US_PR_CBI) },
207 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_QIC, US_PR_CBI) },
208 
209         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
210 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_UFI, US_PR_CBI) },
211 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_8070, US_PR_CBI) },
212 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
213 
214 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
215 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_RBC, US_PR_BULK) },
216 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_8020, US_PR_BULK) },
217 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_QIC, US_PR_BULK) },
218 
219         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
220 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_UFI, US_PR_BULK) },
221 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_8070, US_PR_BULK) },
222 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
223 
224 	{GET_INTERFACE_INFO (USB_CLASS_STORAGE, 0xFF, US_PR_CB) },
225 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
226 	{ 0,0,0,0,0,0,0,0,0,0,0,0}
227 };
228 
229 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
230 static struct us_unusual_dev us_unusual_dev_list[] =
231 {
232 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
233 //#include "unusual_devs.h"
234 //#undef UNUSUAL_DEV
235 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
236 	{ 0,0, US_SC_RBC,
237 	   US_PR_CB,0,0},
238 	{ 0,0, US_SC_8020,
239 	   US_PR_CB,0,0},
240 	{ 0,0, US_SC_QIC,
241 	   US_PR_CB,0,0},
242 	{ 0,0,  US_SC_UFI,
243 	   US_PR_CB,0,0},
244 	{ 0,0,  US_SC_8070,
245 	   US_PR_CB,0,0},
246 	{ 0,0,  US_SC_SCSI,
247 	   US_PR_CB,0,0},
248 
249 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
250 	{ 0,0,  US_SC_RBC,
251 	   US_PR_CBI,0,0},
252 	{  0,0,  US_SC_8020,
253 	   US_PR_CBI,0,0},
254 	{ 0,0,  US_SC_QIC,
255 	   US_PR_CBI,0,0},
256 	{ 0,0,  US_SC_UFI,
257 	   US_PR_CBI,0,0},
258 	{  0,0, US_SC_8070,
259 	   US_PR_CBI,0,0},
260 	{  0,0,  US_SC_SCSI,
261 	   US_PR_CBI,0,0},
262 
263 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
264 	{  0,0,  US_SC_RBC,
265 	   US_PR_BULK,0,0},
266 	{  0,0,  US_SC_8020,
267 	   US_PR_BULK,0,0},
268 	{  0,0,  US_SC_QIC,
269 	   US_PR_BULK,0,0},
270 	{  0,0,  US_SC_UFI,
271 	   US_PR_BULK,0,0},
272 	{  0,0,  US_SC_8070,
273 	   US_PR_BULK,0,0},
274 	{  0,0,  US_SC_SCSI,
275 	   US_PR_BULK,0,0},
276 
277 	{  0,0,  0xFF,
278 	   US_PR_CB,0,0},
279 
280 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
281 	{ 0,0,0,0,0,0 }
282 };
283 
284 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
285 struct usb_driver usb_storage_driver =
286 {
287 	(void*) 0,
288 	"usb-storage",
289 	storage_probe,
290 	storage_disconnect,
291 	0,
292 	storage_usb_ids
293 };
294 
295 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
296 #if 1
fill_inquiry_response(struct us_data * us,unsigned char * data,U32 data_len)297 void fill_inquiry_response(struct us_data *us, unsigned char *data,
298 							U32 data_len)
299 {
300         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
301 	//int i;
302 //	struct scatterlist *sg;		//YPING??
303 	int len =
304 		us->srb.request_bufflen > data_len ? data_len :
305 		us->srb.request_bufflen;
306 	//int transferred;
307 	//int amt;
308 
309         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
310 	if (data_len<36) // You lose.
311 		return;
312 
313 	if(data[0]&0x20)
314 	{
315                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
316 		memset(data+8,0,28);
317 	}
318 	else
319 	{
320                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
321 		memcpy(data+8, us->unusual_dev->vendorName,
322 			strlen(us->unusual_dev->vendorName) > 8 ? 8 :
323 			strlen(us->unusual_dev->vendorName));
324 
325                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
326 		memcpy(data+16, us->unusual_dev->productName,
327 			strlen(us->unusual_dev->productName) > 16 ? 16 :
328 			strlen(us->unusual_dev->productName));
329 
330                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
331 		data[32] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>12) & 0x0F);
332 		data[33] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>8) & 0x0F);
333 		data[34] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>4) & 0x0F);
334 		data[35] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice) & 0x0F);
335 	}
336 
337 
338         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
339 	if (us->srb.use_sg)
340 	{
341 		printk("fill_inquiry_response: Not support scatter function.");
342 	}
343 	else
344 	{
345 		memset(us->srb.request_buffer, 0, us->srb.request_bufflen);
346 		memcpy(us->srb.request_buffer, data, len);
347 	}
348 }
349 #endif
350 
usb_stor_control_thread(void * __us)351 int usb_stor_control_thread(void * __us)
352 {
353 	struct us_data *us = (struct us_data *)__us;
354 
355 //	lock_kernel();
356 
357 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
358 /*	daemonize("usb-storage");
359 
360 	current->flags |= PF_IOTHREAD;
361 
362 	unlock_kernel();
363 */
364 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
365 //	complete(&(us->notify));
366 
367 //	for(;;)
368 	{
369 //		struct Scsi_Host *host;			//YPING01??
370 		US_DEBUGP("*** thread sleeping.%s","\n");
371 //		if(down_interruptible(&us->sema))
372 //			break;
373 
374 		US_DEBUGP("*** thread awakened.%s","\n");
375 
376 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
377 //		if (us->srb == NULL)
378 //		{
379 //			US_DEBUGP("-- exit command received%s","\n");
380 //			goto SkipForAbort;
381 //			break;
382 //		}
383 //		host = us->srb.device->host;
384 
385 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
386 //		scsi_lock(host);
387 
388 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
389 		if (us->sm_state == US_STATE_ABORTING)
390 		{
391 			us->srb.result = DID_ABORT << 16;
392 			goto SkipForAbort;
393 		}
394 
395 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
396 		us->sm_state = US_STATE_RUNNING;
397 //		scsi_unlock(host);
398 
399 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
400 		down(&(us->dev_semaphore));
401 
402 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
403 
404 		if (test_bit(US_FLIDX_DISCONNECTING, &us->flags))
405 		{
406 			US_DEBUGP("No command during disconnect%s","\n");
407 			us->srb.result = DID_BAD_TARGET << 16;
408 		}
409 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
410 		else if (us->srb.sc_data_direction == SCSI_DATA_UNKNOWN)
411 		{
412 			US_DEBUGP("UNKNOWN data direction%s","\n");
413 			us->srb.result = DID_ERROR << 16;
414 		}
415 
416 #if 0
417 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
418 		else if (us->srb.device->id &&
419 				!(us->flags & US_FL_SCM_MULT_TARG))
420 		{
421 			US_DEBUGP("Bad target number (%d:%d)\n",
422 				  us->srb.device->id, us->srb.device->lun);
423 			us->srb.result = DID_BAD_TARGET << 16;
424 		}
425 
426 		else if (us->srb.device->lun > us->max_lun)
427 		{
428 			US_DEBUGP("Bad LUN (%d:%d)\n",
429 				  us->srb.device->id, us->srb.device->lun);
430 			us->srb.result = DID_BAD_TARGET << 16;
431 		}
432 #endif
433 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
434 		else if ((us->srb.cmnd[0] == INQUIRY) &&
435 			    (us->flags & US_FL_FIX_INQUIRY)) {
436 			unsigned char data_ptr[36] = {
437 			    0x00, 0x80, 0x02, 0x02,
438 			    0x1F, 0x00, 0x00, 0x00};
439 
440 			US_DEBUGP("Faking INQUIRY command%s","\n");
441 			fill_inquiry_response(us, data_ptr, 36);
442 			us->srb.result = SAM_STAT_GOOD;
443 		}
444 
445 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
446 		else
447 		{
448 //			US_DEBUGP(usb_stor_show_command(us->srb)"%s","\n");		//xyz
449 			us->proto_handler(&us->srb, us);
450 		}
451 
452 		/* unlock the device pointers */
453 		up(&(us->dev_semaphore));
454 
455 		/* lock access to the state */
456 //		scsi_lock(host);
457 
458 		/* indicate that the command is done */
459 		if (us->srb.result != DID_ABORT << 16)
460 		{
461 			US_DEBUGP("scsi cmd done, result=0x%x\n",
462 				   us->srb.result);
463 
464 			if(us->srb.scsi_done != NULL)
465 				us->srb.scsi_done(&us->srb);
466 		}
467 		else
468 		{
469 			SkipForAbort:
470 			US_DEBUGP("scsi command aborted%s","\n");
471 		}
472 
473 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
474 		if (us->sm_state == US_STATE_ABORTING)
475 			complete(&(us->notify));
476 
477 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
478 //		us->srb = NULL;			//YPING01??
479 		us->sm_state = US_STATE_IDLE;
480 //		scsi_unlock(host);
481 	} /* for (;;) */
482 
483 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
484 	complete(&(us->notify));
485 
486 	return 0;
487 }
488 
associate_dev(struct us_data * us,struct usb_interface * intf)489 static int associate_dev(struct us_data *us, struct usb_interface *intf)
490 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
491 {
492 
493 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
494 	us->pusb_dev = interface_to_usbdev(intf);
495 
496         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
497 	us->pusb_intf = intf;
498 
499         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
500 	us->ifnum = intf->altsetting->desc.bInterfaceNumber;
501 
502 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
503 	usb_set_intfdata(intf, us);
504 	usb_get_dev(us->pusb_dev);
505 
506 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
507 	us->cr = (struct usb_ctrlrequest*) usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr), GFP_KERNEL, &us->cr_dma);
508         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
509 
510 
511         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
512 	if (!us->cr)
513         {
514                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
515 		US_DEBUGP("[associate_dev] usb_ctrlrequest allocation failed%s","\n");
516                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
517 		return -ENOMEM;
518 	}
519 
520         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
521 	us->iobuf = (unsigned char*) usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE, GFP_KERNEL, &us->iobuf_dma);
522         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
523 
524         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
525 	if (!us->iobuf) {
526                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
527 		US_DEBUGP("[associate_dev] I/O buffer allocation failed%s","\n");
528                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
529 		return -ENOMEM;
530 	}
531 
532         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
533 	return 0;
534 }
535 
get_device_info(struct us_data * us,int id_index)536 static void get_device_info(struct us_data *us, int id_index)
537 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
538 {
539 
540 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
541 	struct usb_interface_descriptor *idesc =
542 		&us->pusb_intf->altsetting[us->pusb_intf->act_altsetting].desc;
543 
544         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
545 	struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
546 
547         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
548 	struct usb_device_id *id = &storage_usb_ids[id_index];
549 
550         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
551 	if (unusual_dev->vendorName)
552 		US_DEBUGP("[get_device_info] Vendor: %s\n", unusual_dev->vendorName);
553 
554         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
555 	if (unusual_dev->productName)
556 		US_DEBUGP("[get_device_info] Product: %s\n", unusual_dev->productName);
557 
558 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
559 	us->unusual_dev = unusual_dev;
560 	us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
561 			idesc->bInterfaceSubClass :
562 			unusual_dev->useProtocol;
563 	us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
564 			idesc->bInterfaceProtocol :
565 			unusual_dev->useTransport;
566 	us->flags = unusual_dev->flags;
567 
568 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
569 	if (id->idVendor || id->idProduct) {
570 
571 		//struct usb_device_descriptor *ddesc = &dev->descriptor;
572 		int msg = -1;
573 
574 		if (unusual_dev->useProtocol != US_SC_DEVICE &&
575 			us->subclass == idesc->bInterfaceSubClass)
576 			msg += 1;
577 		if (unusual_dev->useTransport != US_PR_DEVICE &&
578 			us->protocol == idesc->bInterfaceProtocol)
579 			msg += 2;
580 	}
581 
582 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
583 	if (strlen(us->vendor) == 0) {
584 		if (unusual_dev->vendorName)
585 			strncpy(us->vendor, unusual_dev->vendorName,
586 				sizeof(us->vendor));
587 		else
588 			strncpy(us->vendor, "Unknown", sizeof(us->vendor));
589 	}
590 	if (strlen(us->product) == 0) {
591 		if (unusual_dev->productName)
592 			strncpy(us->product, unusual_dev->productName,
593 				sizeof(us->product));
594 		else
595 			strncpy(us->product, "Unknown", sizeof(us->product));
596 	}
597 	if (strlen(us->serial) == 0)
598 		strncpy(us->serial, "None", sizeof(us->serial));
599 }
600 
601 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
get_transport(struct us_data * us)602 static int get_transport(struct us_data *us)
603 {
604 	switch (us->protocol) {
605         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
606 	case US_PR_CB:
607                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
608 		us->transport_name = "Control/Bulk";
609 		us->transport = usb_stor_CB_transport;
610 		us->transport_reset = usb_stor_CB_reset;
611 		//us->max_lun = 7;
612 		us->max_lun = 0;	//FIXME, Find some way to get real Max. LUN
613 		break;
614 
615 	case US_PR_CBI:
616                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
617 		us->transport_name = "Control/Bulk/Interrupt";
618 		us->transport = usb_stor_CBI_transport;
619 		us->transport_reset = usb_stor_CB_reset;
620 		us->max_lun = 0;
621 		break;
622 
623 	case US_PR_BULK:
624                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
625 		us->transport_name = "Bulk";
626 		us->transport = usb_stor_Bulk_transport;
627 		us->transport_reset = usb_stor_Bulk_reset;
628 		break;
629 
630 #ifdef CONFIG_USB_STORAGE_HP8200e
631         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
632 	case US_PR_SCM_ATAPI:
633                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
634 		us->transport_name = "SCM/ATAPI";
635 		us->transport = hp8200e_transport;
636 		us->transport_reset = usb_stor_CB_reset;
637 		us->max_lun = 1;
638 		break;
639 #endif
640 
641 #ifdef CONFIG_USB_STORAGE_SDDR09
642         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
643 	case US_PR_EUSB_SDDR09:
644                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
645 		us->transport_name = "EUSB/SDDR09";
646 		us->transport = sddr09_transport;
647 		us->transport_reset = usb_stor_CB_reset;
648 		us->max_lun = 0;
649 		break;
650 #endif
651 
652 #ifdef CONFIG_USB_STORAGE_SDDR55
653         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
654 	case US_PR_SDDR55:
655                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
656 		us->transport_name = "SDDR55";
657 		us->transport = sddr55_transport;
658 		us->transport_reset = sddr55_reset;
659 		us->max_lun = 0;
660 		break;
661 #endif
662 
663 #ifdef CONFIG_USB_STORAGE_DPCM
664         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
665 	case US_PR_DPCM_USB:
666                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
667 		us->transport_name = "Control/Bulk-EUSB/SDDR09";
668 		us->transport = dpcm_transport;
669 		us->transport_reset = usb_stor_CB_reset;
670 		us->max_lun = 1;
671 		break;
672 #endif
673 
674 #ifdef CONFIG_USB_STORAGE_FREECOM
675         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
676 	case US_PR_FREECOM:
677 		us->transport_name = "Freecom";
678 		us->transport = freecom_transport;
679 		us->transport_reset = usb_stor_freecom_reset;
680 		us->max_lun = 0;
681 		break;
682 #endif
683 
684 #ifdef CONFIG_USB_STORAGE_DATAFAB
685         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
686 	case US_PR_DATAFAB:
687                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
688 		us->transport_name  = "Datafab Bulk-Only";
689 		us->transport = datafab_transport;
690 		us->transport_reset = usb_stor_Bulk_reset;
691 		us->max_lun = 1;
692 		break;
693 #endif
694 
695 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
696         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
697 	case US_PR_JUMPSHOT:
698                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
699 		us->transport_name  = "Lexar Jumpshot Control/Bulk";
700 		us->transport = jumpshot_transport;
701 		us->transport_reset = usb_stor_Bulk_reset;
702 		us->max_lun = 1;
703 		break;
704 #endif
705 
706 	default:
707                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
708 		return -EIO;
709 	}
710         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
711 	US_DEBUGP("Transport: %s\n", us->transport_name);
712 
713 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
714 	if (us->flags & US_FL_SINGLE_LUN)
715 		us->max_lun = 0;
716 	return 0;
717 }
718 
719 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
get_protocol(struct us_data * us)720 static int get_protocol(struct us_data *us)
721 {
722 	switch (us->subclass) {
723 	case US_SC_RBC:
724 		us->protocol_name = "Reduced Block Commands (RBC)";
725 //		USB_sprintf(us->protocol_name,"Reduced Block Commands (RBC)");
726 		us->proto_handler = usb_stor_transparent_scsi_command;
727 		break;
728 
729 	case US_SC_8070:
730 	case US_SC_SCSI:
731 	case 0xFF:	// For Sony DC P8 (P series) and Sony DV
732 		us->protocol_name = "Transparent SCSI";
733 //		USB_sprintf(us->protocol_name,"Transparent SCSI");
734 		us->proto_handler = usb_stor_transparent_scsi_command;
735 		break;
736 
737 	case US_SC_8020:
738 		us->protocol_name = "8020i";
739 		us->proto_handler = usb_stor_transparent_scsi_command;
740 		us->max_lun = 0;
741 		break;
742 
743 #if SupportOtherDevice
744 #if 0
745 	case US_SC_8020:
746 		us->protocol_name = "8020i";
747 		us->proto_handler = usb_stor_ATAPI_command;
748 		us->max_lun = 0;
749 		break;
750 #endif
751 
752 	case US_SC_QIC:
753 		us->protocol_name = "QIC-157";
754 		us->proto_handler = usb_stor_qic157_command;
755 		us->max_lun = 0;
756 		break;
757 #if 0
758 	case US_SC_8070:
759 		us->protocol_name = "8070i";
760 		us->proto_handler = usb_stor_ATAPI_command;
761 		us->max_lun = 0;
762 		break;
763 #endif
764 
765 	case US_SC_UFI:
766 		us->protocol_name = "Uniform Floppy Interface (UFI)";
767 		us->proto_handler = usb_stor_ufi_command;
768 		break;
769 #endif
770 
771 #ifdef CONFIG_USB_STORAGE_ISD200
772 	case US_SC_ISD200:
773 		us->protocol_name = "ISD200 ATA/ATAPI";
774 		us->proto_handler = isd200_ata_command;
775 		break;
776 #endif
777 
778 	default:
779 		return -EIO;
780 	}
781         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
782 	US_DEBUGP("Protocol: %s\n", us->protocol_name);
783         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
784 	return 0;
785 }
786 
787 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
get_pipes(struct us_data * us)788 static int get_pipes(struct us_data *us)
789 {
790         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
791 	struct usb_host_interface *altsetting =	&us->pusb_intf->altsetting[us->pusb_intf->act_altsetting];
792 
793         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
794 	int ii;
795 
796         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
797 	struct usb_endpoint_descriptor *ep_tmp;
798 
799         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
800 	struct usb_endpoint_descriptor *ep_in = NULL;
801 
802         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
803 	struct usb_endpoint_descriptor *ep_out = NULL;
804 
805         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
806 	struct usb_endpoint_descriptor *ep_int = NULL;
807 
808 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
809 	for (ii = 0; ii < altsetting->desc.bNumEndpoints; ii++)
810         {
811                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
812 		ep_tmp=&altsetting->endpoint[ii].desc;
813 
814                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
815 		if ((ep_tmp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK)
816                 {
817 			/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
818 			if (ep_tmp->bEndpointAddress & USB_DIR_IN)
819                         {
820                                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
821 				ep_in = ep_tmp;
822                         }
823 			else
824                         {
825                                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
826 				ep_out = ep_tmp;
827                         }
828 		}
829                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
830 		else
831                 if ((ep_tmp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
832                              == USB_ENDPOINT_XFER_INT)
833                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
834                 {
835                         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
836                        	ep_int = ep_tmp;
837 
838 		}
839 	}
840 
841 
842         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
843 	if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int))
844         {
845                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
846 		US_DEBUGP("Endpoint sanity check failed! Rejecting dev.%s","\n");
847                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
848 		return -EIO;
849 	}
850 
851 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
852 	us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
853 
854         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
855 	us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
856 
857         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
858 	us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
859 		ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
860 
861         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
862 	us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
863 		ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
864 
865         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
866 	if (ep_int) {
867 #if 1    //
868                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
869 		us->recv_intr_pipe = usb_rcvbulkpipe(us->pusb_dev,
870 			ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
871 #else
872                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
873 		us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
874 			ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
875 #endif
876                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
877 		us->ep_bInterval = ep_int->bInterval;
878 	}
879         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
880 	return 0;
881 }
882 
883 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
usb_stor_acquire_resources(struct us_data * us)884 static int usb_stor_acquire_resources(struct us_data *us)
885 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
886 {
887 	//int p;
888 
889 	us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
890 	if (!us->current_urb) {
891 		US_DEBUGP("URB allocation failed%s","\n");
892 		return -ENOMEM;
893 	}
894 	us->data_urb = usb_alloc_urb(0, GFP_KERNEL);
895 	if (!us->data_urb) {
896 		US_DEBUGP("URB allocation failed%s","\n");
897 		return -ENOMEM;
898 	}
899 	us->status_urb = usb_alloc_urb(0, GFP_KERNEL);
900 	if (!us->status_urb) {
901 		US_DEBUGP("URB allocation failed%s","\n");
902 		return -ENOMEM;
903 	}
904 
905 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
906 	down(&us->dev_semaphore);
907 
908 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
909 	if (us->protocol == US_PR_BULK)
910 		us->max_lun = usb_stor_Bulk_max_lun(us);
911 
912 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
913 	us->Mass_stor_device=(struct LUN_Device*)kmalloc(sizeof(struct LUN_Device)*(us->max_lun+1), GFP_KERNEL);
914 	memset(us->Mass_stor_device,0 , sizeof(struct LUN_Device)*(us->max_lun+1));
915 
916 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
917 	if (us->unusual_dev->initFunction)
918 		us->unusual_dev->initFunction(us);
919 
920 	up(&us->dev_semaphore);
921 
922 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
923 	us->sm_state = US_STATE_IDLE;
924 /*			//YPING??
925 	p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
926 	if (p < 0) {
927 		printk(KERN_WARNING USB_STORAGE
928 		       "Unable to start control thread\n");
929 		return p;
930 	}
931 	us->pid = p;
932 */
933 
934 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
935 //	wait_for_completion(&(us->notify));
936 
937 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
938 /*	    	//YPING??
939 	us->host = scsi_host_alloc(&usb_stor_host_template, sizeof(us));
940 	if (!us->host) {
941 		printk(KERN_WARNING USB_STORAGE
942 			"Unable to register the scsi host\n");
943 		return -EBUSY;
944 	}
945 */
946 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
947 //	us->host->hostdata[0] = (unsigned long) us;		//YPING??
948 
949 	return 0;
950 }
951 
952 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
dissociate_dev(struct us_data * us)953 static void dissociate_dev(struct us_data *us)
954 {
955 
956 	US_DEBUGP("-- %s\n", __FUNCTION__);
957         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
958 	down(&us->dev_semaphore);
959 
960 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
961 	if (us->cr) {
962 		usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
963 				us->cr_dma);
964 		us->cr = NULL;
965 	}
966         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
967 	if (us->iobuf) {
968 		usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
969 				us->iobuf_dma);
970 		us->iobuf = NULL;
971 	}
972 
973 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
974 	usb_set_intfdata(us->pusb_intf, NULL);
975 	usb_put_dev(us->pusb_dev);
976 
977         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
978 	us->pusb_dev = NULL;
979 	us->pusb_intf = NULL;
980 	up(&us->dev_semaphore);
981 }
982 
983 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
usb_stor_release_resources(struct us_data * us)984 void usb_stor_release_resources(struct us_data *us)
985 {
986 diag_printf("usb_stor_release_resources\n");
987     MS_U8   ii, iLun;
988 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
989 
990 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
991 /*					//YPING??
992 	if (us->host) {
993 		(struct us_data *) us->host->hostdata[0] = NULL;
994 		scsi_host_put(us->host);
995 	}
996 */
997 
998 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
999 /*	    			//YPING??
1000 	if (us->pid) {
1001 		US_DEBUGP("-- sending exit command to thread\n");
1002 		BUG_ON(us->sm_state != US_STATE_IDLE);
1003 		us->srb = NULL;
1004 		up(&(us->sema));
1005 		wait_for_completion(&(us->notify));
1006 	}
1007 */
1008 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1009 	if (us->extra_destructor) {
1010 		US_DEBUGP("-- calling extra_destructor()%s","\n");
1011 		us->extra_destructor(us->extra);
1012 	}
1013 
1014 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1015 	if (us->extra)
1016 		kfree(us->extra);
1017 	if (us->current_urb)
1018 		usb_free_urb(us->current_urb);
1019 	if (us->data_urb)
1020 		usb_free_urb(us->data_urb);
1021 	if (us->status_urb)
1022 		usb_free_urb(us->status_urb);
1023 
1024 	/* Free the structure itself */
1025 	//vUSBDisk_FileSystem_Remove();
1026 
1027     for (ii=0; ii<NUM_OF_ROOT_HUB*MAX_USTOR; ii++)
1028     {
1029         if (Mass_stor_us[ii] == us)
1030         {
1031             for (iLun=0; iLun<=Mass_stor_us[ii]->max_lun; iLun++)
1032                 RemoveUSBDiskPort(Mass_stor_us[ii]->ustor_id, iLun);
1033 
1034             Mass_stor_us[ii] = NULL;
1035         }
1036     }
1037     if (us->Mass_stor_device)
1038         kfree(us->Mass_stor_device);
1039 
1040   kfree(us);
1041  // US_DEBUGP("-- %s finished\n", __FUNCTION__);
1042 //  US_DEBUGP("USB Mass Storage device disconnected\n");
1043 }
1044 
1045 /* Get free ID from MAX_USTOR during one host controller support range */
GetUStorID(unsigned char startId)1046 int GetUStorID(unsigned char startId)
1047 {
1048     int i;
1049 
1050     if (startId >= NUM_OF_ROOT_HUB*MAX_USTOR)
1051         ASSERT(0);
1052 
1053     for (i=startId; i<startId+MAX_USTOR; i++)
1054     {
1055         if (Mass_stor_us[i] == NULL)
1056             return i;
1057     }
1058 
1059     diag_printf("!! ERROR: Exceed the max support number of usb storage device !!\n");
1060     return (-1);
1061 }
1062 
1063 
1064 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
storage_probe(struct usb_interface * intf,const struct usb_device_id * id)1065 static int storage_probe(struct usb_interface *intf,
1066 			 const struct usb_device_id *id)
1067 {
1068 
1069 	struct us_data *us;
1070 	const int id_index = id - storage_usb_ids;
1071 	int result,i;
1072 	struct usb_device *pudev;
1073 
1074         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1075         diag_printf("storage_probe\n");
1076 	US_DEBUGP("USB Mass Storage device detected%s","\n");
1077 	US_DEBUGP("act_altsetting is %d, id_index is %d\n",
1078 			intf->act_altsetting, id_index);
1079 
1080 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1081 	us = (struct us_data *) kmalloc(sizeof(*us), GFP_KERNEL);
1082 	if (!us)
1083         {
1084 		US_DEBUGP("Out of memory %d\n", us);
1085 		return -ENOMEM;
1086 	}
1087 
1088         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1089 	memset(us, 0, sizeof(struct us_data));
1090         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1091         init_MUTEX(&(us->dev_semaphore));
1092         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1093 	init_MUTEX_LOCKED(&(us->sema));
1094         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1095 	init_completion(&(us->notify));
1096 
1097 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1098 	result = associate_dev(us, intf);
1099         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1100 	if (result)
1101 	      goto BadDevice;
1102 
1103 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1104 	get_device_info(us, id_index);
1105 
1106 #ifdef CONFIG_USB_STORAGE_SDDR09
1107         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1108 	if (us->protocol == US_PR_EUSB_SDDR09 || us->protocol == US_PR_DPCM_USB)
1109         {
1110 		/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1111 		result = usb_set_configuration(us->pusb_dev, 1);
1112                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1113 		US_DEBUGP("Result from usb_set_configuration is %d\n", result);
1114                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1115 		if (result == -EPIPE)
1116                 {
1117                         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1118 			US_DEBUGP("-- stall on control interface\n");
1119 		}
1120                 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1121                 else if (result != 0)
1122                 {
1123 			/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1124 			US_DEBUGP("-- Unknown error.  Rejecting device\n");
1125                         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1126 			goto BadDevice;
1127 		}
1128 	}
1129 #endif
1130 
1131 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1132 	result = get_transport(us);
1133         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1134 	if (result)
1135 		goto BadDevice;
1136         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1137 	result = get_protocol(us);
1138         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1139 	if (result)
1140 		goto BadDevice;
1141         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1142 	result = get_pipes(us);
1143         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1144 	if (result)
1145 		goto BadDevice;
1146 
1147 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1148 	result = usb_stor_acquire_resources(us);
1149         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1150 	if (result)
1151 		goto BadDevice;
1152 
1153 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1154 	pudev = interface_to_usbdev(intf);
1155         i = GetUStorID(MAX_USTOR* (((struct usb_hcd *)pudev->bus->hcpriv)->host_id));
1156 
1157     if (i != (-1))
1158     {
1159         Mass_stor_us[i] = us;
1160         us->ustor_id = (unsigned char)i;
1161         //diag_printf("storage_probe: get ustor_id: %d\n", i);
1162     }
1163     else
1164         goto BadDevice;
1165 
1166         Mass_stor_us[i]->host_id = ((struct usb_hcd *)pudev->bus->hcpriv)->host_id;
1167 
1168 
1169 	for(i = 0; i<=us->max_lun; i++)
1170 		us->Mass_stor_device[i].bDeviceReady = FALSE;
1171 	//Mass_stor_us->bFileSystemInit = FALSE;
1172 
1173 	kdbg("WARNING: USB Mass Storage data integrity not assured%s\n","");
1174 
1175 	US_DEBUGP("USB Mass Storage device found at %d\n", us->pusb_dev->devnum);
1176 
1177 	return 0;
1178 
1179 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1180 BadDevice:
1181 	US_DEBUGP("storage_probe() failed%s","\n");
1182 diag_printf("storage_probe() failed%s","\n");
1183         /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1184 	dissociate_dev(us);
1185 
1186 	usb_stor_release_resources(us);
1187 
1188 	return result;
1189 
1190 }
1191 
1192 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
storage_disconnect(struct usb_interface * intf)1193 static void storage_disconnect(struct usb_interface *intf)
1194 {
1195 	struct us_data *us = (struct us_data*) usb_get_intfdata(intf);
1196 //	struct scsi_device *sdev;
1197 
1198 	diag_printf("storage_disconnect() : ustor_id: %d \n", us->ustor_id);
1199 
1200 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1201 /*
1202 	scsi_lock(us->host);
1203 	list_for_each_entry(sdev, &us->host->my_devices, siblings)
1204 		sdev->online = 0;
1205 	scsi_unlock(us->host);
1206 */
1207 
1208 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1209 	set_bit(US_FLIDX_DISCONNECTING, &us->flags, unsigned long);
1210 	usb_stor_stop_transport(us);
1211 
1212 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1213 	dissociate_dev(us);
1214 
1215 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1216 /*
1217 	if (scsi_remove_host(us->host))
1218 	{
1219 		US_DEBUGP("-- SCSI refused to remove the host\n");
1220 		BUG();
1221 		return;
1222 	}
1223 */
1224 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1225 	usb_stor_release_resources(us);
1226 }
1227 
1228 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1229 
usb_stor_init(void)1230 int  usb_stor_init(void)
1231 {
1232 	printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1233 
1234 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1235 	if (usb_register(&usb_storage_driver) < 0)
1236 		return -1;
1237 
1238 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1239 	printk(KERN_INFO "USB Mass Storage support registered.\n");
1240 	return 0;
1241 }
1242 
1243 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
usb_stor_exit(void)1244 void  usb_stor_exit(void)
1245 {
1246 
1247 	US_DEBUGP("usb_stor_exit() called%s","\n");
1248 
1249 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1250 	US_DEBUGP("-- calling usb_deregister()%s","\n");
1251 	usb_deregister(&usb_storage_driver) ;
1252 
1253 #if 0
1254 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1255 	for (next = us_list; next; next = next->next) {
1256 		US_DEBUGP("-- calling scsi_unregister_host()\n");
1257 		scsi_unregister_host(&usb_stor_host_template);
1258 	}
1259 
1260 	/*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1261 	while (us_list) {
1262 		/* keep track of where the next one is */
1263 		next = us_list->next;
1264 
1265 		/* If there's extra data in the us_data structure then
1266 		 * free that first */
1267 		if (us_list->extra) {
1268 			/* call the destructor routine, if it exists */
1269 			if (us_list->extra_destructor) {
1270 				US_DEBUGP("-- calling extra_destructor()\n");
1271 				us_list->extra_destructor(us_list->extra);
1272 			}
1273 
1274 			/* destroy the extra data */
1275 			US_DEBUGP("-- freeing the data structure\n");
1276 			kfree(us_list->extra);
1277 		}
1278 
1279 		/* free the structure itself */
1280 		kfree (us_list);
1281 
1282 		/* advance the list pointer */
1283 		us_list = next;
1284 	}
1285 #endif
1286 }
1287 
1288 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1289  /*
1290 int Test_and_Clear_Bit(int nr, void *addr)
1291 {
1292 	U32 mask, retval;
1293 	U32 *adr = (U32 *)addr;
1294 
1295 	adr += nr >> 5;
1296 	mask = 1 << (nr & 0x1f);
1297 	retval = (mask & *adr) != 0;
1298 	*adr &= ~mask;
1299 	return retval;
1300 }
1301 
1302  int Test_Bit(int nr, long * addr)
1303 {
1304 	int	mask;
1305 
1306 	addr += nr >> 5;
1307 	mask = 1 << (nr & 0x1f);
1308 	return ((mask & *addr) != 0);
1309 }
1310 */
1311 
1312 /*USB HOST USB HOST USB HOST USB HOST USB HOST USB HOST*/
1313 
1314 #endif	//#ifdef
1315 
1316