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