1 /*
2  *  Sony MemoryStick support
3  *
4  *  Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11  * that made this driver possible.
12  *
13  */
14 
15 #include <linux/memstick.h>
16 #include <linux/idr.h>
17 #include <linux/fs.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 
23 #define DRIVER_NAME "memstick"
24 
25 static unsigned int cmd_retries = 3;
26 module_param(cmd_retries, uint, 0644);
27 
28 static struct workqueue_struct *workqueue;
29 static DEFINE_IDR(memstick_host_idr);
30 static DEFINE_SPINLOCK(memstick_host_lock);
31 
memstick_dev_match(struct memstick_dev * card,struct memstick_device_id * id)32 static int memstick_dev_match(struct memstick_dev *card,
33 			      struct memstick_device_id *id)
34 {
35 	if (id->match_flags & MEMSTICK_MATCH_ALL) {
36 		if ((id->type == card->id.type)
37 		    && (id->category == card->id.category)
38 		    && (id->class == card->id.class))
39 			return 1;
40 	}
41 
42 	return 0;
43 }
44 
memstick_bus_match(struct device * dev,struct device_driver * drv)45 static int memstick_bus_match(struct device *dev, struct device_driver *drv)
46 {
47 	struct memstick_dev *card = container_of(dev, struct memstick_dev,
48 						 dev);
49 	struct memstick_driver *ms_drv = container_of(drv,
50 						      struct memstick_driver,
51 						      driver);
52 	struct memstick_device_id *ids = ms_drv->id_table;
53 
54 	if (ids) {
55 		while (ids->match_flags) {
56 			if (memstick_dev_match(card, ids))
57 				return 1;
58 			++ids;
59 		}
60 	}
61 	return 0;
62 }
63 
memstick_uevent(struct device * dev,struct kobj_uevent_env * env)64 static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
65 {
66 	struct memstick_dev *card = container_of(dev, struct memstick_dev,
67 						  dev);
68 
69 	if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
70 		return -ENOMEM;
71 
72 	if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
73 		return -ENOMEM;
74 
75 	if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
76 		return -ENOMEM;
77 
78 	return 0;
79 }
80 
memstick_device_probe(struct device * dev)81 static int memstick_device_probe(struct device *dev)
82 {
83 	struct memstick_dev *card = container_of(dev, struct memstick_dev,
84 						 dev);
85 	struct memstick_driver *drv = container_of(dev->driver,
86 						   struct memstick_driver,
87 						   driver);
88 	int rc = -ENODEV;
89 
90 	if (dev->driver && drv->probe) {
91 		rc = drv->probe(card);
92 		if (!rc)
93 			get_device(dev);
94 	}
95 	return rc;
96 }
97 
memstick_device_remove(struct device * dev)98 static int memstick_device_remove(struct device *dev)
99 {
100 	struct memstick_dev *card = container_of(dev, struct memstick_dev,
101 						  dev);
102 	struct memstick_driver *drv = container_of(dev->driver,
103 						   struct memstick_driver,
104 						   driver);
105 
106 	if (dev->driver && drv->remove) {
107 		drv->remove(card);
108 		card->dev.driver = NULL;
109 	}
110 
111 	put_device(dev);
112 	return 0;
113 }
114 
115 #ifdef CONFIG_PM
116 
memstick_device_suspend(struct device * dev,pm_message_t state)117 static int memstick_device_suspend(struct device *dev, pm_message_t state)
118 {
119 	struct memstick_dev *card = container_of(dev, struct memstick_dev,
120 						  dev);
121 	struct memstick_driver *drv = container_of(dev->driver,
122 						   struct memstick_driver,
123 						   driver);
124 
125 	if (dev->driver && drv->suspend)
126 		return drv->suspend(card, state);
127 	return 0;
128 }
129 
memstick_device_resume(struct device * dev)130 static int memstick_device_resume(struct device *dev)
131 {
132 	struct memstick_dev *card = container_of(dev, struct memstick_dev,
133 						  dev);
134 	struct memstick_driver *drv = container_of(dev->driver,
135 						   struct memstick_driver,
136 						   driver);
137 
138 	if (dev->driver && drv->resume)
139 		return drv->resume(card);
140 	return 0;
141 }
142 
143 #else
144 
145 #define memstick_device_suspend NULL
146 #define memstick_device_resume NULL
147 
148 #endif /* CONFIG_PM */
149 
150 #define MEMSTICK_ATTR(name, format)                                           \
151 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
152 			    char *buf)                                        \
153 {                                                                             \
154 	struct memstick_dev *card = container_of(dev, struct memstick_dev,    \
155 						 dev);                        \
156 	return sprintf(buf, format, card->id.name);                           \
157 }                                                                             \
158 static DEVICE_ATTR_RO(name);
159 
160 MEMSTICK_ATTR(type, "%02X");
161 MEMSTICK_ATTR(category, "%02X");
162 MEMSTICK_ATTR(class, "%02X");
163 
164 static struct attribute *memstick_dev_attrs[] = {
165 	&dev_attr_type.attr,
166 	&dev_attr_category.attr,
167 	&dev_attr_class.attr,
168 	NULL,
169 };
170 ATTRIBUTE_GROUPS(memstick_dev);
171 
172 static struct bus_type memstick_bus_type = {
173 	.name           = "memstick",
174 	.dev_groups	= memstick_dev_groups,
175 	.match          = memstick_bus_match,
176 	.uevent         = memstick_uevent,
177 	.probe          = memstick_device_probe,
178 	.remove         = memstick_device_remove,
179 	.suspend        = memstick_device_suspend,
180 	.resume         = memstick_device_resume
181 };
182 
memstick_free(struct device * dev)183 static void memstick_free(struct device *dev)
184 {
185 	struct memstick_host *host = container_of(dev, struct memstick_host,
186 						  dev);
187 	kfree(host);
188 }
189 
190 static struct class memstick_host_class = {
191 	.name        = "memstick_host",
192 	.dev_release = memstick_free
193 };
194 
memstick_free_card(struct device * dev)195 static void memstick_free_card(struct device *dev)
196 {
197 	struct memstick_dev *card = container_of(dev, struct memstick_dev,
198 						 dev);
199 	kfree(card);
200 }
201 
memstick_dummy_check(struct memstick_dev * card)202 static int memstick_dummy_check(struct memstick_dev *card)
203 {
204 	return 0;
205 }
206 
207 /**
208  * memstick_detect_change - schedule media detection on memstick host
209  * @host - host to use
210  */
memstick_detect_change(struct memstick_host * host)211 void memstick_detect_change(struct memstick_host *host)
212 {
213 	queue_work(workqueue, &host->media_checker);
214 }
215 EXPORT_SYMBOL(memstick_detect_change);
216 
217 /**
218  * memstick_next_req - called by host driver to obtain next request to process
219  * @host - host to use
220  * @mrq - pointer to stick the request to
221  *
222  * Host calls this function from idle state (*mrq == NULL) or after finishing
223  * previous request (*mrq should point to it). If previous request was
224  * unsuccessful, it is retried for predetermined number of times. Return value
225  * of 0 means that new request was assigned to the host.
226  */
memstick_next_req(struct memstick_host * host,struct memstick_request ** mrq)227 int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
228 {
229 	int rc = -ENXIO;
230 
231 	if ((*mrq) && (*mrq)->error && host->retries) {
232 		(*mrq)->error = rc;
233 		host->retries--;
234 		return 0;
235 	}
236 
237 	if (host->card && host->card->next_request)
238 		rc = host->card->next_request(host->card, mrq);
239 
240 	if (!rc)
241 		host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
242 	else
243 		*mrq = NULL;
244 
245 	return rc;
246 }
247 EXPORT_SYMBOL(memstick_next_req);
248 
249 /**
250  * memstick_new_req - notify the host that some requests are pending
251  * @host - host to use
252  */
memstick_new_req(struct memstick_host * host)253 void memstick_new_req(struct memstick_host *host)
254 {
255 	if (host->card) {
256 		host->retries = cmd_retries;
257 		reinit_completion(&host->card->mrq_complete);
258 		host->request(host);
259 	}
260 }
261 EXPORT_SYMBOL(memstick_new_req);
262 
263 /**
264  * memstick_init_req_sg - set request fields needed for bulk data transfer
265  * @mrq - request to use
266  * @tpc - memstick Transport Protocol Command
267  * @sg - TPC argument
268  */
memstick_init_req_sg(struct memstick_request * mrq,unsigned char tpc,const struct scatterlist * sg)269 void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
270 			  const struct scatterlist *sg)
271 {
272 	mrq->tpc = tpc;
273 	if (tpc & 8)
274 		mrq->data_dir = WRITE;
275 	else
276 		mrq->data_dir = READ;
277 
278 	mrq->sg = *sg;
279 	mrq->long_data = 1;
280 
281 	if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
282 		mrq->need_card_int = 1;
283 	else
284 		mrq->need_card_int = 0;
285 }
286 EXPORT_SYMBOL(memstick_init_req_sg);
287 
288 /**
289  * memstick_init_req - set request fields needed for short data transfer
290  * @mrq - request to use
291  * @tpc - memstick Transport Protocol Command
292  * @buf - TPC argument buffer
293  * @length - TPC argument size
294  *
295  * The intended use of this function (transfer of data items several bytes
296  * in size) allows us to just copy the value between request structure and
297  * user supplied buffer.
298  */
memstick_init_req(struct memstick_request * mrq,unsigned char tpc,const void * buf,size_t length)299 void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
300 		       const void *buf, size_t length)
301 {
302 	mrq->tpc = tpc;
303 	if (tpc & 8)
304 		mrq->data_dir = WRITE;
305 	else
306 		mrq->data_dir = READ;
307 
308 	mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
309 	if (mrq->data_dir == WRITE)
310 		memcpy(mrq->data, buf, mrq->data_len);
311 
312 	mrq->long_data = 0;
313 
314 	if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
315 		mrq->need_card_int = 1;
316 	else
317 		mrq->need_card_int = 0;
318 }
319 EXPORT_SYMBOL(memstick_init_req);
320 
321 /*
322  * Functions prefixed with "h_" are protocol callbacks. They can be called from
323  * interrupt context. Return value of 0 means that request processing is still
324  * ongoing, while special error value of -EAGAIN means that current request is
325  * finished (and request processor should come back some time later).
326  */
327 
h_memstick_read_dev_id(struct memstick_dev * card,struct memstick_request ** mrq)328 static int h_memstick_read_dev_id(struct memstick_dev *card,
329 				  struct memstick_request **mrq)
330 {
331 	struct ms_id_register id_reg;
332 
333 	if (!(*mrq)) {
334 		memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, &id_reg,
335 				  sizeof(struct ms_id_register));
336 		*mrq = &card->current_mrq;
337 		return 0;
338 	} else {
339 		if (!(*mrq)->error) {
340 			memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
341 			card->id.match_flags = MEMSTICK_MATCH_ALL;
342 			card->id.type = id_reg.type;
343 			card->id.category = id_reg.category;
344 			card->id.class = id_reg.class;
345 			dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
346 		}
347 		complete(&card->mrq_complete);
348 		return -EAGAIN;
349 	}
350 }
351 
h_memstick_set_rw_addr(struct memstick_dev * card,struct memstick_request ** mrq)352 static int h_memstick_set_rw_addr(struct memstick_dev *card,
353 				  struct memstick_request **mrq)
354 {
355 	if (!(*mrq)) {
356 		memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS,
357 				  (char *)&card->reg_addr,
358 				  sizeof(card->reg_addr));
359 		*mrq = &card->current_mrq;
360 		return 0;
361 	} else {
362 		complete(&card->mrq_complete);
363 		return -EAGAIN;
364 	}
365 }
366 
367 /**
368  * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
369  *                        complete
370  * @card - media device to use
371  */
memstick_set_rw_addr(struct memstick_dev * card)372 int memstick_set_rw_addr(struct memstick_dev *card)
373 {
374 	card->next_request = h_memstick_set_rw_addr;
375 	memstick_new_req(card->host);
376 	wait_for_completion(&card->mrq_complete);
377 
378 	return card->current_mrq.error;
379 }
380 EXPORT_SYMBOL(memstick_set_rw_addr);
381 
memstick_alloc_card(struct memstick_host * host)382 static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
383 {
384 	struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
385 					    GFP_KERNEL);
386 	struct memstick_dev *old_card = host->card;
387 	struct ms_id_register id_reg;
388 
389 	if (card) {
390 		card->host = host;
391 		dev_set_name(&card->dev, "%s", dev_name(&host->dev));
392 		card->dev.parent = &host->dev;
393 		card->dev.bus = &memstick_bus_type;
394 		card->dev.release = memstick_free_card;
395 		card->check = memstick_dummy_check;
396 
397 		card->reg_addr.r_offset = offsetof(struct ms_register, id);
398 		card->reg_addr.r_length = sizeof(id_reg);
399 		card->reg_addr.w_offset = offsetof(struct ms_register, id);
400 		card->reg_addr.w_length = sizeof(id_reg);
401 
402 		init_completion(&card->mrq_complete);
403 
404 		host->card = card;
405 		if (memstick_set_rw_addr(card))
406 			goto err_out;
407 
408 		card->next_request = h_memstick_read_dev_id;
409 		memstick_new_req(host);
410 		wait_for_completion(&card->mrq_complete);
411 
412 		if (card->current_mrq.error)
413 			goto err_out;
414 	}
415 	host->card = old_card;
416 	return card;
417 err_out:
418 	host->card = old_card;
419 	kfree_const(card->dev.kobj.name);
420 	kfree(card);
421 	return NULL;
422 }
423 
memstick_power_on(struct memstick_host * host)424 static int memstick_power_on(struct memstick_host *host)
425 {
426 	int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
427 
428 	if (!rc)
429 		rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
430 
431 	return rc;
432 }
433 
memstick_check(struct work_struct * work)434 static void memstick_check(struct work_struct *work)
435 {
436 	struct memstick_host *host = container_of(work, struct memstick_host,
437 						  media_checker);
438 	struct memstick_dev *card;
439 
440 	dev_dbg(&host->dev, "memstick_check started\n");
441 	pm_runtime_get_noresume(host->dev.parent);
442 	mutex_lock(&host->lock);
443 	if (!host->card) {
444 		if (memstick_power_on(host))
445 			goto out_power_off;
446 	} else if (host->card->stop)
447 		host->card->stop(host->card);
448 
449 	card = memstick_alloc_card(host);
450 
451 	if (!card) {
452 		if (host->card) {
453 			device_unregister(&host->card->dev);
454 			host->card = NULL;
455 		}
456 	} else {
457 		dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
458 			card->id.type, card->id.category, card->id.class);
459 		if (host->card) {
460 			if (memstick_set_rw_addr(host->card)
461 			    || !memstick_dev_match(host->card, &card->id)
462 			    || !(host->card->check(host->card))) {
463 				device_unregister(&host->card->dev);
464 				host->card = NULL;
465 			} else if (host->card->start)
466 				host->card->start(host->card);
467 		}
468 
469 		if (!host->card) {
470 			host->card = card;
471 			if (device_register(&card->dev)) {
472 				put_device(&card->dev);
473 				host->card = NULL;
474 			}
475 		} else {
476 			kfree_const(card->dev.kobj.name);
477 			kfree(card);
478 		}
479 	}
480 
481 out_power_off:
482 	if (!host->card)
483 		host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
484 
485 	mutex_unlock(&host->lock);
486 	pm_runtime_put(host->dev.parent);
487 	dev_dbg(&host->dev, "memstick_check finished\n");
488 }
489 
490 /**
491  * memstick_alloc_host - allocate a memstick_host structure
492  * @extra: size of the user private data to allocate
493  * @dev: parent device of the host
494  */
memstick_alloc_host(unsigned int extra,struct device * dev)495 struct memstick_host *memstick_alloc_host(unsigned int extra,
496 					  struct device *dev)
497 {
498 	struct memstick_host *host;
499 
500 	host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
501 	if (host) {
502 		mutex_init(&host->lock);
503 		INIT_WORK(&host->media_checker, memstick_check);
504 		host->dev.class = &memstick_host_class;
505 		host->dev.parent = dev;
506 		device_initialize(&host->dev);
507 	}
508 	return host;
509 }
510 EXPORT_SYMBOL(memstick_alloc_host);
511 
512 /**
513  * memstick_add_host - start request processing on memstick host
514  * @host - host to use
515  */
memstick_add_host(struct memstick_host * host)516 int memstick_add_host(struct memstick_host *host)
517 {
518 	int rc;
519 
520 	idr_preload(GFP_KERNEL);
521 	spin_lock(&memstick_host_lock);
522 
523 	rc = idr_alloc(&memstick_host_idr, host, 0, 0, GFP_NOWAIT);
524 	if (rc >= 0)
525 		host->id = rc;
526 
527 	spin_unlock(&memstick_host_lock);
528 	idr_preload_end();
529 	if (rc < 0)
530 		return rc;
531 
532 	dev_set_name(&host->dev, "memstick%u", host->id);
533 
534 	rc = device_add(&host->dev);
535 	if (rc) {
536 		spin_lock(&memstick_host_lock);
537 		idr_remove(&memstick_host_idr, host->id);
538 		spin_unlock(&memstick_host_lock);
539 		return rc;
540 	}
541 
542 	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
543 	memstick_detect_change(host);
544 	return 0;
545 }
546 EXPORT_SYMBOL(memstick_add_host);
547 
548 /**
549  * memstick_remove_host - stop request processing on memstick host
550  * @host - host to use
551  */
memstick_remove_host(struct memstick_host * host)552 void memstick_remove_host(struct memstick_host *host)
553 {
554 	flush_workqueue(workqueue);
555 	mutex_lock(&host->lock);
556 	if (host->card)
557 		device_unregister(&host->card->dev);
558 	host->card = NULL;
559 	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
560 	mutex_unlock(&host->lock);
561 
562 	spin_lock(&memstick_host_lock);
563 	idr_remove(&memstick_host_idr, host->id);
564 	spin_unlock(&memstick_host_lock);
565 	device_del(&host->dev);
566 }
567 EXPORT_SYMBOL(memstick_remove_host);
568 
569 /**
570  * memstick_free_host - free memstick host
571  * @host - host to use
572  */
memstick_free_host(struct memstick_host * host)573 void memstick_free_host(struct memstick_host *host)
574 {
575 	mutex_destroy(&host->lock);
576 	put_device(&host->dev);
577 }
578 EXPORT_SYMBOL(memstick_free_host);
579 
580 /**
581  * memstick_suspend_host - notify bus driver of host suspension
582  * @host - host to use
583  */
memstick_suspend_host(struct memstick_host * host)584 void memstick_suspend_host(struct memstick_host *host)
585 {
586 	mutex_lock(&host->lock);
587 	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
588 	mutex_unlock(&host->lock);
589 }
590 EXPORT_SYMBOL(memstick_suspend_host);
591 
592 /**
593  * memstick_resume_host - notify bus driver of host resumption
594  * @host - host to use
595  */
memstick_resume_host(struct memstick_host * host)596 void memstick_resume_host(struct memstick_host *host)
597 {
598 	int rc = 0;
599 
600 	mutex_lock(&host->lock);
601 	if (host->card)
602 		rc = memstick_power_on(host);
603 	mutex_unlock(&host->lock);
604 
605 	if (!rc)
606 		memstick_detect_change(host);
607 }
608 EXPORT_SYMBOL(memstick_resume_host);
609 
memstick_register_driver(struct memstick_driver * drv)610 int memstick_register_driver(struct memstick_driver *drv)
611 {
612 	drv->driver.bus = &memstick_bus_type;
613 
614 	return driver_register(&drv->driver);
615 }
616 EXPORT_SYMBOL(memstick_register_driver);
617 
memstick_unregister_driver(struct memstick_driver * drv)618 void memstick_unregister_driver(struct memstick_driver *drv)
619 {
620 	driver_unregister(&drv->driver);
621 }
622 EXPORT_SYMBOL(memstick_unregister_driver);
623 
624 
memstick_init(void)625 static int __init memstick_init(void)
626 {
627 	int rc;
628 
629 	workqueue = create_freezable_workqueue("kmemstick");
630 	if (!workqueue)
631 		return -ENOMEM;
632 
633 	rc = bus_register(&memstick_bus_type);
634 	if (rc)
635 		goto error_destroy_workqueue;
636 
637 	rc = class_register(&memstick_host_class);
638 	if (rc)
639 		goto error_bus_unregister;
640 
641 	return 0;
642 
643 error_bus_unregister:
644 	bus_unregister(&memstick_bus_type);
645 error_destroy_workqueue:
646 	destroy_workqueue(workqueue);
647 
648 	return rc;
649 }
650 
memstick_exit(void)651 static void __exit memstick_exit(void)
652 {
653 	class_unregister(&memstick_host_class);
654 	bus_unregister(&memstick_bus_type);
655 	destroy_workqueue(workqueue);
656 	idr_destroy(&memstick_host_idr);
657 }
658 
659 module_init(memstick_init);
660 module_exit(memstick_exit);
661 
662 MODULE_AUTHOR("Alex Dubov");
663 MODULE_LICENSE("GPL");
664 MODULE_DESCRIPTION("Sony MemoryStick core driver");
665